Spring讀取配置XML檔案分三步:
一.建立一個Java Bean:
packagespringdemo;public classHelloBean {privateString helloWorld;publicString getHelloWorld() {returnhelloWorld;
}public voidsetHelloWorld(String helloWorld) {this.helloWorld =helloWorld;
}
}
二.建構一個配置檔案bean_config.xml:
Hello!chb!
三.讀取配置檔案:
1.利用ClassPathXmlApplicationContext:
ApplicationContext context = new ClassPathXmlApplicationContext("bean_config.xml");//這種用法不夠靈活,不建議使用。
HelloBean helloBean = (HelloBean)context.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
ClassPathXmlApplicationContext實作了接口ApplicationContext,ApplicationContext實作了BeanFactory。其通過jdom進行XML配置檔案的讀取,并建構執行個體化Bean,放入容器内。
public interfaceBeanFactory {publicObject getBean(String id);
}
//實作類ClassPathXmlApplicationContextimportjava.lang.reflect.Method;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importorg.jdom.Document;importorg.jdom.Element;importorg.jdom.input.SAXBuilder;public class ClassPathXmlApplicationContext implementsBeanFactory {private Map beans = new HashMap();//(IOC:Inverse of Control/DI:Dependency Injection)
public ClassPathXmlApplicationContext() throwsException {
SAXBuilder sb=newSAXBuilder();
Document doc=sb.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); //構造文檔對象
Element root=doc.getRootElement(); //擷取根元素HD
List list=root.getChildren("bean");//取名字為disk的所有元素
for(int i=0;i
Element element=(Element)list.get(i);
String id=element.getAttributeValue("id");
String clazz=element.getAttributeValue("class");
Object o=Class.forName(clazz).newInstance();
System.out.println(id);
System.out.println(clazz);
beans.put(id, o);for(Element propertyElement : (List)element.getChildren("property")) {
String name= propertyElement.getAttributeValue("name"); //userDAO
String bean = propertyElement.getAttributeValue("bean"); //u
Object beanObject = beans.get(bean);//UserDAOImpl instance
String methodName= "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
System.out.println("method name = " +methodName);
Method m= o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);
m.invoke(o, beanObject);
}
}
}publicObject getBean(String id) {returnbeans.get(id);
}
}
BeanFactory是一個很根的接口,ApplicationContext和ClassPathXmlApplicationContext都實作了接口BeanFactory,是以也可以這麼寫:
ApplicationContext context = new ClassPathXmlApplicationContext("bean_config.xml");
HelloBean helloBean= (HelloBean)context.getBean("helloBean");
BeanFactory factory= new ClassPathXmlApplicationContext("bean_config.xml");
HelloBean helloBean= (HelloBean)factory.getBean("helloBean");
ClassPathXmlApplicationContext層級關系如下:
2.利用FileSystemResource讀取
Resource rs = new FileSystemResource("D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/bean_config.xml");
BeanFactory factory= newXmlBeanFactory(rs);
HelloBean helloBean= (HelloBean)factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
注意:利用FileSystemResource,則配置檔案必須放在project直接目錄下,或者寫明絕對路徑,否則就會抛出找不到檔案的異常。
Spring讀取properties配置檔案
介紹兩種技術:利用spring讀取properties 檔案和利用java.util.Properties讀取:
一.利用spring讀取properties 檔案
還利用上面的HelloBean.java檔案,構造如下bean_config.properties檔案:
helloBean.class=springdemo.HelloBean
helloBean.helloWorld=Hello!HelloWorld!
屬性檔案中的"helloBean"名稱即是Bean的别名設定,.class用于指定類來源。
然後利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader來讀取屬性檔案。
BeanDefinitionRegistry reg = newDefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader= newPropertiesBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("bean_config.properties"));
BeanFactory factory=(BeanFactory)reg;
HelloBean helloBean= (HelloBean)factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
二.利用java.util.Properties讀取屬性檔案
比如,我們構造一個ip_config.properties來儲存伺服器ip位址和端口,如:
ip=192.168.0.1
port=8080
我們可以用如下程式來獲得伺服器配置資訊:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ip_config.properties");
Properties p= newProperties();try{
p.load(inputStream);
}catch(IOException e1) {
e1.printStackTrace();
}
System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));
三.用接口類WebApplicationContext來取。
privateWebApplicationContext wac;
wac=WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
wac=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
JdbcTemplate jdbcTemplate= (JdbcTemplate)ctx.getBean("jdbcTemplate");
其中,jdbcTemplate為spring配置檔案中的一個bean的id值。
這種用法比較靈活,spring配置檔案在web中配置啟動後,該類會自動去找對應的bean,而不用再去指定配置檔案的具體位置。