以下示例后,这适用于JUnit测试:
@ContextConfiguration(locations = "classpath:/spring/context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class ServiceTest {
@Autowired
private Service service;
@Test
public void doSomething() {
assertEquals(0, service.getNumber()); ...
}
当我尝试将其转换为非测试代码时,这不起作用:
// @ContextConfiguration only works for testing (JUnit). What do I use here, instead?
// @ContextConfiguration(locations = "classpath:/spring/helloWorldContext.xml")
@Service // supposedly to get the Spring Container to 'see' this class
public class Accessor {
@Autowired( required=true )
private Service service;
@Autowired
public Accessor() {
return service.getNumber(); ...
}
基本上,我想以与使用测试类相同的方式使用非测试类。
答案 0 :(得分:0)
您可能需要通过将annotation-config添加到spring配置文件来启用它:
<context:annotation-config />
答案 1 :(得分:0)
据我所知,你不能为常规bean做,但是你可以创建@Configuration
类并将它放在你的bean旁边:
@Configuration
@ImportResource("classpath:/spring/context.xml")
public class ServiceConfiguration {}