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