Spring读取配置文件的几种方式

  1 import java.io.FileInputStream;
  2 import java.io.FileNotFoundException;
  3 import java.io.FileReader;
  4 import java.io.IOException;
  5 import java.io.Reader;
  6 import java.util.Properties;
  7 
  8 import org.junit.Test;
  9 import org.springframework.core.io.ClassPathResource;
 10 import org.springframework.core.io.FileSystemResource;
 11 import org.springframework.core.io.Resource;
 12 
 13 /**
 14  * BeanFactory只有依赖注入功能没有AOP功能, ApplicationContext继承自BeanFactory有AOP功能
 15  */
 16 public class GetBeanFactory {
 17     /**
 18      * 加载项目内的配置文件,读取classPath之下的文件
 19      */
 20     public void test01() {
 21         Resource resource = new ClassPathResource("applicationContext.xml");
 22         BeanFactory bf = new XmlBeanFactory(resource);
 23         StudentAction studentService = (StudentAction) bf
 24                 .getBean("StudentAction");
 25         System.out.println(studentService);
 26     }
 27 
 28     /**
 29      * 加载项目外的配置文件,File读取C盘下的文件
 30      */
 31     public void test02() {
 32         Resource resource = new FileSystemResource("C:/applicationContext.xml");
 33         BeanFactory beanFactory = new XmlBeanFactory(resource);
 34         StudentAction studentAction = (StudentAction) beanFactory
 35                 .getBean("studentAction");
 36         System.out.println(studentAction);
 37     }
 38 
 39     /**
 40      * 读取Tomcat中的application配置文件, 必须导入Spring3-Web.jar包
 41      */
 42     public void test03() {
 43         /*
 44          * 将下面的代码必须放到jsp页面里面执行 <% org.springframework.core.io.Resource
 45          * resource=null; org.springframework.beans.factory.BeanFactory
 46          * beanFactory=null; resource=new
 47          * org.springframework.web.context.support
 48          * .ServletContextResource(application
 49          * ,"/WEB-INF/classes/applicationContext.xml"); beanFactory=new
 50          * org.springframework.beans.factory.xml.XmlBeanFactory(resource);
 51          * System.out.println(beanFactory); %>
 52          */
 53     }
 54 
 55     /**
 56      * ApplicationContext继承自BeanFactory有AOP功能
 57      */
 58     public void test04() {
 59         ApplicationContext context = new ClassPathXmlApplicationContext(
 60                 "applicationContext.xml");
 61         StudentService studentService = (StudentService) context
 62                 .getBean("studentService");
 63         studentService.save(new Student("test", 22));
 64     }
 65 
 66     /**
 67      * ApplicationContext继承自BeanFactory有AOP功能
 68      */
 69     public void test05() {
 70         ApplicationContext context = new FileSystemXmlApplicationContext(
 71                 "C:/applicationContext.xml");
 72         System.out.println(context.getBeanDefinitionCount());// 定义bean的总数
 73     }
 74     /**
 75      * ApplicationContext继承自BeanFactory有AOP功能
 76      */
 77     public void test06() {
 78         String[] filepath = { "applicationContext.xml" };
 79         ApplicationContext factory = new ClassPathXmlApplicationContext(
 80                 filepath);
 81         StudentService studentService = (StudentService) factory
 82                 .getBean("studentService");
 83     }
 84     /**
 85      * 用Spring读取properties文件
 86      */
 87     @Test
 88     public void test07() throws Exception, Exception {
 89         Resource r = new ClassPathResource("ssh.properties");
 90         Properties p=new Properties();
 91         p.load(new FileInputStream(r.getFile()));
 92         System.out.println(p.get("studentDao"));
 93     }
 94     
 95     @Test
 96     public void test08() throws Exception, Exception {
 97         Resource r = new ClassPathResource("a.txt");
 98     }
 99 
100 }

 

posted on 2014-08-24 15:19  腾飞工作室  阅读(5858)  评论(0编辑  收藏  举报