Spring--初始化IOC容器的几种方式
初始化beanfactory主要有以下的三种方式: 1、filesystemXmlCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1 Resource resource = new FileSystemResource(
·
初始化beanfactory主要有以下的三种方式:
1、filesystemXml
1、ClassPathXmlApplicationContext。用来读取classpath中的xml文件
ps:BeanFactory和ApplicationContext的重要区别:后者会在Context启动时预先读取所有singleton的类。
1、filesystemXml
1
Resource resource
=
new
FileSystemResource(
"
beans.xml
"
);
2 BeanFactory factory = new XmlBeanFactory(resource);
2、一个classpathXml
2 BeanFactory factory = new XmlBeanFactory(resource);
1
ClassPathResource resource
=
new
ClassPathResource(
"
beans.xml
"
);
2 BeanFactory factory = new XmlBeanFactory(resource);
3、多个classpathXml
2 BeanFactory factory = new XmlBeanFactory(resource);
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同样也有三种方式:
2 new String[] { " applicationContext.xml " , " applicationContext-part2.xml " });
3 // of course, an ApplicationContext is just a BeanFactory
4 BeanFactory factory = (BeanFactory) context;
1、ClassPathXmlApplicationContext。用来读取classpath中的xml文件
1
ApplicationContext context
=
2 new ClassPathXmlApplicationContext( " foo.xml " );
2、FileSystemXmlApplicationContext。用来读取filesystem中的xml文件
2 new ClassPathXmlApplicationContext( " foo.xml " );
1
ApplicationContext context
=
2 new FileSystemXmlApplicationContext( " c:/foo.xml " );
3、XmlWebApplicationContext。用来读取包含在一个webApplication中的xml文件
2 new FileSystemXmlApplicationContext( " c:/foo.xml " );
ps:BeanFactory和ApplicationContext的重要区别:后者会在Context启动时预先读取所有singleton的类。
更多推荐
已为社区贡献2条内容
所有评论(0)