【发布时间】:2015-03-13 06:39:58
【问题描述】:
我创建了一个配置了父子应用程序上下文的 Web 项目。根应用程序上下文配置了基础设施 bean(Datasource、Transaction、ORM、缓存、安全性),以及配置了视图解析器的 Web 应用程序上下文,所有这些 bean 都在相关配置文件下分组,如下所示:
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>
xml,
cache,JDK,Ehcache,Redis,Memcache,xmemcached-Client,Composite,
db,C3P0,
orm,Hibernate,Hibernate4,
transaction
</param-value>
</context-param>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:servlet/rest-servlet.xml</param-value>
</init-param>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>xml,combined</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
当我尝试启动这个项目时,我收到了这个错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheTestController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.test.springbasic.service.impl.ContactService org.test.springbasic.controller.CacheTestController.contactService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.test.springbasic.service.impl.ContactService] 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)}
我想知道 spring profile 应用程序上下文是否特定(我的控制器在子上下文下,我的服务在根上下文下并自动连接到控制器),ContactService 用@Profile({ "Hibernate" }) 注释,为什么我不能得到 bean , 在那里,我已经激活了spring.profiles.default context-param 下的“Hibernate”配置文件!
【问题讨论】:
标签: spring profile applicationcontext