读取spring配置文件的位置

读取spring配置文件的位置
2008-08-23 10:17
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.XmlWebApplicationContext;

public class ReadSpringContext {
/**
   * 读取spring配置文件的位置,在工作目录下<p>
   * 在eclipse工程中与src平级的目录下
   */
    public static ApplicationContext readFromProject(String xml) {
        //ApplicationContext context = new FileSystemXmlApplicationContext("one.xml");
        //UserBean ub = (UserBean)context.getBean("ub");
        //System.out.println(ub.getUid());
        return new FileSystemXmlApplicationContext(xml);
    }
   
    /**
    * 读取spring配置文件的位置,在web-inf目录
    */
    public static ApplicationContext readFromWebinf() {
        //ApplicationContext context = new XmlWebApplicationContext();       
        //UserBean ub = (UserBean)context.getBean("ub");
        //System.out.println(ub.getUid());
        return new XmlWebApplicationContext();
    }
/**
   * 读取spring配置文件的位置,在src或包目录
   */
    public static ApplicationContext readFromSrc(String xml) {
        //ApplicationContext context = new ClassPathXmlApplicationContext("one.xml");
        //ApplicationContext context = new ClassPathXmlApplicationContext("accp/y2/bean/one.xml");
        //UserBean ub = (UserBean)context.getBean("ub");
        //System.out.println(ub.getUid());
        return new ClassPathXmlApplicationContext(xml);
    }
    /**
    * 从任意位置读取spring配置文件
    */
    public static BeanFactory readFromAny(String xml) {
        //Resource rs=new FileSystemResource("d:/_temp/one.xml");
        //BeanFactory factory=new XmlBeanFactory(rs);
        //ApplicationContext context = new GenericApplicationContext();
        //Resource resource = context.getResource("file:d:/_temp/one.xml");
        //BeanFactory factory = new XmlBeanFactory(resource);
        //UserBean ub = (UserBean)factory.getBean("ub");
        //System.out.println(ub.getUid());
        Resource rs=new FileSystemResource(xml);
        return new XmlBeanFactory(rs);
    }
   
    public static void main(String[] args) {
        //readFromSrc();
        //readFromProject();
        //readFromAny();
        //readFromWebinf();
    }
}

你可能感兴趣的:(eclipse,spring,Web,xml,bean)