简体   繁体   English

Spring-如何@Autowire一个不调用ClassPathXmlApplicationContext的bean

[英]Spring - how to @Autowire a bean WITHOUT calling ClassPathXmlApplicationContext

是否可以在不调用的情况下自动装配bean:

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

If you mean no-xml conifg try this test 如果您的意思是no-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);
    }
}

it will show that T1 bean was injected into T2 bean 这表明T1豆已注入T2豆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM