世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
public static void main(String[] args) {
// 通过xml获取bean
String xmlPath = "application-context.xml";
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(xmlPath);
BeanService service = (BeanService) context.getBean("beanService");
System.out.println("xml bean : " + service);
service.print();
// 通过annotation获取bean
AnnotationConfigApplicationContext aContext =
new AnnotationConfigApplicationContext(Config.class);
BeanService bService = (BeanService) aContext.getBean("beanService");
System.out.println("anno bean : " + bService);
bService.print();
// 通过annotation扫描
AnnotationConfigApplicationContext annoContext =
new AnnotationConfigApplicationContext();
annoContext.register(ScanConfig.class);
annoContext.refresh();
BeanService beanService = (BeanService) annoContext.getBean("beanService");
System.out.println("scan anno bean : " + beanService);
beanService.print();
}
更多信息,请关注公众号: