Spring MVC @EnableWebMvc Bean配置

时间:2014-12-28 04:32:36

标签: java spring spring-mvc dependency-injection annotations

我正在尝试使用@Bean批注配置要在Spring MVC Controller应用程序中使用的Bean。

据我所知,@ Bean注释等同于XML配置中的标记。我正在做的是以下内容:

具有配置的类

@Configuration
public class ContextConfig {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

但是当尝试在MVC控制器中自动装配myBean时,它会失败。

豆子注射

@Controller
public class HomeController {

    @Autowired
    private MyBean myBean;

    @RequestMapping({"/", "/home"})
    public ModelAndView home (ModelAndView model) {
        model.setViewName("home");
        return model;
    }
}

错误是:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.validation.ValidatorFactory com.proeza.sgs.controller.HomeController.factory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.validation.ValidatorFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.validation.ValidatorFactory com.proeza.sgs.controller.HomeController.factory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.validation.ValidatorFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.validation.ValidatorFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 24 more

最终评论

  1. 还有其他通过注释(@Bean)配置的bean,它们正在被注入而没有问题。
  2. MyBean类没有实现也没有扩展任何内容。
  3. 配置位置在 web.xml
  4. 中声明

    CONF:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.myapp.config</param-value>
    </context-param>
    

    ContextConfig 类位于该包

    知道为什么会这样吗?

2 个答案:

答案 0 :(得分:1)

该异常与MyBean类无关。例外情况说:

No qualifying bean of type [javax.validation.ValidatorFactory] found for dependency

Spring无法将javax.validation.ValidatorFactory注入HomeController类。检查ValidatorFactory类的配置。

答案 1 :(得分:0)

感谢Ilya的回应。 你是对的,我粘贴的堆栈不正确。原因是为了简化,我用MyBean给出了示例,我忘了将它重命名为堆栈。

问题已经解决了。 MyBean的真实名称是MessageResolver,发生的事情是我在ContextConfig类中已经有另一个名为messageResolver的方法,用于为Thymeleaf创建一个bean。区别在于我没有接收参数而另一个接收到MessageSource,但是......阅读Spring文档我发现了这个:

Spring Doc Quote:

  

2.2.6。自定义bean命名

     

默认情况下,JavaConfig使用@Bean方法的名称作为名称   结果豆。但是,使用可以覆盖此功能   BeanNamingStrategy扩展点。

Spring @Bean doc

所以,问题是bean没有被创建,因为它被覆盖了它的定义。 只需重命名messageResolver方法,错误就会消失。

再次感谢。此致!