public class BeanFactory {
/**
* [@param](https://my.oschina.net/u/2303379) args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建资源对象
ClassPathResource resource=new ClassPathResource("applicationContext.xml");
//创建bean工厂对象
DefaultListableBeanFactory factory=new DefaultListableBeanFactory();
//读工厂对象
XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory);
//读资源对象
reader.loadBeanDefinitions(resource);
//从beanfactory中取出具体对象
Book book=(Book) factory.getBean("book");
System.out.println(book.getId());
System.out.println(book.getName());
}
}
public class Test { public static void main(String args[]){ ClassPathXmlApplicationContext application=new ClassPathXmlApplicationContext("applicationContext.xml"); Book book=(Book) application.getBean("book"); System.out.println(book.getId()); System.out.println(book.getName()); }
}
一般建议第二种