您的位置:首页

获取bean的两种方式

2016-05-04 11:05 120 查看
BeanFactory方式:

1:

public void testFactory(){
ResourcePatternResolver rpt=new PathMatchingResourcePatternResolver();
Resource resource=rpt.getResource("com/test/applicationContext.xml");
BeanFactory bf=new XmlBeanFactory(resource);
Dog dog=(Dog) bf.getBean("dog");
dog.shou();
}


2:

@Test
public  void test2(){
XmlBeanFactory factory=new XmlBeanFactory(
new ClassPathResource("com/test/applicationContext.xml"));
IService hello=(IService) factory.getBean("serviceImpl");
hello.service("Helloween");
}


ApplicationContext方式:

public void testContext(){
ApplicationContext ac=
new ClassPathXmlApplicationContext("com/test/applicationContext.xml");
Dog dog=(Dog) ac.getBean("dog");
dog.shou();
System.out.println(dog.getName()+"\t"+dog.getAge());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: