Spring Profile应用程序上下文是否特定?

时间:2015-03-13 06:39:59

标签: spring profile applicationcontext

我已经创建了一个配置了父子应用程序上下文的Web项目。使用视图解析器配置的基础结构bean(Datasource,事务,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" }),为什么我可以&# 39;得到豆子,就在那里,我已经激活了&#34; Hibernate&#34; spring.profiles.default context-param下的个人资料!

1 个答案:

答案 0 :(得分:0)

可以通过多种方式激活配置文件。现在让我们看看哪一个优先于另一个,如果你使用多个优先级 - 从最高优先级到最低优先级会发生什么:

  1. web.xml中的上下文参数
  2. WebApplicationInitializer
  3. JVM系统参数
  4. 环境变量
  5. 根据以上信息,init-param中设置的个人资料会覆盖context-param

    中设置的个人资料