• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

没有合格的bean … spring-webmvc:在applicationContext中定义的bean在ServletContext中不可见

spring-mvc 来源:AnNik 29次浏览

我在APplicationContext中定义的bean有问题。 (?可能是我的问题是应用的理解〜Servlet上下文中)没有合格的bean … spring-webmvc:在applicationContext中定义的bean在ServletContext中不可见

所以,我定义<豆ID =在 “为messageSource”:

的applicationContext.xml

<beans 
xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc 

http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 
     http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0. 
<!-- // configure manage of transactions using annotations @Transactional // --> 

<tx:annotation-driven /> 

<!-- // SPRING-SECURITY // --> 
<import resource="security-servlet.xml" /> 

<!-- // DB1 - Database Configuration // --> 
<context:property-placeholder location="/WEB-INF/spring/db.config/jdbc-db1.properties" /> 

<import resource="./db.config/DataSource-db1.xml" /> 
<import resource="./db.config/jpa-config-db1.xml" /> 

<!-- Auto scan the components --> 
<context:annotation-config /> 

<!-- // common beans for rest & mvc // --> 
<context:component-scan base-package="abc.def.data" /> 

<bean 
    id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource" 
> 
    <property 
     name="basename" 
     value="validation" /> 
</bean> 

也我有MVC-servlet.xml中在web.xml定义为:

<!-- // MVC // --> 
<servlet> 
    <servlet-name>mvc</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>WEB-INF/spring/mvc-servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>mvc</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
</servlet-mapping> 

在我@Controller我用豆为messageSource这样的方式:

@Controller 
@RequestMapping(value = "/admin/su") 
public class AdminSuController { 
private final Logger LOG = LoggerFactory.getLogger(getClass()); 
    ..... 
    @Autowired 
    private ResourceBundleMessageSource messageSource; 

,当我在MVC-servlet.xml中定义的bean – 只要我把它移动到(我的理解根上下文没有问题 为ServletContext)的applicationContext.xml(我这样做,要使用此豆REST-servlet.xml中和其他@控制器-S)我得到:

#19:41:26.594 [localhost-startStop-1] ERROR 

osweb.servlet.DispatcherServlet – 上下文初始化失败 org.springframework.beans.factory.BeanCreationException:错误 创建名为’addressController’的bean:注入自动装配的 依赖关系失败;嵌套的例外是 org.springframework.beans.factory .BeanCreationException:无法 自动装配领域:私人 org.springframework.context.support.ResourceBundleMessageSource abc.def.rest.controller.AddressController.messageSource;嵌套0​​例外是org.sp ringframework.beans.factory.NoSuchBeanDefinitionException:无类型的 排位豆 [org.springframework.context.support.ResourceBundleMessageSource] 找到依赖性:预期至少1豆,其有资格作为 自动装配候选为此依赖。依赖注解: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 〜[弹簧-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202) 〜[弹簧豆-4.1.4。 RELEASE.jar:4.1.4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 〜[spring-beans-4.1.4.RELEASE.jar:4.1.4 .RELEASE]在 org.springframework.beans.facto ry.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 〜[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory $ 1。 getObject(AbstractBeanFactory.java:303) 〜[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230 ) 〜[spring-beans-4.1.4.RELEASE.jar:4.1.4。RELEASE]在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 〜[弹簧豆-4.1.4.RELEASE.jar:4.1.4.RELEASE]在 org.springframework .beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 〜[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.beans.factory.support。 DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762) 〜[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757 ) 〜[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.context。 support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 〜[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java :663) 〜[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629) 〜[spring-webmvc -4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677) 〜[spring-webmvc-4.1.4.RELEASE.jar: 4.1.4.RELEASE] at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548) 〜[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489) 〜[spring-webmvc-4.1。 4.RELEASE.jar:4.1.4.RELEASE] org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) [spring-webmvc-4.1.4.RELEASE.jar:4.1.4。 (GenericServlet.java:158) [servlet-api.jar:3.0.FR] at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284) [catalina.jar:7.0.59] at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197) [catalina.jar:7.0.59] at org.apache.catalin a.core.StandardWrapper.load(StandardWrapper.java:1087) [catalina.jar:7.0.59] at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5262) [catalina.jar: 7.0.59]在 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5550) [catalina.jar:7.0.59]在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase。的java:150) [catalina.jar:7.0.59]在 org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) [catalina.jar:7.0.59]在 org.apache。 catalina.core.ContainerBase.addChild(ContainerBase.java:877) [catalina.jar:7.0.59] at org.apache.catalina。 core.StandardHost.addChild(StandardHost.java:649) [catalina.jar:7.0.59] at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1245) [catalina.jar:7.0。 59] at org.apache.catalina.startup.HostConfig $ DeployDirectory.run(HostConfig.java:1895) [catalina.jar:7.0.59] at java.util.concurrent.Executors $ RunnableAdapter.call(Executors。 java:471) [na:1.7.0] at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:334) [na:1.7.0] at java.util.concurrent.FutureTask。运行(FutureTask.java:166)[na:1.7.0] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor。java:1110) [na:1.7.0] at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:603) [na:1.7.0] at java.lang.Thread.run(Thread .java:722)[na:1.7.0]由org.springframework.beans.factory.BeanCreationException造成 : controller.AddressControll er.messageSource;嵌套0​​例外是 org.springframework.beans.factory.NoSuchBeanDefinitionException:无类型的 排位豆 [org.springframework.context.support.ResourceBundleMessageSource] 找到依赖性:预期至少1豆,其有资格作为 自动装配候选这种依赖性。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(必需=真)} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561) 〜 [spring-beans-4.1.4.RELEASE.jar:4.1.4.RELE ASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 〜[spring-beans-4.1 .4.RELEASE.jar:4.1.4.RELEASE]在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) 〜[弹簧豆-4.1.4.RELEASE.jar: 4.1.4.RELEASE] … 34个常用帧 省略引起者: org.springframework.beans.factory.NoSuchBeanDefinitionException:否 [org.springframework.context.support.ResourceBundleMessageSource] 类型发现依赖 排位豆:预期在勒AST 1种豆,其有资格作为 自动装配候选这种依赖性。依赖注解: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308) 〜[弹簧-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE]在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054) 〜[弹簧豆-4.1.4。 RELEASE.jar:4.1.4.RELEASE] org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949) 〜[spring-beans-4.1.4.RELEASE.jar:4.1.4 .RELEASE]在 org.springframework.beans.factory.annotation.AutowiredAnnotationB eanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) 〜[弹簧豆-4.1.4.RELEASE.jar:4.1.4.RELE ASE] … 36个共同帧 省略


===========解决方案如下:

https://stackoverflow.com/users/2843242/oerlien感谢 这里是回答Moving messageSource to applicationContext causes the default messageSource not to be visible in dispatcher-servlet context

In your applicationContext.xml: 

<bean id="baseMessageSource" class="org.springframework...YourMessageSourceClass"> 
    ... 
</bean> 

In your xxx-servlet.xml: 

<bean id="messageSource" parent="baseMessageSource" /> 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)