一、使用org.apache.commons.configuration
需要使用的是jar包:commons-collections-3.2.1.jar、commons-configuration-1.10.jar、commons-lang-2.6.jar和commons-logging-1.2.jar。
可以讀取的配置文件:xml和properties
1、讀取xml文件
使用到的config.xml內容如下:
- <Account type="by0003">
- <code>100001</code>
- <pass>123</pass>
- <name>李四</name>
- <money>1000000.00</money>
- </Account>
- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.ConfigurationException;
- import org.apache.commons.configuration.XMLConfiguration;
- public class xmlLoaderTest {
- public static void main(String[] args) throws ConfigurationException{
- Configuration config = new XMLConfiguration("com/styspace/config.xml");
- String name = config.getString("Account.name");
- System.out.println("name:" + name);
- }
- }
需要注意的是config.getString(“Account.name”)中的參數是Account.name,這個參數是XPath格式的,而且不能包含xml中的根元素。
使用到的config.properties文件內容如下:
- timout=15.52
- interactive=true
- color=red
- speed=50
- name=Default User
第一種方式:
- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.ConfigurationException;
- import org.apache.commons.configuration.PropertiesConfiguration;
- public class peropertiesLoaderTest {
- public static void main(String[] args) throws ConfigurationException{
- Configuration config = new PropertiesConfiguration("com/styspace/config.properties");
- String name = config.getString("name");
- System.out.println("name:" + name);
- }
- }
- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.ConfigurationException;
- import org.apache.commons.configuration.PropertiesConfiguration;
- public class peropertiesLoaderTest {
- public static void main(String[] args) throws ConfigurationException{
- Configuration config = new PropertiesConfiguration("com/styspace/config.properties");
- String name = config.getString("name");
- System.out.println("name:" + name);
- }
- }
第二種方式: 使用java.util.Properties讀取
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- public class PropertiesTest {
- public static void main(String[] args){
- PropertiesTest pt = new PropertiesTest();
- try {
- pt.getProperties();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private void getProperties() throws IOException {
- InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("com/styspace/config.properties");
- System.out.println("begin!!!");
- Properties properties = new Properties();
- try{
- properties.load(inputStream);
- }catch (IOException ioE){
- ioE.printStackTrace();
- }finally{
- inputStream.close();
- }
- System.out.println("name:"+properties.getProperty("name"));
- }
- }
ClassLoader()和URLClassLoader()區別:ClassLoader()只能查找src目錄下的文件,而URLClassLoader()則能查找任意目錄下的文件。
三、spring中配置文件的讀取
1、ClassPathXmlApplicationContext:從類路徑中加載。
2、FileSystemXmlApplicationContext:從文件系統加載。
3、XmlWebApplicationContext:從web系統中加載
- BeanFactory factory = null; //聲明
- ClassPathResource resource = new ClassPathResource("spring.xml");//類路徑
- factory= new XmlBeanFactory(resource);
- FileSystemResource fileSystemResource = new FileSystemResource("D:\\Ncode\\mcode\\sday02\\src\\spring.xml");//文件路徑
- factory= new XmlBeanFactory(fileSystemResource);
- //XmlBeanFactory(參數可以是resource或者fileSystemResource等
- //但是不能是 res 原因可以查看:文檔Part III. Core Technologies 6. Resources
- //中6.2 The Resource interface 有關isOpen方法的說明);
- //InputStreamResource res = new InputStreamResource(new FileInputStream("D:\\Ncode\\mcode\\sday02\\src\\spring.xml"));//系統路徑
- HelloService helloService = factory.getBean("helloServiceImpl", HelloServiceImpl.class);
- helloService.sayHello();
上下文更加高級:提供文本信息解析工具,包括對國際化支持;提供載入文件資源的通用方法,如圖片;可以向註冊爲監聽器的bean發送事件。在很少的情況下,使用BeanFactory.
- //從文件系統
- ApplicationContext context = new FileSystemXmlApplicationContext("file:D:\\Ncode\\mcode\\sday02\\src\\spring.xml");
- //從類路徑
- ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
- HelloService helloService = context.getBean("helloServiceImpl", HelloServiceImpl.class);
- helloService.sayHello();
3.1、使用XmlWebApplicationContext
- XmlWebApplicationContext context = new XmlWebApplicationContext();
- //默認的路徑/WEB-INF/applicationContext.xml
- //applicationContext.xml文件名稱 可以任意起
- //重新設置路徑
- //context.setConfigLocations(new String[] {"/WEB-INF/classes/applicationContext.xml"});
- //設置ServletContext上下下文爲web應用程序的上下文
- context.setServletContext(getServletContext());
- //刷新
- context.refresh();
- //根據id名稱獲取
- HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
- //執行helloDao對象的方法
- helloDao.sayHello();
- //直接採用getWebApplicationContext(getServletContext()) 獲取context對象
- WebApplicationContext context=
- WebApplicationContextUtils.getWebApplicationContext(getServletContext());
- //context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
- System.out.println(context);
- HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
- helloDao.sayHello()
1、當採用getWebApplicationContext(getServletContext())獲取context對象的時候,輸出的context對象爲null 所以在使用
context.getBean("helloDaoImpl", HelloDaoImpl.class);會出現空指針的異常
2、當採用getRequiredWebApplicationContext(getServletContext());獲取context對象的時候 會出現如下bug
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered
轉載自: http://blog.csdn.net/stypace/article/details/38414871