Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应用系统配置源。Spring中的几种容器都支持使用XML装配bean,包括:
XMLBeanFactory ,
ClassPathXMLApplicationContext ,
FileSystemXMLApplicationContext ,
XMLWebApplicationContext
一:XMLBeanFactory 引用资源
Resource resource = new ClassPathResource("appcontext.XML");
BeanFactory factory = new XMLBeanFactory(resource);
二:ClassPathXMLApplicationContext 编译路径
ApplicationContext factory=new ClassPathXMLApplicationContext("classpath:appcontext.XML");
ApplicationContext factory=new ClassPathXMLApplicationContext("appcontext.XML"); // src目录下的
ApplicationContext factory=new ClassPathXMLApplicationContext("conf/appcontext.XML"); // src/conf 目录下的
ApplicationContext factory=new ClassPathXMLApplicationContext("file:G:/Test/src/appcontext.XML");
三 : 用文件系统的路径
ApplicationContext factory=new FileSystemXMLApplicationContext("src/appcontext.XML");
//使用了 classpath: 前缀,作为标志, 这样,FileSystemXMLApplicationContext 也能够读入classpath下的相对路径
ApplicationContext factory=new FileSystemXMLApplicationContext("classpath:appcontext.XML");
ApplicationContext factory=new FileSystemXMLApplicationContext("file:G:/Test/src/appcontext.XML");
ApplicationContext factory=new FileSystemXMLApplicationContext("G:/Test/src/appcontext.XML");
四: XMLWebApplicationContext 是专为Web工程定制的。
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext );
Spring中加载ApplicationContext.xml文件的方式
1.利用ClassPathXmlApplicationContext
可以从classpath中读取XML文件
(1)
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao)context.getBean("userDao");
(2)
ClassPathXmlApplicationContext resource = new ClassPathXmlApplicationContext(new String[]{"applicationContext-ibatis-oracle.xml","applicationContext.xml","applicationContext-data-oracle.xml"}); BeanFactory factory = resource; UserDao userDao = (UserDao) factory.getBean("userDao");
. 2.利用ClassPathResource
可以从classpath中读取XML文件
Resource cr = new ClassPathResource("applicationContext.xml"); BeanFactory bf=new XmlBeanFactory(cr); UserDao userDao = (UserDao)bf.getBean("userDao");
//加载一个xml文件org.springframework.beans.factory.config.PropertyPlaceholderConfigurer不起作用
3.利用XmlWebApplicationContext读取
XmlWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setConfigLocations(new String[] {"/WEB-INF/ applicationContext.xml"); ctx.setServletContext(pageContext.getServletContext()); ctx.refresh(); UserDao userDao = (UserDao ) ctx.getBean("userDao ");
4.利用FileSystemResource读取
Resource rs = new FileSystemResource("D:/tomcat/webapps/test/WEB-INF/classes/ applicationContext.xml"); BeanFactory factory = new XmlBeanFactory(rs); UserDao userDao = (UserDao )factory.getBean("userDao");
值得注意的是:利用FileSystemResource,则配置文件必须放在project直接目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。
5.利用FileSystemXmlApplicationContext读取
可以指定XML定义文件的相对路径或者绝对路径来读取定义文件。
方法一:
String[] path={"WebRoot/WEB-INF/applicationContext.xml","WebRoot/WEB-INF/applicationContext_task.xml"}; ApplicationContext context = new FileSystemXmlApplicationContext(path);
方法二:
String path="WebRoot/WEB-INF/applicationContext*.xml"; ApplicationContext context = new FileSystemXmlApplicationContext(path);
方法三:
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:地址");
没有classpath的话就是从当前的工作目录
原文地址:http://blog.csdn.net/snowjlz/article/details/8158560