SomeService service = ac.getBean(“someServiceImpl”, SomeService.class);
"someServiceImpl"是.xml文件中bean标签中id。
SomeService.class是service的类型的class。
@TestpublicvoidtestSome02(){//创建容器对象,ApplicationContext初始化时,所有容器中beans创建完毕
ApplicationContext ac =newClassPathXmlApplicationContext("applicationContext.xml");
SomeService service = ac.getBean("someServiceImpl", SomeService.class);
service.doSome();}
创建Beanfactory对象。
@TestpublicvoidtestSome03(){//创建容器对象,BeanFactory当调用getBean的时候获取相应对象时,才创建对象
BeanFactory bf =newXmlBeanFactory(newClassPathResource("applicationContext.xml"));
SomeService service = bf.getBean("someServiceImpl", SomeService.class);
service.doSome();}