惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

博客园 - 信息时代的生存哲学

搜索引擎谁主沉浮 一个有意思的搜索引擎Clusty(转) 是男人,一定要知道!(转) 猎狗与兔子 小狗的幸福 谨以此篇献给即将奔三十的人们 jakarta commons log4j 学习笔记 女孩对男孩说 等待是一种美丽 软件之战 我有话要说 老婆对老公说:我对你的要求不高吧!? [CI柳叶刀]谁诱奸了企业竞争情报?(转载) 毕业那天我们一起失恋 一些有待阅读的文章 推荐人力资源的文章 成功就是如此的简单 管理者执行力的素质要求 管理团队的“满汉全席”
jakarta commons configuration 学习笔记
信息时代的生存哲学 · 2004-11-18 · via 博客园 - 信息时代的生存哲学

jakarta commons configuration 学习笔记

关键字:jakarta, commons, configuration

Runtime Dependencies

运行时需要的一些组件包:

依存关系:

  1. Core
    • commons collections
    • commons lang
  2. ConfigurationFactory
    • commons logging
    • commons digester
  3. DatabaseConfiguration
    • commons logging
  4. XMLConfiguration, HierarchicalXMLConfiguraion
    • xml apis or xerces
  5. JNDIConfiguration
    • commons logging
  6. ConfigurationDynaBean
    • commons beanutils
    • commons logging

用ConfigurationFactory 进行配置

portal/
|--src/
|--WEB-INF/
|----config.xml
|----properties/
|------usergui.properties
|----classes/
|------ConfigurationFactoryTest.class

ConfigurationFactory 提供了两种方式用于指定配置文件的位置:

  • setConfigurationURL(URL)
  • setConfigurationFileName(String)

使用factory.setConfigurationURL(URL) 方法指定配置文件

ConfigurationFactory factory = new ConfigurationFactory();
URL url = new File("WEB-INF/config.xml").toURL();
factory.setConfigurationURL(url);
Configuration config = factory.getConfiguration();
System.out.println(config.getString("colors.background"));

此时config.xml 中其他配置文件的声明要用相对于config.xml 的路径

<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
  <properties fileName="properties/usergui.properties"/>
  <!--或 fileName="./properties/usergui.properties"-->
</configuration>

使用factory.setConfigurationFileName(String) 方法指定配置文件

ConfigurationFactory factory = new ConfigurationFactory();
factory.setConfigurationFileName("WEB-INF/config.xml");
Configuration config = factory.getConfiguration();
System.out.println(config.getString("colors.background"));

此时config.xml 中其他配置文件的声明要用相对于项目的根目录的路径:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
  <properties fileName="WEB-INF/properties/usergui.properties"/>
</configuration>

!注:如果配置文件的路径填写不正确的话就会遇到如下两个Exception

  • org.apache.commons.configuration.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:135) - Exception caught opening stream to URL 表明找不到config.xml
  • org.apache.commons.digester.Digester.endElement(Digester.java:1069) - End event threw exception java.lang.reflect.InvocationTargetException 表明找不到config.xml 中声明的其他配置文件