在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据笔者工作中用到的读取配置文件的方法小小总结一下,主要叙述的是spring读取配置文件的方法。

一.读取xml配置文件

(一)新建一个java bean(HelloBean.java)

java 代码:

public class HelloBean {

    private String helloWorld;

    public String getHelloWorld() {
        return helloWorld;
    }

    public void setHelloWorld(String helloWorld) {
        this.helloWorld = helloWorld;
    }
}


(二)构造一个配置文件(beanConfig.xml)


xml 代码:


<!---->xml version="1.0" encoding="UTF-8"?>  
<!---->>  
<beans>  
 <bean id="helloBean" class="chb.demo.vo.HelloBean">  
  <property name="helloWorld">  
   <value>Hello!chb!value>  
  property>  
 bean>  
beans>

(三)读取xml文件

1.利用ClassPathXmlApplicationContext
java 代码:

ApplicationContext context = new ClassPathXmlApplicationContext("beanConfig.xml");   
HelloBean helloBean = (HelloBean)context.getBean("helloBean");   
System.out.println(helloBean.getHelloWorld());

2.利用FileSystemResource读取


java 代码:

Resource rs = new FileSystemResource("D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/beanConfig.xml");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean helloBean = (HelloBean) factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());

值得注意的是:利用FileSystemResource,则配置文件必须放在project直接目录下,或者写明绝对路径,否则就会抛出找不到文件的异常


二.读取properties配置文件

这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取


(一)利用spring读取properties 文件


我们还利用上面的HelloBean.java文件,构造如下beanConfig.properties文件:


properties 代码:


helloBean.class=chb.demo.vo.HelloBean   
helloBean.helloWorld=Hello!chb!

属性文件中的"helloBean"名称即是Bean的别名设定,.class用于指定类来源。


然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件


java 代码:

BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("beanConfig.properties"));
BeanFactory factory = (BeanFactory) reg;
HelloBean helloBean = (HelloBean) factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());


(二)利用java.util.Properties读取属性文件


比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:


properties 代码


ip=192.168.0.1    

 port=8080


则,我们可以用如下程序来获得服务器配置信息:


java 代码:

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties");
Properties p = new Properties();
try {
    p.load(inputStream);
} catch (IOException e1) {
    e1.printStackTrace();
}
System.out.println("ip:" + p.getProperty("ip") + ",port:" + p.getProperty("port"));

(三)利用Spring注解读取属性文件
第一步:在applicationContext.xml配置

<!-- 加载属性配置文件 -->
<context:property-placeholder location="classpath:config/config.properties"/>

第二步:建立配置文件内容


report.page.image=E\:/resource/template/image/cover/logo/

第三步:在Controller中使用注解获得配置项内容


/** 报告封面图片路径 */
@Value("${report.page.image}")
private String reportPageImage;

public String getReportPageImage() {
    return reportPageImage;
}

public void setReportPageImage(String reportPageImage) {
    this.reportPageImage = reportPageImage;
}

第四步:直接使用reportPageImage这个私用变量获取配置文件的内容


/** 公共属性配置类 */
@Resource
public PublicConfig publicConfig;
String imagePath = publicConfig.getReportPageImage();



三.关于JAVA读取配置文件的路径问题

在软件开发中经常遇到读取配置文件,以及文件定位问题。今天做个总结。


1、在java project中的配置文件读取

在java project中的配置文件读取java project项目的目录结构只有两个src和bin。


src中存放的是源文件


bin中存放的是*.class的字节码文件。


源文件进过编译就放进bin目录中,其中也包括没有编译的配置文件。我们需要的是对bin中的配置文件进行读取,因为项目最终打包成*.jar的就是bin目录下的文件。采用相对路劲进行读取的时候,需要确定此时相对的是哪一个基路径。其实这个基路径就是java.io默认定位到的当前用户目录("user.dir")(即工程根目录)。相对于这个路径在定位我们的配置文件。


如下所示:


File  file=new File("test/login.conf");



JVM就可以据"user.dir"与"test/login.conf" 得到完整的路径(即绝对路径)"D:\DecisionTree\test\login.conf",从而找到login.conf文件。


通常我们使用“类名”.class.getClassLoader().getResource("login.conf").openStream()来加载classes目录下的配置文件。得到文件的输入流。


“类名”.class.getClassLoader().getResource("")定位到的绝对路径是WEB-INF\classes目录下。getResource(String filepath)。参数filepath是classes目录下的相对路径。


2、在java web project中的配置文件读取

如果我们把配置文件定义在了classpath路径下。也就是WEB-INF\classes路径下,那么就可以像读取java project中的配置文件那样读取了。


如果我们把配置文件定义在了WEB-INF的直接目录下。而不是classes目录下,那么是classpath之外的路径了,通过上面的那种方式是读取不到了。


这是在应用环境下,要用到ServletContext


ServletActionContext.getServletContext().getResource("/")可以定位到当前web应用目录 如:jndi:/server/TEST/  TEST为当前的web应用系统名。getResource(String filepath) 注意其中filepath必须以“/”开始。


ServletActionContext.getServletContext().getResourceAsStream("/WEB-INF/config/login.conf");就可以得到/WEB-INF/config/login.conf文件的输入流了。