【问题标题】:Injecting specific bean to DAO using Spring profile使用 Spring 配置文件将特定 bean 注入 DAO
【发布时间】:2017-06-28 22:48:13
【问题描述】:

在我的 web 应用程序中,applicationContext 中有两个数据源 bean,一个用于真实环境,一个用于执行测试。 dataSource 对象被注入到一个 DAO 类中。使用 Spring 配置文件我如何配置测试数据源应该在运行测试(JUnit)时注入 DAO 和 real-env 数据源应该在代码部署到 real-env 时注入 DAO?

【问题讨论】:

    标签: spring dependency-injection junit4 spring-profiles springjunit4classrunner


    【解决方案1】:

    其实有两种方法可以实现你的目标:

    1. 一种方法是使用两个不同的 spring 配置文件,一个用于测试 (JUnit),另一个用于运行时 (real-env)。

    用于真实环境:

    <!-- default-spring-config.xml -->
    <beans>
    
        ...other beans goes here...
    
        <bean id="datasource" class="xx.yy.zz.SomeDataSourceProvider1" />
    
    </beans>
    

    用于测试:

    <!-- test-spring-config.xml -->
    <beans>
    
        ...other beans goes here...
    
        <bean id="datasource" class="xx.yy.zz.SomeDataSourceProvider2" />
    
    </beans>
    

    在您的 web.xml 中,将 default-spring-config.xml 指定为 spring 在运行时使用的 contextConfigLocation

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/default-spring-config.xml</param-value>
    </context-param>
    

    最后在 ContextConfiguration 中为 Test 指定 test-spring-config.xml

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("/test-spring-config.xml")// it will be loaded from "classpath:/test-spring-config.xml"
    public class YourClassTest {
    
        @Autowired
        private DataSource datasource;
    
        @Test
        public void your_function_test() {
    
            //use datasource here...
    
        }
    }
    

    1. 第二种方法是按照您的建议使用 Spring 配置文件(即使我更喜欢第一种,因为它更适合这种情况)。

    首先将这些行添加到您的 web.xml 以让 Spring 了解默认配置文件 (real-env):

    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>default</param-value
    </context-param>
    

    现在在你的 Spring 配置文件(一个单一的配置文件)中创建两个不同的数据源:

    <beans xmlns="http://www.springframework.org/schema/beans"
        xsi:schemaLocation="...">
    
        ...other beans goes here...
    
        <beans profile="default">
    
            <bean id="defaultDatasource" class="xx.yy.zz.SomeDataSourceProvider1" />
    
        </beans>
    
        <beans profile="test" >
    
            <bean id="testDatasource" class="xx.yy.zz.SomeDataSourceProvider2" />
    
        </beans>
    
    </beans>
    

    然后使用 ActiveProfiles 将 DataSource 注入您的测试类:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    @ActiveProfiles("test")
    public class YourClassTest {
    
        @Autowired
        private DataSource datasource;
    
        @Test
        public void your_function_test() {
    
            //use datasource here...
    
        }
    }
    

    来源:https://spring.io/blog/2011/06/21/spring-3-1-m2-testing-with-configuration-classes-and-profiles

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2010-09-23
      • 2012-10-28
      相关资源
      最近更新 更多