【问题标题】:Importing resource conditionally in spring application context在 Spring 应用程序上下文中有条件地导入资源
【发布时间】:2013-05-30 21:09:32
【问题描述】:

只有在满足特定条件的情况下,我才能在 spring 上下文中导入东西吗?

<!-- import ONLY IF current environment is NOT testing -->
<import resource="classpath:context/caching-context.xml" />

目前我通过在我的测试用例中导入完全不同的上下文来做到这一点

@ContextConfiguration(locations = { "classpath*:ApplicationContextTesting.xml" })

但也许有一个更优雅的解决方案,不为生产和测试维护两个单独的应用程序上下文。

【问题讨论】:

    标签: java spring


    【解决方案1】:

    使用 Spring 配置文件。

    <beans profile="not_test">
       <import resource="classpath:context/caching-context.xml" />
    </beans>
    

    Spring documentationblog post 中的更多信息。

    【讨论】:

    • 这只有放在配置文件的末尾才有效,因此之后没有 bean 定义(Spring 3.2)
    猜你喜欢
    • 2011-03-03
    • 1970-01-01
    • 2019-04-21
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    相关资源
    最近更新 更多