[转]使用@Test 也可以从spring容器中获取依赖注入
转自:http://blog.csdn.net/u010987379/article/details/52091790
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" })
- @TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class })
- @Transactional
- public class TestClass {
-
@Resource
private ServiceOne serviceOne; - //这个类会在执行时被注入,这里是按类型注入,如果想按名称注入,需要加上@Named注解,如@Named("class1")
- //实现类可以加上@Named("class1")注解,也可以是配置在配置文件中的
- @Test
- public void t1(){
- }
- }