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");
    }

}

猜你喜欢

转载自blog.csdn.net/m0_37542889/article/details/83308277