1. 創(chuàng )建項目
· 新建一個(gè)Java Project:SpringBeanFile,注意要導入用戶(hù)庫Spring。
· 這是完成后整個(gè)項目的結構(預覽一下):
2. 編寫(xiě)類(lèi)文件
· 下面開(kāi)始創(chuàng )建一個(gè)新類(lèi):BeanFile ;包名:javamxj.spring.beanfile
| BeanFile.java |
| package javamxj.spring.beanfile;
public class BeanFile {
private String beanFile = "多種方式加載Bean的配置文件";
public void setBeanFile(String beanFile) { this.beanFile = beanFile; }
public String getBeanFile() { return beanFile; } } |
· 新建Test.java,測試一下。
package javamxj.spring.beanfile;
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 Test {
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("bean1.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Resource resource = new InputStreamResource(is);
sayHello(resource);
/*
* 利用 ClassPathResource(String path)
* 要將bean.xml放在源文件夾(src)目錄下
*/
resource = new ClassPathResource("bean2.xml");
sayHello(resource);
/*
* 利用 FileSystemResource(String path)
* 要將bean.xml放在項目根目錄下
*/
resource = new FileSystemResource("bean3.xml");
sayHello(resource);
/*
* 利用 Properties
* 要將bean.properties放在類(lèi)路徑--源文件夾(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放在類(lèi)路徑--源文件夾(src)目錄下
*/
ApplicationContext appContext = new ClassPathXmlApplicationContext(
"bean4.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");
}
}
3. 配置文件
由上面的Test.java可知,這里一共需要四個(gè)XML文件和一個(gè)Properties文件,現在分別建立。
· bean1.xml放在項目根目錄下:
| bean1.xml |
| <?xml version="1.0" encoding="GBK"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <bean id="beanFile" class="javamxj.spring.beanfile.BeanFile"> <property name="beanFile"> <value>InputStreamResource(InputStream inputStream)</value> </property> </bean> </beans> |
bean2.xml、bean3.xml、bean4.xml與bean1.xml相似,僅僅需要替換一下<value>值即可。重要的注意文件的存放位置。這里只給出不同的代碼;
· bean2.xml放在源文件夾(src)目錄下:
| bean2.xml(部分) |
| <property name="beanFile"> <value>ClassPathResource(String path)</value> </property> |
· bean3.xml放在項目根目錄下:
| bean3.xml(部分) |
<property name="beanFile"> <value>FileSystemResource(String path)</value> </property> |
· bean4.xml放在源文件夾(src)目錄下:
| bean4.xml(部分) |
<property name="beanFile"> <value>ApplicationContext</value> </property> |
Spring也可以使用屬性文件來(lái)定義配置文件,如下:
· bean.properties放在源文件夾(src)目錄下:
| bean.properties |
| beanFile.class=javamxj.spring.beanfile.BeanFile beanFile.beanFile=properties |
4. 運行程序
右擊Test.java,運行程序,控制臺輸出如下:
多種方式加載Bean的配置文件
利用 InputStreamResource(InputStream inputStream) 加載 Bean.xml
利用 ClassPathResource(String path) 加載 Bean.xml
利用 FileSystemResource(String path) 加載 Bean.xml
利用 properties 加載 Bean.properties
利用 ApplicationContext 加載 Bean.xml
5. 小結
這篇文章主要談?wù)摿巳绾渭虞dSpring的配置文件,一般來(lái)說(shuō),就是BeanFactory和ApplicationContext。最常使用的、簡(jiǎn)單的BeanFactory實(shí)現是org.springframework.beans.factory.xml.XmlBeanFactory,其加載方式為:
BeanFactory factory = new XmlBeanFactory(Resource resource)
這里resource必須是xml格式。Resource包括: AbstractResource, ClassPathResource, FileSystemResource, InputStreamResource, ServletContextResource, UrlResource。這篇文章 談了常用的三種:ClassPathResource, FileSystemResource, InputStreamResource。
ApplicationContext包括了BeanFactory的所有功能,也要比BeanFactory強大的多(以后會(huì )詳細介紹的)。這里只簡(jiǎn)單的使用了ClassPathXmlApplicationContext加載了Bean配置文件。你可以將log4j.properties中的“Warn”改為“Debug”, 對比一下和ClassPathResource的輸出,
在Eclipse中,bean2.xml、bean4xml雖然都是放在源文件夾(src)目錄下,但實(shí)際上,是由已經(jīng)編譯好的Test.class從類(lèi)文件夾(這里是bin文件夾)中加載的。