1. 本帖内容
在用java作开发时,一般会选择使用xml文件作为配置文件,故而通过java工程读取配置文件则是必须掌握的知识;传统的C/C++通过File文件的方式或者说通过流的方式进行读写,过分的浪费内存与效率,且读写及其不方便,不适用于java的灵活配置,而java的注解方式成功的解决了该问题。
作者在从事相关的开发中学习了一下注解的方式,并成功开发了项目;虽然现在网上关于此知识已经铺天盖地,而我仍旧希望将自己的经验与大家分享。
2. 选择要读取的配置文件
下面的配置文件虽说是作者编纂,但仍可代表一般的配置,读者姑妄观之,见下
<?xml version="1.0" encoding="UTF-8"?>
<cham formatversion="1.14">
<servers>
<centers>
<center id="1" name="center1">
<main>
<iphost>127.0.0.1</iphost>
<port>8443</port>
<byte-sequence>BigEndian</byte-sequence>
</main>
<preparation>
<iphost>10.10.10.1</iphost>
<port>8080</port>
<byte-sequence>BigEndian</byte-sequence>
</preparation>
<argument-ref>ccsArg</argument-ref>
</center>
</centers>
</servers>
</cham>
上文中的配置文件格式是标准的配置文件格式,配置文件中其实只包含了一个数组,记<centers></centers>节点,而我们此次的主要目的也是讲解该节点。
3. 基本注解讲解
从严格意义上来讲,XML文件常用的注解共有四个:
1) @XmlRootElement(name="") 用于注解根节点,即XML的起点
2)@XmlAttribute(name="") 用于注释attribute,例如<center id="1" name="center1"/>中的id和name两个属性
2)@XmlElement(name="") 最常用的注释方式,用于注释节点,例如<port>8080</port>
4)@XmlTransient 用于放置在get方法上,放置报错。
4. java相应文件的写作
分析上述的cham.xml配置文件,很容易产清楚它的结构,它有以下几个部分组成
1. cham.xml文件由一个<servers/>节点组成
2. servers节点由<centers/>节点组成,而<centers/>节点是个数组,用于可以在其中配置多个<center/>,而本帖中只包含了一个元素。
3. <center/>节点由两部分组成,即<main/><preparation/> 和 <argument-ref/>节点。
我们的分析方式采用的由内及外的读取方式
4.1 最基本的<main/>单元
查看上述cham.xml文件,则可以发现<main/>和<preparation/>是最基本的配置单元,没有比它更小的单元了,我们不妨先从它开始注解
我们可以建立一个名字叫做ServerPoint.java的文件来表示该节点
package com.china.domain.config;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
public class ServerPoint implements Serializable {
private static final long serialVersionUID = 3623939598540009923L;
@XmlElement(name="iphost")
private String iphost;
@XmlElement(name="port")
private int port;
@XmlElement(name="byte-sequence")
private String byteSequence;
@XmlTransient
public String getIphost() {
return iphost;
}
public void setIphost(String iphost) {
this.iphost = iphost;
}
@XmlTransient
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
@XmlTransient
public String getByteSequence() {
return byteSequence;
}
public void setByteSequence(String byteSequence) {
this.byteSequence = byteSequence;
}
@Override
public String toString() {
return " [iphost=" + iphost + ", port=" + port + ", byteSequence=" + byteSequence + "]";
}
}
读者很容易看出来,该文件和之前<main/>或者<preparation/>节点的内容正好一一对应。所涉及的注释有两个,即@XmlElement 和 @XmlTransient,它们的功能此处不再赘述。
4.2 <center/>节点讲解
从之前的分析可以知道,<center/>节点共包含两个部分或者说三个部分,即<main/><preparation/>和 <argument-ref/>节点。从内容上看,需要用到的注释有三个,即@XmlElement 、@XmlAttribute 、@XmlTransient
我们可以用一个Center.java 文件来表示该节点,如下所示:
package com.china.domain.config;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
public class Center implements Serializable {
private static final long serialVersionUID = 4568925940918840647L;
@XmlAttribute(name="id")
private int id;
@XmlAttribute(name="name")
private String name;
@XmlElement(name="main")
private ServerPoint serverPoint;
@XmlElement(name="preparation")
private ServerPoint back;
@XmlElement(name="argument-ref")
private String argumentRef;
@XmlTransient
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlTransient
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlTransient
public ServerPoint getServerPoint() {
return serverPoint;
}
public void setServerPoint(ServerPoint serverPoint) {
this.serverPoint = serverPoint;
}
@XmlTransient
public ServerPoint getBack() {
return back;
}
public void setBack(ServerPoint back) {
this.back = back;
}
@XmlTransient
public String getArgumentRef() {
return argumentRef;
}
public void setArgumentRef(String argumentRef) {
this.argumentRef = argumentRef;
}
@Override
public String toString() {
return "Center [id=" + id + ", name=" + name + ", serverPoint=" + serverPoint + ", back=" + back
+ ", argumentRef=" + argumentRef + "]";
}
}
从上文中可以看出,用@XmlAttribute可以注释<center id="1" name="center1"/>的场景,由于代码和注释一一匹配,此处不再多言。
4.3 <servers/>节点及数组的注释方式
前面分析已知:<servers/>节点下是一个包含了多个(本例中只有一个)<center/>的<centers/>数组,则我们创建和<servers/>节点相对应的java文件时要考虑数组的因素。一般来讲,我们是通过@XmlElementWrapper 来注释数组,用法如下
package com.china.domain.config;
import java.io.Serializable;
import java.util.Arrays;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlTransient;
public class Servers implements Serializable {
private static final long serialVersionUID = 8836963744878452510L;
@XmlElementWrapper(name="centers")//用于注释<centers/>数组,表示这是个数组
@XmlElement(name="center")//用于注释数组的子元素
private Center[] centers;
@XmlTransient
public Center[] getCenters() {
return centers;
}
public void setCenters(Center[] centers) {
this.centers = centers;
}
@Override
public String toString() {
return " [centers=" + Arrays.toString(centers) + "]";
}
}
4.4 注释根节点
XML注释中有 @XmlRootElement(name="cham")注释根节点,其中 cham是xml文件根节点的名称,在本例中的name,即<cham formatversion="1.14"/>中的cham
用@XmlAccessorType(XmlAccessType.FIELD)注解表示当前所注解内容的类型,除FIELD以外还有其他数种,但最长用的是FIELD,至于其他的含义,读者请搜索相关资料。
我们可以创建一个名字叫做 SystemConfig.java的文件来存放该内容:
package com.china.domain.config;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="cham")
public class SystemConfig implements Serializable {
private static final long serialVersionUID = -7667130896271777648L;
@XmlAttribute(name="formatversion")
private String formatVersion;
@XmlElement(name="servers")
private Servers servers;
@XmlTransient
public String getFormatVersion() {
return formatVersion;
}
public void setFormatVersion(String formatVersion) {
this.formatVersion = formatVersion;
}
@XmlTransient
public Servers getServers() {
return servers;
}
public void setServers(Servers servers) {
this.servers = servers;
}
@Override
public String toString() {
return "SystemConfig {formatVersion=" + formatVersion + ", servers=" + servers + "}";
}
}
至此,注解部分已经完成。
5. XML文件的位置
一般来讲,无论C/C++还是java,都会将配置文件放置在工程的某个目录之下。由于作者开发中使用到了tomcat容器,根据习惯,故将配置文件放置在tomacat下的/conf/目录下
java提供 getProperty 方法获取/conf/上层路径,即我们可以通过 String configPath = System.getProperty("catalina.base"); 方式来获取到/conf/的路径,其中catalina.base即为/conf/上层路径,读者可以查看相关tomcat文档中的定义
6. 分隔符的讲解
该部分是作者在开发过程中碰到的一个小陷阱,特拿出来和大家分享;作者在获取到路径后却始终不能读取文件,后来发现是缺少分隔符
我们可以通过如下代码获取/conf/路径并添加分隔符
if(!this.configPath.endsWith(File.separator)) {
this.configPath = this.configPath + File.separatorChar + "conf" + File.separatorChar;
}else {
this.configPath = this.configPath + "conf" + File.separatorChar;
}
如果我们再创建一个 configFile变量来存储cham.xml文件,则configFile=configPath+"cham.xml";
7.java中读取xml注解的方式
这段代码比较固定,读者可以再网上任意搜到
package com.china.domain.comm.basicread;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import org.xml.sax.InputSource;
import com.china.domain.config.SystemConfig;
import javax.xml.bind.Unmarshaller;
public class XmlReaders {
private String configPath;
private String configFile;
private SystemConfig config;
public XmlReaders(String configPath) {
super();
this.configPath = configPath;
initConfigPath();
initSystemConfig();
}
private void initConfigPath() {
if(null == this.configPath) {
System.out.println("configPath is null");
return;
}
if(!this.configPath.endsWith(File.separator)) {
this.configPath = this.configPath + File.separatorChar + "conf" + File.separatorChar;
}else {
this.configPath = this.configPath + "conf" + File.separatorChar;
}
this.configFile = this.configPath + "cham.xml";
}
private void initSystemConfig() {
if(null == this.configFile) {
System.out.println("configFile is null");
return;
}
try {
JAXBContext context = JAXBContext.newInstance(SystemConfig.class);//首先创建SystemConfig.java的模型
Unmarshaller umar = context.createUnmarshaller();
File file = new File(this.configFile);
InputStream inputStream = new FileInputStream(file);//通过输入流读取配置文件
InputSource source = new InputSource(inputStream);
source.setEncoding("UTF-8");//设置读取字节的方式
this.config = (SystemConfig)umar.unmarshal(inputStream);
}catch(Exception e) {
System.out.println("Exceptions : " + e.toString());
}
}
public String getConfigPath() {
return configPath;
}
public void setConfigPath(String configPath) {
this.configPath = configPath;
}
public String getConfigFile() {
return configFile;
}
public void setConfigFile(String configFile) {
this.configFile = configFile;
}
public SystemConfig getConfig() {
return config;
}
public void setConfig(SystemConfig config) {
this.config = config;
}
}
只要调用XmlReaders.java这个类的构造函数,则可以成功的将配置文件读取到SystemConfig.java类中
8.结果展示
SystemConfig = SystemConfig {formatVersion=1.14, servers= [centers=[Center [id=1, name=center1, serverPoint= [iphost=127.0.0.1, port=8443, byteSequence=BigEndian], back= [iphost=10.10.10.1, port=8080, byteSequence=BigEndian], argumentRef=ccsArg]]]}