简体   繁体   English

Spring MVC @EnableWebMvc Bean配置

[英]Spring MVC @EnableWebMvc Bean config

I´m trying to configure a Bean to be used in a Spring MVC Controller app using the @Bean annotation. 我正在尝试使用@Bean批注配置要在Spring MVC Controller应用程序中使用的Bean。

As far as I know, the @Bean annotation is equivalent to the tag in XML configuration. 据我所知,@ Bean注释等效于XML配置中的标记。 What I am doing is the following: 我在做什么如下:

The class with the configuration 具有配置的类

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

But when trying to autowire myBean in an MVC Controller it fails. 但是,当尝试在MVC Controller中自动装配myBean时,它将失败。

The bean injection 豆类注射

@Controller
public class HomeController {

    @Autowired
    private MyBean myBean;

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

The error is: 错误是:

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

Final comments 最后评论

  1. There are other beans configured via annotation (@Bean), that are being injected without problem. 还有其他通过注解(@Bean)配置的Bean,可以毫无问题地注入。
  2. The MyBean class is not implementing nor extending anything. MyBean类没有实现或扩展任何东西。
  3. The configuration location is declared in the web.xml 配置位置在web.xml中声明

Conf: CONF:

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

The ContextConfig class is in that package ContextConfig类在该程序包中

Any idea why is this happening? 知道为什么会这样吗?

The exception is not related to the MyBean class. 该异常与MyBean类无关。 The exception says: 异常说明:

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

Spring failed to inject javax.validation.ValidatorFactory to the HomeController class. Spring无法将javax.validation.ValidatorFactory注入HomeController类。 Check that configuration for ValidatorFactory class. 检查ValidatorFactory类的配置。

Thanks for the response Ilya. 感谢您的回应伊利亚。 You´re right, the stack I pasted was not the correct one. 您是对的,我粘贴的堆栈不是正确的堆栈。 The cause was that to simplify, I gave the example with MyBean and I forgot to rename it in the stack. 原因是为了简化,我使用MyBean给出了示例,但忘记了在堆栈中重命名它。

The problem has been solved. 问题已经解决。 The real name of MyBean is MessageResolver and what was happening was that I already had another method called messageResolver inside the ContextConfig class, used to create a bean for Thymeleaf. MyBean的真实名称是MessageResolver,发生的事情是我在ContextConfig类中已经有了另一个称为messageResolver的方法,该方法用于为Thymeleaf创建bean。 The difference is that mine does not receive parameters and the other receives a MessageSource, BUT...Reading the Spring docmentation I found this: 区别在于我的不接收参数,而其他人则接收到MessageSource,但是...阅读Spring文档,我发现:

Spring Doc Quote: Spring Doc Quote:

2.2.6. 2.2.6。 Customizing bean naming 自定义Bean命名

By default, JavaConfig uses a @Bean method's name as the name of the resulting bean. 默认情况下,JavaConfig使用@Bean方法的名称作为结果bean的名称。 This functionality can be overridden, however, using the BeanNamingStrategy extension point. 但是,可以使用BeanNamingStrategy扩展点来覆盖此功能。

Spring @Bean doc Spring @Bean文档

So, the problem is that the bean was not being created because it was being overrided its definition. 因此,问题在于未创建该Bean,因为它已被覆盖其定义。 The error disappears just renaming the messageResolver method. 只是重命名messageResolver方法,错误消失了。

Thanks again. 再次感谢。 Regards! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM