2013-04-09 131 views
0

是否有可能自动装配豆而不调用:春天 - 如何@Autowire一个bean,而无需调用的ClassPathXmlApplicationContext

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
+2

不需要。至少有一个Spring Context需要创建到Autowire或从Context中执行依赖注入 – shazin 2013-04-09 06:01:20

+0

绝对不是。这是一个独立的应用程序,您应该了解所有首先必须由Spring管理的自动布线bean,然后发生其他事情。 – OQJF 2013-04-09 06:14:56

回答

1

如果你的意思是没有的XML conifg试试这个测试

class T1 { 
} 

class T2 { 
    @Autowired 
    T1 t1; 
} 

public class Main { 

    public static void main(String[] args) throws Exception { 
     AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); 
     ctx.register(T1.class, T2.class); 
     ctx.refresh(); 
     System.out.println(ctx.getBean(T2.class).t1); 
    } 
} 

它会显示T1 bean被注入到T2 bean中