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

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

/**
 * BeanFactory只有依赖注入功能没有AOP功能, ApplicationContext继承自BeanFactory有AOP功能
 */
public class GetBeanFactory {
    /**
     * 加载项目内的配置文件,读取classPath之下的文件
     */
    public void test01() {
        Resource resource = new ClassPathResource("applicationContext.xml");
        BeanFactory bf = new XmlBeanFactory(resource);
        StudentAction studentService = (StudentAction) bf
                .getBean("StudentAction");
        System.out.println(studentService);
    }

    /**
     * 加载项目外的配置文件,File读取C盘下的文件
     */
    public void test02() {
        Resource resource = new FileSystemResource("C:/applicationContext.xml");
        BeanFactory beanFactory = new XmlBeanFactory(resource);
        StudentAction studentAction = (StudentAction) beanFactory
                .getBean("studentAction");
        System.out.println(studentAction);
    }

    /**
     * 读取Tomcat中的application配置文件, 必须导入Spring3-Web.jar包
     */
    public void test03() {
        /*
         * 将下面的代码必须放到jsp页面里面执行 <% org.springframework.core.io.Resource
         * resource=null; org.springframework.beans.factory.BeanFactory
         * beanFactory=null; resource=new
         * org.springframework.web.context.support
         * .ServletContextResource(application
         * ,"/WEB-INF/classes/applicationContext.xml"); beanFactory=new
         * org.springframework.beans.factory.xml.XmlBeanFactory(resource);
         * System.out.println(beanFactory); %>
         */
    }

    /**
     * ApplicationContext继承自BeanFactory有AOP功能
     */
    public void test04() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        StudentService studentService = (StudentService) context
                .getBean("studentService");
        studentService.save(new Student("test", 22));
    }

    /**
     * ApplicationContext继承自BeanFactory有AOP功能
     */
    public void test05() {
        ApplicationContext context = new FileSystemXmlApplicationContext(
                "C:/applicationContext.xml");
        System.out.println(context.getBeanDefinitionCount());// 定义bean的总数
    }
    /**
     * ApplicationContext继承自BeanFactory有AOP功能
     */
    public void test06() {
        String[] filepath = { "applicationContext.xml" };
        ApplicationContext factory = new ClassPathXmlApplicationContext(
                filepath);
        StudentService studentService = (StudentService) factory
                .getBean("studentService");
    }
    /**
     * 用Spring读取properties文件
     */
    @Test
    public void test07() throws Exception, Exception {
        Resource r = new ClassPathResource("ssh.properties");
        Properties p=new Properties();
        p.load(new FileInputStream(r.getFile()));
        System.out.println(p.get("studentDao"));
    }
    
    @Test
    public void test08() throws Exception, Exception {
        Resource r = new ClassPathResource("a.txt");
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值