[Apache Commons]Configuration
Language/JAVA 2014. 11. 11. 16:37http://commons.apache.org/configuration/
연구실에서 프로젝트를 할 때, DB 계정 정보 등의 설정 정보 등을 XML파일로 적어놓고 참조하려고 마음을 먹었습니다. 그래서 XML파일 읽는 코드를 한 20줄 짜고, 이 설정 정보를 java.util.Properties 에 저장하여 메모리에 유지하도록 static 하게 변수를 선언하는 등의 작업을 하였으나, 검색해보니 Apache Commons 프로젝트에 Configuration 이라는 컴포넌트가 있습니다.
구글에서 검색하니 http://twit88.com/blog/2007/09/18/xml-configuration-for-java-program/ 에도 간단한 예가 나와있습니다. 어쨌든, http://commons.apache.org/configuration/userguide/user_guide.html 를 보시면 이 컴포넌트의 사용방법이 자세하게 기술되어 있습니다. 그리고 User guide의 Hierarchical Properties 항목을 보면, XML파일을 이용하는 방법이 기술되어 있습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version="1.0" encoding="UTF-8"?><config> <database> <server> <type>PostgreSQL</type> <host>localhost</host> <port>5432</port> </server> <user> <name>coinus</name> <password>1124</password> </user> <database> <name>coinus</name> <charset>UTF8</charset> </database> </database> </config> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package kr.ac.uos.dmlab.config;import org.apache.commons.configuration.ConfigurationException;import org.apache.commons.configuration.XMLConfiguration;public class ConfigLoader { private static XMLConfiguration load(String file) { XMLConfiguration conf= null; try { conf= new XMLConfiguration("conf/conf.xml"); System.out.println(conf.getString("database.server.type")); } catch (ConfigurationException e) { e.printStackTrace(); } return conf; } public static void main(String[] args) { load("conf/conf.xml"); } } |
출처 - http://thebasis.tistory.com/94
'Language > JAVA' 카테고리의 다른 글
| How to get file resource from Maven src/test/resources/ folder in JUnit test? (0) | 2014.11.14 |
|---|---|
| Class.getResource vs. ClassLoader.getResource (0) | 2014.11.14 |
| pageContext 내장 객체 (0) | 2014.11.11 |
| http://www.docjar.com/ (0) | 2014.11.11 |
| ReentrantReadWriteLock (0) | 2014.04.16 |
