当前位置: 移动技术网 > IT编程>开发语言>Java > 详解五种方式让你在java中读取properties文件内容不再是难题

详解五种方式让你在java中读取properties文件内容不再是难题

2019年07月22日  | 移动技术网IT编程  | 我要评论

一、背景

最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题。就借此机会把spring+springmvc+mybatis整合开发的项目中通过java程序读取properties文件内容的方式进行了梳理和分析,先和大家共享。

二、项目环境介绍

  • spring 4.2.6.release
  • springmvc 4.2.6.release
  • mybatis 3.2.8
  • maven 3.3.9
  • jdk 1.7
  • idea 15.04

三、五种实现方式

方式1.通过context:property-placeholder加载配置文件jdbc.properties中的内容

<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>

上面的配置和下面配置等价,是对下面配置的简化

<bean id="propertyconfigurer" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">
  <property name="ignoreunresolvableplaceholders" value="true"/>
  <property name="locations">
   <list>
     <value>classpath:jdbc.properties</value>
   </list>
  </property>
</bean>

注意:这种方式下,如果你在spring-mvc.xml文件中有如下配置,则一定不能缺少下面的红色部分,关于它的作用以及原理.

<!-- 配置组件扫描,springmvc容器中只扫描controller注解 -->
<context:component-scan base-package="com.hafiz.www" use-default-filters="false">
  <context:include-filter type="annotation" expression="org.springframework.stereotype.controller"/>
</context:component-scan>

方式2.使用注解的方式注入,主要用在java代码中使用注解注入properties文件中相应的value值

<bean id="prop" class="org.springframework.beans.factory.config.propertiesfactorybean">
  <!-- 这里是propertiesfactorybean类,它也有个locations属性,也是接收一个数组,跟上面一样 -->
  <property name="locations">
    <array>
     <value>classpath:jdbc.properties</value>
    </array>
  </property>
</bean>

方式3.使用util:properties标签进行暴露properties文件中的内容

<util:properties id="propertiesreader" location="classpath:jdbc.properties"/>

注意:使用上面这行配置,需要在spring-dao.xml文件的头部声明以下红色的部分

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemalocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/util 
     http://www.springframework.org/schema/util/spring-util.xsd">

方式4.通过propertyplaceholderconfigurer在加载上下文的时候暴露properties到自定义子类的属性中以供程序中使用

<bean id="propertyconfigurer" class="com.hafiz.www.util.propertyconfigurer">
  <property name="ignoreunresolvableplaceholders" value="true"/>
  <property name="ignoreresourcenotfound" value="true"/>
  <property name="locations">
    <list>
     <value>classpath:jdbc.properties</value>
    </list>
  </property>
</bean>

自定义类propertyconfigurer的声明如下:

package com.hafiz.www.util;

import org.springframework.beans.beansexception;
import org.springframework.beans.factory.config.configurablelistablebeanfactory;
import org.springframework.beans.factory.config.propertyplaceholderconfigurer;

import java.util.properties;

/**
 * desc:properties配置文件读取类
 * created by hafiz.zhang on 2016/9/14.
 */
public class propertyconfigurer extends propertyplaceholderconfigurer {

  private properties props;    // 存取properties配置文件key-value结果

  @override
  protected void processproperties(configurablelistablebeanfactory beanfactorytoprocess, properties props)
              throws beansexception {
    super.processproperties(beanfactorytoprocess, props);
    this.props = props;
  }

  public string getproperty(string key){
    return this.props.getproperty(key);
  }

  public string getproperty(string key, string defaultvalue) {
    return this.props.getproperty(key, defaultvalue);
  }

  public object setproperty(string key, string value) {
    return this.props.setproperty(key, value);
  }
}

使用方式:在需要使用的类中使用@autowired注解注入即可。

方式5.自定义工具类propertyutil,并在该类的static静态代码块中读取properties文件内容保存在static属性中以供别的程序使用

package com.hafiz.www.util;

import org.slf4j.logger;
import org.slf4j.loggerfactory;

import java.io.*;
import java.util.properties;

/**
 * desc:properties文件获取工具类
 * created by hafiz.zhang on 2016/9/15.
 */
public class propertyutil {
  private static final logger logger = loggerfactory.getlogger(propertyutil.class);
  private static properties props;
  static{
    loadprops();
  }

  synchronized static private void loadprops(){
    logger.info("开始加载properties文件内容.......");
    props = new properties();
    inputstream in = null;
    try {
       <!--第一种,通过类加载器进行获取properties文件流-->
      in = propertyutil.class.getclassloader().getresourceasstream("jdbc.properties");
       <!--第二种,通过类进行获取properties文件流-->
      //in = propertyutil.class.getresourceasstream("/jdbc.properties");
      props.load(in);
    } catch (filenotfoundexception e) {
      logger.error("jdbc.properties文件未找到");
    } catch (ioexception e) {
      logger.error("出现ioexception");
    } finally {
      try {
        if(null != in) {
          in.close();
        }
      } catch (ioexception e) {
        logger.error("jdbc.properties文件流关闭出现异常");
      }
    }
    logger.info("加载properties文件内容完成...........");
    logger.info("properties文件内容:" + props);
  }

  public static string getproperty(string key){
    if(null == props) {
      loadprops();
    }
    return props.getproperty(key);
  }

  public static string getproperty(string key, string defaultvalue) {
    if(null == props) {
      loadprops();
    }
    return props.getproperty(key, defaultvalue);
  }
}

说明:这样的话,在该类被加载的时候,它就会自动读取指定位置的配置文件内容并保存到静态属性中,高效且方便,一次加载,可多次使用。

四、注意事项及建议

以上五种方式,前三种方式比较死板,而且如果你想在带有@controller注解的bean中使用,你需要在springmvc的配置文件spring-mvc.xml中进行声明,如果你想在带有@service、@respository等非@controller注解的bean中进行使用,你需要在spring的配置文件中spring.xml中进行声明。

我个人比较建议第四种和第五种配置方式,第五种为最好,它连工具类对象都不需要注入,直接调用静态方法进行获取,而且只一次加载,效率也高。而且前三种方式都不是很灵活,需要修改@value的键值。

五、测试验证是否可用

1.首先我们创建propertiesservice

package com.hafiz.www.service;

/**
 * desc:java程序获取properties文件内容的service
 * created by hafiz.zhang on 2016/9/16.
 */
public interface propertiesservice {

  /**
   * 第一种实现方式获取properties文件中指定key的value
   *
   * @return
   */
  string getproperybyfirstway();

  /**
   * 第二种实现方式获取properties文件中指定key的value
   *
   * @return
   */
  string getproperybysecondway();

  /**
   * 第三种实现方式获取properties文件中指定key的value
   *
   * @return
   */
  string getproperybythirdway();

  /**
   * 第四种实现方式获取properties文件中指定key的value
   *
   * @param key
   *
   * @return
   */
  string getproperybyfourthway(string key);

  /**
   * 第四种实现方式获取properties文件中指定key的value
   *
   * @param key
   *
   * @param defaultvalue
   *
   * @return
   */
  string getproperybyfourthway(string key, string defaultvalue);

  /**
   * 第五种实现方式获取properties文件中指定key的value
   *
   * @param key
   *
   * @return
   */
  string getproperybyfifthway(string key);

  /**
   * 第五种实现方式获取properties文件中指定key的value
   *
   * @param key
   *
   * @param defaultvalue
   *
   * @return
   */
  string getproperybyfifthway(string key, string defaultvalue);
}

2.创建实现类propertiesserviceimpl

package com.hafiz.www.service.impl;

import com.hafiz.www.service.propertiesservice;
import com.hafiz.www.util.propertyconfigurer;
import com.hafiz.www.util.propertyutil;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.service;

/**
 * desc:java程序获取properties文件内容的service的实现类
 * created by hafiz.zhang on 2016/9/16.
 */
@service
public class propertiesserviceimpl implements propertiesservice {

  @value("${test}")
  private string testdatabyfirst;

  @value("#{prop.test}")
  private string testdatabysecond;

  @value("#{propertiesreader[test]}")
  private string testdatabythird;

  @autowired
  private propertyconfigurer pc;

  @override
  public string getproperybyfirstway() {
    return testdatabyfirst;
  }

  @override
  public string getproperybysecondway() {
    return testdatabysecond;
  }

  @override
  public string getproperybythirdway() {
    return testdatabythird;
  }

  @override
  public string getproperybyfourthway(string key) {
    return pc.getproperty(key);
  }

  @override
  public string getproperybyfourthway(string key, string defaultvalue) {
    return pc.getproperty(key, defaultvalue);
  }

  @override
  public string getproperybyfifthway(string key) {
    return propertyutil.getpropery(key);
  }

  @override
  public string getproperybyfifthway(string key, string defaultvalue) {
    return propertyutil.getproperty(key, defaultvalue);
  }
}

3.控制器类propertycontroller

package com.hafiz.www.controller;

import com.hafiz.www.service.propertiesservice;
import com.hafiz.www.util.propertyutil;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.responsebody;

/**
 * desc:properties测试控制器
 * created by hafiz.zhang on 2016/9/16.
 */
@controller
@requestmapping("/prop")
public class propertycontroller {
  @autowired
  private propertiesservice ps;

  @requestmapping(value = "/way/first", method = requestmethod.get)
  @responsebody
  public string getpropertybyfirstway(){
    return ps.getproperybyfirstway();
  }

  @requestmapping(value = "/way/second", method = requestmethod.get)
  @responsebody
  public string getpropertybysecondway(){
    return ps.getproperybysecondway();
  }

  @requestmapping(value = "/way/third", method = requestmethod.get)
  @responsebody
  public string getpropertybythirdway(){
    return ps.getproperybythirdway();
  }

  @requestmapping(value = "/way/fourth/{key}", method = requestmethod.get)
  @responsebody
  public string getpropertybyfourthway(@pathvariable("key") string key){
    return ps.getproperybyfourthway(key, "defaultvalue");
  }

  @requestmapping(value = "/way/fifth/{key}", method = requestmethod.get)
  @responsebody
  public string getpropertybyfifthway(@pathvariable("key") string key){
    return propertyutil.getproperty(key, "defaultvalue");
  }
}

4.jdbc.properties文件

jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://192.168.1.196:3306/dev?useunicode=true&characterencoding=utf-8
jdbc.username=root
jdbc.password=123456
jdbc.maxactive=200
jdbc.minidle=5
jdbc.initialsize=1
jdbc.maxwait=60000
jdbc.timebetweenevictionrunsmillis=60000
jdbc.minevictableidletimemillis=300000
jdbc.validationquery=select 1 from t_user
jdbc.testwhileidle=true
jdbc.testonreturn=false
jdbc.poolpreparedstatements=true
jdbc.maxpoolpreparedstatementperconnectionsize=20
jdbc.filters=stat
#test data
test=com.hafiz.www

5.项目结果图

  

6.项目下载:demo

7.测试结果

第一种方式

  

第二种方式

 

第三种方式

  

第四种方式

  

第五种方式

  

六、总结

通过本次的梳理和测试,我们理解了spring和springmvc的父子容器关系以及context:component-scan标签包扫描时最容易忽略的use-default-filters属性的作用以及原理。能够更好地定位和快速解决再遇到的问题。总之,棒棒哒~~~

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网