在我的web应用程序中,applicationContext中有两个数据源bean,一个用于real-env,另一个用于执行测试。 dataSource对象被注入到一个DAO类中。使用Spring配置文件如何配置测试数据源应该在运行时注入DAO Test(JUnit)和real-env dataSource应该注入DAO而代码部署到real-env?使用Spring配置文件向DAO注入特定bean
0
A
回答
1
其实有两种方式来实现自己的目标:
- 一种方法是使用两个不同的Spring配置文件,一个用于测试(JUnit的),另一个用于运行时(实ENV)。
用于真正的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中指定默认弹簧-config.xml中作为contextConfigLocation春季在运行时使用:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/default-spring-config.xml</param-value>
</context-param>
最后指定测试 - 弹簧 - config.xml中在ContextConfiguration用于测试:
@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...
}
}
- 第二种方法是使用Spring型材正如你所建议的那样(即使我更喜欢第一个,因为它对这种情况更合适)。
首先添加这些行到你的web.xml让春天知道默认配置文件(实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然后注入数据源到你的测试类
@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
相关问题
- 1. Spring 3不注入DAO bean
- 2. 向spring bean注入xml文件
- 3. 向Spring bean注入文件资源
- 4. 向EJB3注入Spring bean
- 5. 如何使用Guice向bean注入简单的配置参数?
- 6. spring bean配置
- 7. Spring bean未注入
- 8. 如何向Apache Wink注入Spring bean?
- 9. Perf4j Spring bean上的配置文件注释... jmsConnectionFactory的错误
- 10. 无法从URL位置导入Spring安全bean配置文件
- 11. 是否可以在Spring中使用XML文件注入bean?
- 12. 使用CDI注入Spring bean @Inject
- 13. Spring bean配置xml
- 14. 如何使用XML配置文件配置Spring 4中提供的条件bean
- 15. 在使用@Autowired时向Spring bean注入基本属性?
- 16. 将当前日期字符串注入到Spring bean配置中
- 17. Apache CXF spring bean未注入
- 18. JSF 2.0的Spring bean注入
- 19. 将FacesContext注入Spring bean
- 20. 的Spring bean字段注入
- 21. Spring bean GeoModule bean注入到RepositoryRestMvcConfiguration
- 22. 向Spring管理的上下文中注入Bean
- 23. 你在哪里定义的Spring bean配置文件
- 24. 使用特定的Automapper配置文件
- 25. Spring java配置bean定义覆盖
- 26. 无法使用相对路径导入Spring bean定义文件
- 27. Spring中的Bean配置
- 28. Java spring bean配置错误
- 29. 使用Spring @Configuration注释注入bean列表
- 30. 如何在DAO中同时使用Spring验证注解(Bean验证规范)