在下面的例子中,有沒有辦法避免做一個context.getBean()? testService隨後使用的所有其他bean都會自動裝配。 (這是一個控制檯應用程序)在春天,是否有自動裝配第一個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,但它沒有工作。除了它如何知道我的app-context.xml位於何處?
更新:我發現我需要什麼over here
應用程序上下文不是常規bean。實際上這個類包含了所有其他的bean定義。這個類運行'@ Autowired'註釋。所以,如果上下文沒有創建,誰會掃描? –
查看@RunWith(SpringJUnit4ClassRunner.class) –