Five ways to read the contents of a properties file in Java

Source: Internet
Author: User
Tags access properties
I. Background

Recently, in the process of project development, I encountered the need to define some custom variables in the properties file for the dynamic reading of Java programs, modify variables, no longer need to modify the code problem. To take this opportunity to spring+springmvc+mybatis integrated development of the project through the Java program to read the contents of the properties file of the way to comb and analyze, and we are sharing. second, the Project environment introduction

Spring 4.2.6.RELEASE

Springmvc 4.2.6.RELEASE

MyBatis 3.2.8

Maven 3.3.9

JDK 1.7

Idea 15.04 three or five ways to implement

Mode 1. Loading the contents of the configuration file jdbc.properties via Context:property-placeholder

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

The above configuration is equivalent to the following configuration, which simplifies the configuration below

<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>

Note: In this way, if you have the following configuration in the Spring-mvc.xml file, you must not lack the following Red section, about its function and principle, see another blog: Context: The function and principle analysis of the use-default-filters attribute of Component-scan label

<!--Configure component scans, SPRINGMVC containers only Scan controller annotations-->
<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>

Mode 2. Injecting using annotations, mainly using annotations in Java code to inject the corresponding value value in the properties file

<bean id= "prop" class= "Org.springframework.beans.factory.config.PropertiesFactoryBean" >
   <!-- Here is the Propertiesfactorybean class, which also has a locations attribute, and also receives an array that is the same as the above-->
   <property name= "Locations" >
       < array>
          <value>classpath:jdbc.properties</value>
       </array>
   </property>
</bean>

Mode 3. Use the util:properties tag to expose content in a properties file

<util:properties id= "Propertiesreader" location= "Classpath:jdbc.properties"/>

Note: Using the above line configuration, you need to declare the following red section in the header of the Spring-dao.xml file

<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/spring-util.xsd ">

Mode 4. Exposes properties to custom subclass attributes for use in programs by Propertyplaceholderconfigurer when loading context

<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>

The declaration of the custom class Propertyconfigurer is as follows:

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 configuration file Read class * Created by Hafiz.zhang on 2016/9/14.       * * public class Propertyconfigurer extends Propertyplaceholderconfigurer {private Properties props; Access properties configuration file Key-value results @Override protected void processproperties (Configurablelistablebeanfactory beanfac Torytoprocess, Properties props) throws Beansexception {super.processproperties (beanf
        Actorytoprocess, 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, Defaultvalu
    e); } public Object SETproperty (string key, String value) {return This.props.setProperty (key, value); }
}

How to use: Use the @autowired annotation in the class you want to use.

Mode 5. Custom tool class Propertyutil, and read the properties file contents in static static code block of the class stored in the static property for other programs to use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.