源码注释性容器的创建及初始化

ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); 
MyService myService = ctx.getBean(MyService.class);  // by type... myService.doStuff();

动态注册:

public static void main(String[] args) {

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); 
ctx.register(AppConfig.class, OtherConfig.class);
ctx.register(AdditionalConfig.class); ctx.refresh(); MyService myService = ctx.getBean(MyService.class); myService.doStuff(); }

 主动扫描package:

public static void main(String[] args) {

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.scan("com.acme");

    ctx.refresh();

    MyService myService = ctx.getBean(MyService.class);

}

 

你可能感兴趣的:(初始化)