Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧4控制器建议和初始绑定器_Java_Spring_Spring Mvc Initbinders - Fatal编程技术网

Java 弹簧4控制器建议和初始绑定器

Java 弹簧4控制器建议和初始绑定器,java,spring,spring-mvc-initbinders,Java,Spring,Spring Mvc Initbinders,我试图在@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.an

我试图在@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方法复制并粘贴到我的一个控制器中,它就可以很好地用于特定的控制器

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

你知道这是怎么回事吗?

所以对于任何遇到这个问题的人。。。下面是我为解决这个问题所做的

而不是

<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>


只是想知道你是否真的可以混合使用ControllerAdvice和EnableWebMvc?我不记得我在哪里见过其他人做同样的事情-但是删除它似乎没有什么区别。很可能这是一个类扫描问题。ControllerAdvice必须加载到与控制器相同的应用程序上下文中。由于您的建议在配置包中,您确定它是由spring初始化到servlect上下文中的吗?我的组件扫描位于root-context.xml中,这不是我应该放置它的地方吗?我有包名,比如com.myapp.spring.controllers.com.myapp.spring.configuration,我的XML文件包含以下内容