初始化beanfactory主要有以下的三种方式:
1、filesystemXml
1 Resource resource = new FileSystemResource("beans.xml");
2 BeanFactory factory = new XmlBeanFactory(resource);
2、一个classpathXml
1 ClassPathResource resource = new ClassPathResource("beans.xml");
2 BeanFactory factory = new XmlBeanFactory(resource); 3、多个classpathXml
1 ApplicationContext context = new ClassPathXmlApplicationContext(
2 new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
3 // of course, an ApplicationContext is just a BeanFactory
4 BeanFactory factory = (BeanFactory) context;初始话ApplicationContext同样也有三种方式:
1、ClassPathXmlApplicationContext。用来读取classpath中的xml文件
1 ApplicationContext context =
2 new ClassPathXmlApplicationContext("foo.xml"); 2、FileSystemXmlApplicationContext。用来读取filesystem中的xml文件
1 ApplicationContext context =
2 new FileSystemXmlApplicationContext("c:/foo.xml"); 3、XmlWebApplicationContext。用来读取包含在一个webApplication中的xml文件
ps:BeanFactory和ApplicationContext的重要区别:后者会在Context启动时预先读取所有singleton的类。