【发布时间】:2016-06-03 07:20:10
【问题描述】:
在下面的例子中,有没有办法避免执行 context.getBean()? testService 随后使用的所有其他 bean 都会自动装配。 (它是一个控制台应用程序)
public class Test {
private static ITestService testService;
private static ApplicationContext context;
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext(
new String[]{"/META-INF/spring/app-context.xml"});
ITestService testService = context.getBean(ITestService.class);
}
}
我尝试向 ApplicationContext 添加 autowire 注释,但没有奏效。另外,如果我自动装配它,它如何知道我的 app-context.xml 的位置?
更新:我找到了我需要的东西over here
【问题讨论】:
-
应用程序上下文不是常规 bean。实际上这个类包含所有其他 bean 定义。并且此类运行扫描
@Autowired注释。那么,如果没有创建上下文,谁来扫描? -
看看@RunWith(SpringJUnit4ClassRunner.class)