在我的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注入文件资源
- 3. 向spring bean注入xml文件
- 4. Spring 3.1使用bean定义配置文件的bean可见性
- 5. 向EJB3注入Spring bean
- 6. 向EJB3注入Spring bean
- 7. 通过弹簧配置文件向DAO注入查询
- 8. spring 3 bean配置文件
- 9. 如何向Spring中注入Spring bean DefultProducer
- 10. Spring + Hibernate + DAO + SessionFactory +注入
- 11. 如何使用Guice向bean注入简单的配置参数?
- 12. spring bean配置
- 13. 如何使用spring配置multisupport dao
- 14. Spring bean未注入
- 15. 如何向Apache Wink注入Spring bean?
- 16. 向Spring bean注入依赖关系
- 17. 如何将Spring bean id注入到另一个Spring配置Bean中?
- 18. Perf4j Spring bean上的配置文件注释... jmsConnectionFactory的错误
- 19. 无法从URL位置导入Spring安全bean配置文件
- 20. 是否可以在Spring中使用XML文件注入bean?
- 21. 使用CDI注入Spring bean @Inject
- 22. Beetwen在Spring MVC中使用Bean配置文件和注解的区别?
- 23. Spring bean配置xml
- 24. 如何使用XML配置文件配置Spring 4中提供的条件bean
- 25. 在使用@Autowired时向Spring bean注入基本属性?
- 26. 在Spring Boot中使用特定配置文件打包战争
- 27. 将当前日期字符串注入到Spring bean配置中
- 28. 的Spring bean当使用编程配置
- 29. Spring FileSystemXmlApplicationContext找不到bean配置文件并进入错误
- 30. 在Spring Bean中注入ResourceBundle