读取spring配置文件的位置

来源:互联网 发布:浙大教授被骗婚 知乎 编辑:程序博客网 时间:2024/05/05 18:17

在spring 中 ,我们可以从项目的不同的文件位置读取spring 配置文件,相关位置有四种情况:

1> 在 源代码 src或与src 平齐的目录下

2> 在 WEB-INF 目录下,相对于WEB 工程

3> 在 源代码 src或src 的包下

4> 在 任意位置

 

相关代码如下:

 

Java代码  收藏代码
  1. import org.springframework.beans.factory.BeanFactory;  
  2. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  6. import org.springframework.core.io.FileSystemResource;  
  7. import org.springframework.core.io.Resource;  
  8. import org.springframework.web.context.support.XmlWebApplicationContext;  
  9.   
  10. public class ReadSpringContext {  
  11. /** 
  12.    * 读取spring配置文件的位置,在工作目录下<p> 
  13.    * 在eclipse工程中与工程名同级目录 
  14.    */  
  15.     public static ApplicationContext readFromProject(String xml) {  
  16.         //ApplicationContext context = new FileSystemXmlApplicationContext("one.xml");  
  17.         //UserBean ub = (UserBean)context.getBean("ub");  
  18.         //System.out.println(ub.getUid());  
  19.         return new FileSystemXmlApplicationContext(xml);  
  20.     }  
  21.       
  22.     /** 
  23.     * 读取spring配置文件的位置,在web-inf目录 
  24.     */  
  25.     public static ApplicationContext readFromWebinf() {  
  26.         //ApplicationContext context = new XmlWebApplicationContext();          
  27.         //UserBean ub = (UserBean)context.getBean("ub");  
  28.         //System.out.println(ub.getUid());  
  29.         return new XmlWebApplicationContext();  
  30.     }  
  31. /** 
  32.    * 读取spring配置文件的位置,在src或包目录 
  33.    */  
  34.     public static ApplicationContext readFromSrc(String xml) {  
  35.         //ApplicationContext context = new ClassPathXmlApplicationContext("one.xml");  
  36.         //ApplicationContext context = new ClassPathXmlApplicationContext("accp/y2/bean/one.xml");  
  37.         //UserBean ub = (UserBean)context.getBean("ub");  
  38.         //System.out.println(ub.getUid());  
  39.         return new ClassPathXmlApplicationContext(xml);  
  40.     }  
  41.     /** 
  42.     * 从任意位置读取spring配置文件 
  43.     */  
  44.     public static BeanFactory readFromAny(String xml) {  
  45.         //Resource rs=new FileSystemResource("d:/_temp/one.xml");  
  46.         //BeanFactory factory=new XmlBeanFactory(rs);  
  47.         //ApplicationContext context = new GenericApplicationContext();  
  48.         //Resource resource = context.getResource("file:d:/_temp/one.xml");  
  49.         //BeanFactory factory = new XmlBeanFactory(resource);  
  50.         //UserBean ub = (UserBean)factory.getBean("ub");  
  51.         //System.out.println(ub.getUid());  
  52.         Resource rs=new FileSystemResource(xml);  
  53.         return new XmlBeanFactory(rs);  
  54.     }  
  55.       
  56.     public static void main(String[] args) {  
  57.         //readFromSrc();  
  58.         //readFromProject();  
  59.         //readFromAny();  
  60.         //readFromWebinf();  
  61.     }  
  62. }  
0 0
原创粉丝点击