在遵循示例之后,这适用于 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(); ...
}
基本上,我想以与使用测试类相同的方式使用非测试类。