2

@ControllerAdvice クラス内の @InitBinder アノテーション付きメソッドを使用して、グローバル InitBinder を登録しようとしています。

package com.myapp.spring.configuration;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@ControllerAdvice
@EnableWebMvc
public class CustomInitBinder {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        System.out.println("INIT BINDER");
        binder.registerCustomEditor(java.sql.Date.class, new SqlDatePropertyEditor());
        binder.registerCustomEditor(java.sql.Timestamp.class, new SqlTimestampPropertyEditor());
    }

}

私が抱えている問題は、ロード時に @InitBinder を見つけていることですが、「INIT BINDER」が System.out に出力されないため、実際にはメソッドに入らないことです。これは、カスタム エディターが登録されていないため、機能しないことを意味します。initBinder メソッドをコントローラの 1 つにコピー アンド ペーストすると、その特定のコントローラで問題なく動作します。

1989  INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (RequestMappingHandlerAdapter.java:636) - Detected @InitBinder methods in customInitBinder

ここで何が起こっているのですか?

4

1 に答える 1

2

したがって、この問題に遭遇した人のために...これを解決するために私がしたことは次のとおりです。

持つ代わりに

<context:component-scan base-package="com.myapp.spring"></context:component-scan> 

私のroot-context.xmlで

これを構成パッケージに変更しました

<context:component-scan base-package="com.myapp.spring.configuration"></context:component-scan>

次に、@ControllerAdvice アノテーション付きクラスを com.myapp.spring.controllers に移動し、servlet-context.xml に追加しました

<context:component-scan base-package="com.myapp.spring.controllers">
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
于 2014-02-24T03:57:11.327 に答える