欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring ApplicationContext and Resource

程序员文章站 2022-05-25 11:55:05
...

  Spring提供了多种加载配置文件的类和接口,其中常用的有三个,第一个是FileSystemXmlApplicationContext,第二个是ClassPathXmlApplicationContext,第三个是XmlWebApplicationContext,三个类都实现了ApplicationContext接口。第一个从文件系统中加载配置文件,需要文件的绝对路径,第二个会从项目的类路径中查找配置文件,即classes文件夹下,第三个是特定针对Web项目而设计的,会从WEB-INF文件夹下查找配置文件。Spring还为我们提供了一个辅助类WebApplicationContextUtils用来处理以第三种方式加载配置文件。

 

  Spring提供了一个Resource接口用于处理项目资源文件,常用实现有四种,第一个是FileSystemResource,第二个ClassPathResource,第三个是ServletContextResource,第三个是UrlResource。同理ApplicationContext,第一个是从文件系统中查找文件,第二个是从项目类路径中查找文件,第三个是从Web项目上下文件中查找文件。Spring也为我们提供了一个辅助类ResourceUtils来处理文件查找与加载。

 

  Spring提供了一个PropertiesLoaderUtils用于编程时获取属性文件中的键值对,可以使用Resource来加载属性文件。

 

 


package com.spring;

import com.dream.model.photo.Photo;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-9-4
 * Time: 下午11:19
 */
public class SpringApplicationContextAndResourceTest extends TestCase {

    public void testFileSystemXmlApplicationContext() throws Exception {
        String location = "F:\\IdeaProjects\\DreamBlog\\src\\test\\resources\\testApplicationContext.xml";
        ApplicationContext context = new FileSystemXmlApplicationContext(location);
        Photo bean = (Photo) context.getBean("photo");
        assertEquals("First photo", bean.getName());
        assertNotNull(bean.getDescription());
        assertNull(bean.getPath());
    }

    public void testClassPathXmlApplicationContext() throws Exception {
        String location = "testApplicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(location);
        Photo bean = (Photo) context.getBean("photo");
        assertEquals("First photo", bean.getName());
        assertNotNull(bean.getDescription());
        assertNull(bean.getPath());
    }

    public void testXmlWebApplicationContext() throws Exception {
//        ApplicationContext context = new XmlWebApplicationContext();
//        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext();
    }

    public void testFileSystemResource() throws Exception {
        String location = "F:\\IdeaProjects\\DreamBlog\\src\\test\\resources\\testApplicationContext.xml";
        Resource resource = new FileSystemResource(location);
        assertTrue(resource.exists());
        assertEquals("testApplicationContext.xml", resource.getFilename());
    }


    public void testClassPathResource() throws Exception {
        String location = "testApplicationContext.xml";
        Resource resource = new ClassPathResource(location);
        assertTrue(resource.exists());
        assertEquals("testApplicationContext.xml", resource.getFilename());
    }

    public void testServletContextResource() throws Exception {
//        Resource resource = new ServletContextResource();
    }

    public void testPropertiesFile() throws Exception {
        String location = "datasource.properties";
        Resource resource = new ClassPathResource(location);
        Properties properties = PropertiesLoaderUtils.loadProperties(resource);
        String username = properties.getProperty("username");
        assertEquals("zhonggang", username);
    }
}