配置文件load的几种方法(转)

1. InputStreamResource(InputStream inputStream) 加载 Bean.xml

is = new FileInputStream("D:\\workspace\\mycripts\\src\\net\\villion\\test\\spring\\beans.xml");  

            Resource isr = new InputStreamResource(is)

xbf=new XmlBeanFactory(isr);  

2。利用 ClassPathResource(String path) 加载 Bean.xml

ClassPathResource res = new ClassPathResource("build.xml");  

XmlBeanFactory factory = new XmlBeanFactory(res);  

3

ApplicationContext  ctx = new ClassPathXmlApplicationContext("build.xml");  OR

 ApplicationContext context = new ClassPathXmlApplicationContext( 

        new String[] {"services.xml", "daos.xml"}); 

当前的*.xml文件路径是相对于WEB-INF/class路径 

4 .利用 FileSystemResource(String path) 加载 Bean.xml

如果配置文件在WEB-ROOT目录下的话可以用 

applicationContext = new FileSystemXmlApplicationContext("applicationContext.xml");

ApplicationContext context = new FileSystemXmlApplicationContext( 

new String[] { "WebRoot/frameworkconfig/springframeworkconfig.xml" }); 

5.XmlBeanFactory ctx = new XmlBeanFactory(new ClassPathResource("build.xml"));  

猜你喜欢

转载自datum.iteye.com/blog/1363906