几种加载Spring的配置文件的方式

package com.liu.test;

public class BeanFile {
 private String beanFile = "多种方式加载Bean的配置文件";

 public void setBeanFile(String beanFile) {
 this.beanFile = beanFile;
 }

 public String getBeanFile() {
 return beanFile;
 }

}

package com.liu.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;

public class MainBeanFile {
 public static void main(String[] args) {

 // 直接调用HelloBean
 BeanFile bf = new BeanFile();
 System.out.println(bf.getBeanFile());

 /**
 * 利用XmlBeanFactory(Resource resource) 这里Resource必须是xml格式
 * Resource包括:AbstractResource, ClassPathResource, FileSystemResource,
 * InputStreamResource, ServletContextResource, UrlResource
 */

 /*
 * 利用 InputStreamResource(InputStream inputStream) 要将bean.xml放在项目根目录下,目录一定要写完整
 */
 InputStream is = null;
 try {
 is = new FileInputStream("c:/workspace/spring/WebRoot/WEB-INF/InputStreamResource.xml");
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 }
 Resource resource = new InputStreamResource(is);
 sayHello(resource);

 /*
 * 利用 ClassPathResource(String path) 要将bean.xml放在源文件夹(src)目录下
 */
 resource = new ClassPathResource("ClassPathResource.xml");
 sayHello(resource);

 /*
 * 利用 FileSystemResource(String path) 要将bean.xml放在项目根目录下,目录一定要写完整
 * c:/workspace/spring/src/com/liu/test/FileSystemResource.xml
 */
 resource = new FileSystemResource("c:/workspace/spring/src/com/liu/test/FileSystemResource.xml");
 sayHello(resource);

 /*
 * 利用 Properties 要将bean.properties放在类路径--源文件夹(src)目录下
 */
 BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
 PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);
 reader.loadBeanDefinitions(new ClassPathResource("bean.properties"));
 BeanFactory factory = (BeanFactory) reg;
 bf = (BeanFile) factory.getBean("beanFile");
 System.out.println("利用" + bf.getBeanFile() + " 加载 Bean.properties");

 /*
 * 利用 ApplicationContext 要将bean.xml放在类路径--源文件夹(src)目录下
 */
 ApplicationContext appContext = new ClassPathXmlApplicationContext(
 "/ApplicationContext.xml");
 bf = (BeanFile) appContext.getBean("beanFile");
 System.out.println("利用" + bf.getBeanFile() + " 加载 Bean.xml");

 }

 public static void sayHello(Resource resource) {
 BeanFactory factory = new XmlBeanFactory(resource);
 BeanFile bf = (BeanFile) factory.getBean("beanFile");
 System.out.println("利用 " + bf.getBeanFile() + " 加载 Bean.xml");
 }


}
 

InputStreamResource.xml

"
http://www.springframework.org/dtd/spring-beans.dtd">


   
       
            InputStreamResource(InputStream inputStream)
       

   

ClassPathResource.xml

"
http://www.springframework.org/dtd/spring-beans.dtd">


 
  
   ClassPathResource(String path)
  

 


FileSystemResource.xml


"
http://www.springframework.org/dtd/spring-beans.dtd">


 
  
   FileSystemResource(String path)
  

 


ApplicationContext.xml

"
http://www.springframework.org/dtd/spring-beans.dtd">


 
  
   ApplicationContext
  

 


bean.properties(Spring也可以使用属性文件来定义配置文件,bean.properties放在源文件夹(src)目录下)
beanFile.class=com.liu.test.BeanFile
beanFile.beanFile=properties

输出结果:(也跟log4j的配置文件有关)
多种方式加载Bean的配置文件
利用 InputStreamResource(InputStream inputStream) 加载 Bean.xml
利用 ClassPathResource(String path) 加载 Bean.xml
利用 FileSystemResource(String path) 加载 Bean.xml
 利用properties 加载 Bean.properties
利用ApplicationContext 加载 Bean.xml

 

你可能感兴趣的:(经验技巧之Spring)