【问题标题】:Is Spring Profile application context specific?Spring Profile 应用程序上下文是特定的吗?
【发布时间】: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


    【解决方案1】:

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

    1. web.xml 中的上下文参数
    2. WebApplicationInitializer
    3. JVM 系统参数
    4. 环境变量

    根据以上信息,init-param 中设置的配置文件会覆盖context-param 中设置的配置文件

    【讨论】:

    • 谢谢,但是当我将“休眠”配置文件添加到 时,它起作用了,并且由于您对优先权的回答,我不应该获得基础设施 bean,因为配置文件被 覆盖
    • 再次感谢。但这可能不是正确的配置。
    猜你喜欢
    • 2012-01-20
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 2010-09-12
    • 2016-08-27
    相关资源
    最近更新 更多