2012-01-18 29 views
2

コントローラのデフォルトのコンストラクタにいくつかの初期化メソッドを入れようとしていますが、決して呼び出されないという問題があります。 @Autowiredアノテーションを置くと、エラーが発生します - Autowiredアノテーションには、少なくとも引数に必要です。Spring MVCコントローラのコンストラクタ

初期化コードを各コントローラのメソッドに置くことを除いて、1つの場所に置くベストプラクティスは何ですか? はありがとう

@InitBinder 
public void initBinder(WebDataBinder binder) { 
    try { 
     initialize(); 
     Logger l = Logger.getLogger(this.getClass().getName()); 
     l.warning("Init!!!"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    <context:component-scan base-package="de.butler.crm.controller" /> 
    <mvc:annotation-driven /> 
    <bean id="localeChangeInterceptor" 
     class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
     <property name="paramName" value="l" /> 
    </bean> 
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" > 
     <property name="interceptors"> 
      <list> 
      <ref bean="localeChangeInterceptor" /> 
      </list> 
     </property> 
    </bean> 
    <bean id="messageSource" 
     class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <property name="basename" value="de.butler.crm.resource.Resources" /> 
    </bean> 
    <bean id="localeResolver" 
     class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> 
     <property name="defaultLocale" value="de" /> 
    </bean> 
    <bean id="multipartResolver" 
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
     <property name="maxUploadSize" value="50000000"/> 
    </bean> 
    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/view/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
</beans> 
+0

を行うために、このメソッドを使用する必要がありますので、この2つの方法は@PostConstructと@PreDestroyを呼び出します私はあなたに何かを見ることができませんXML。コントローラクラスも投稿できますか? –

答えて

13

the Spring Bean lifecycleのすべての側面が適用されるようにa)のコントローラは、単なる春の豆です。

I.e.あなたはautowireプロパティまたは(annotation support付き)コンストラクタのパラメータは、the InitializingBean interfacea @PostConstruct methodを使って豆を初期化することができますすることができますなど

これのどれもが、その後、あなたのセットアップに何か問題がありません動作し、あなたのWebコンテキストXMLをポストする必要があります場合はおよび/またはスタックトレースを含むことができる。あなたのコードの一部を使用してBeanを初期化したい.IF

B)あなたが要求ごとの設定が必要な場合は、その後、春には、Beanのデフォルトのライフサイクルによって@InitBinder mechanism

+0

そして、@ItitBinderメソッドは各リクエストごとに実行されますか? – nKognito

+0

ところで、それは、コントローラは、要求 – nKognito

+0

@InitBinder 公共ボイドinitBinder(WebDataBinderバインダー){ \t \t試み{ \t \t \tが初期化()でそのmetnodを実行しない役に立ちません。 \t \t \t \t \t \tロガーL = Logger.getLogger(this.getClass()のgetName()。)。 \t \t \t l.warning( "Init !!!"); \t \t \t \t \t} \t \tキャッチ(例外e){ \t \t \t試み{ \t \t \t \t eventsLogService.add(新規のLogEvent(例えば、あるCurrentUser、新しいDate()))。 \t \t \t}キャッチ(例外例){ \t \t \t \t ex.printStackTrace(); \t \t \t} \t \t}} – nKognito

0

を使用するには、Springコンテナによって管理されます@postconstructメソッドでそれを行うことができます。

あなたのBeanのリスナーを作成する必要がありますし、あなたのBeanが初期化されるたびに、物事

関連する問題