SpringBoot四种读取properties文件的方式(小结)

 更新时间:2020年05月27日 09:08:28   作者:猿敲月下码  
这篇文章主要介绍了SpringBoot四种读取properties文件的方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

在项目开发中经常会用到配置文件,配置文件的存在解决了很大一份重复的工作。今天就分享四种在Springboot中获取配置文件的方式。

注:前三种测试配置文件为springboot默认的application.properties文件

#######################方式一#########################
com.zyd.type3=Springboot - @ConfigurationProperties
com.zyd.title3=使用@ConfigurationProperties获取配置文件
#map
com.zyd.login[username]=zhangdeshuai
com.zyd.login[password]=zhenshuai
com.zyd.login[callback]=http://www.flyat.cc
#list
com.zyd.urls[0]=http://ztool.cc
com.zyd.urls[1]=http://ztool.cc/format/js
com.zyd.urls[2]=http://ztool.cc/str2image
com.zyd.urls[3]=http://ztool.cc/json2Entity
com.zyd.urls[4]=http://ztool.cc/ua

#######################方式二#########################
com.zyd.type=Springboot - @Value
com.zyd.title=使用@Value获取配置文件

#######################方式三#########################
com.zyd.type2=Springboot - Environment
com.zyd.title2=使用Environment获取配置文件

一、@ConfigurationProperties方式

自定义配置类:PropertiesConfig.java

package com.zyd.property.config;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 对应上方配置文件中的第一段配置
 * @author <a href="mailto:yadong.zhang0415@gmail.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >yadong.zhang</a>
 * @date 2017年6月1日 下午4:34:18 
 * @version V1.0
 * @since JDK : 1.7
 */
@Component
@ConfigurationProperties(prefix = "com.zyd")
// PropertySource默认取application.properties
// @PropertySource(value = "config.properties")
public class PropertiesConfig {

  public String type3;
  public String title3;
  public Map<String, String> login = new HashMap<String, String>();
  public List<String> urls = new ArrayList<>();

  public String getType3() {
    return type3;
  }

  public void setType3(String type3) {
    this.type3 = type3;
  }

  public String getTitle3() {
    try {
      return new String(title3.getBytes("ISO-8859-1"), "UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return title3;
  }

  public void setTitle3(String title3) {
    this.title3 = title3;
  }

  public Map<String, String> getLogin() {
    return login;
  }

  public void setLogin(Map<String, String> login) {
    this.login = login;
  }

  public List<String> getUrls() {
    return urls;
  }

  public void setUrls(List<String> urls) {
    this.urls = urls;
  }

} 

程序启动类:Applaction.java

package com.zyd.property;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zyd.property.config.PropertiesConfig;

/**
 * @author <a href="mailto:yadong.zhang0415@gmail.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >yadong.zhang</a>
 * @date 2017年6月1日 下午3:49:30 
 * @version V1.0
 * @since JDK : 1.7
 */
@SpringBootApplication
@RestController
public class Applaction {

  @Autowired
  private PropertiesConfig propertiesConfig;

  /**
   * 
   * 第一种方式:使用`@ConfigurationProperties`注解将配置文件属性注入到配置对象类中
   * 
   * @author zyd
   * @throws UnsupportedEncodingException
   * @since JDK 1.7
   */
  @RequestMapping("/config")
  public Map<String, Object> configurationProperties() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("type", propertiesConfig.getType3());
    map.put("title", propertiesConfig.getTitle3());
    map.put("login", propertiesConfig.getLogin());
    map.put("urls", propertiesConfig.getUrls());
    return map;
  }

  public static void main(String[] args) throws Exception {
    SpringApplication application = new SpringApplication(Applaction.class);
    application.run(args);
  }
}

访问结果:

{"title":"使用@ConfigurationProperties获取配置文件","urls":["http://ztool.cc","http://ztool.cc/format/js","http://ztool.cc/str2image","http://ztool.cc/json2Entity","http://ztool.cc/ua"],"login":{"username":"zhangdeshuai","callback":"http://www.flyat.cc","password":"zhenshuai"},"type":"Springboot - @ConfigurationProperties"}

二、使用@Value注解方式

程序启动类:Applaction.java

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author <a href="mailto:yadong.zhang0415@gmail.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >yadong.zhang</a>
 * @date 2017年6月1日 下午3:49:30 
 * @version V1.0
 * @since JDK : 1.7
 */
@SpringBootApplication
@RestController
public class Applaction {

  @Value("${com.zyd.type}")
  private String type;

  @Value("${com.zyd.title}")
  private String title;

  /**
   * 
   * 第二种方式:使用`@Value("${propertyName}")`注解
   * 
   * @author zyd
   * @throws UnsupportedEncodingException
   * @since JDK 1.7
   */
  @RequestMapping("/value")
  public Map<String, Object> value() throws UnsupportedEncodingException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("type", type);
    // *.properties文件中的中文默认以ISO-8859-1方式编码,因此需要对中文内容进行重新编码
    map.put("title", new String(title.getBytes("ISO-8859-1"), "UTF-8"));
    return map;
  }

  public static void main(String[] args) throws Exception {
    SpringApplication application = new SpringApplication(Applaction.class);
    application.run(args);
  }
}

访问结果:

{"title":"使用@Value获取配置文件","type":"Springboot - @Value"}

三、使用Environment

程序启动类:Applaction.java

package com.zyd.property;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author <a href="mailto:yadong.zhang0415@gmail.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >yadong.zhang</a>
 * @date 2017年6月1日 下午3:49:30
 * @version V1.0
 * @since JDK : 1.7
 */
@SpringBootApplication
@RestController
public class Applaction {

  @Autowired
  private Environment env;

  /**
   * 
   * 第三种方式:使用`Environment`
   * 
   * @author zyd
   * @throws UnsupportedEncodingException
   * @since JDK 1.7
   */
  @RequestMapping("/env")
  public Map<String, Object> env() throws UnsupportedEncodingException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("type", env.getProperty("com.zyd.type2"));
    map.put("title", new String(env.getProperty("com.zyd.title2").getBytes("ISO-8859-1"), "UTF-8"));
    return map;
  }

  public static void main(String[] args) throws Exception {
    SpringApplication application = new SpringApplication(Applaction.class);
    application.run(args);
  }
}

访问结果:

{"title":"使用Environment获取配置文件","type":"Springboot - Environment"}

四、使用PropertiesLoaderUtils

app-config.properties

#### 通过注册监听器(`Listeners`) + `PropertiesLoaderUtils`的方式
com.zyd.type=Springboot - Listeners
com.zyd.title=使用Listeners + PropertiesLoaderUtils获取配置文件
com.zyd.name=zyd
com.zyd.address=Beijing
com.zyd.company=in

PropertiesListener.java 用来初始化加载配置文件

package com.zyd.property.listener;

import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;

import com.zyd.property.config.PropertiesListenerConfig;

/**
 * 配置文件监听器,用来加载自定义配置文件
 * 
 * @author <a href="mailto:yadong.zhang0415@gmail.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >yadong.zhang</a>
 * @date 2017年6月1日 下午3:38:25 
 * @version V1.0
 * @since JDK : 1.7
 */
public class PropertiesListener implements ApplicationListener<ApplicationStartedEvent> {

  private String propertyFileName;

  public PropertiesListener(String propertyFileName) {
    this.propertyFileName = propertyFileName;
  }

  @Override
  public void onApplicationEvent(ApplicationStartedEvent event) {
    PropertiesListenerConfig.loadAllProperties(propertyFileName);
  }
}

PropertiesListenerConfig.java 加载配置文件内容

package com.zyd.property.config;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * 第四种方式:PropertiesLoaderUtils
 * 
 * @author <a href="mailto:yadong.zhang0415@gmail.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >yadong.zhang</a>
 * @date 2017年6月1日 下午3:32:37
 * @version V1.0
 * @since JDK : 1.7
 */
public class PropertiesListenerConfig {
  public static Map<String, String> propertiesMap = new HashMap<>();

  private static void processProperties(Properties props) throws BeansException {
    propertiesMap = new HashMap<String, String>();
    for (Object key : props.keySet()) {
      String keyStr = key.toString();
      try {
        // PropertiesLoaderUtils的默认编码是ISO-8859-1,在这里转码一下
        propertiesMap.put(keyStr, new String(props.getProperty(keyStr).getBytes("ISO-8859-1"), "utf-8"));
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      } catch (java.lang.Exception e) {
        e.printStackTrace();
      }
    }
  }

  public static void loadAllProperties(String propertyFileName) {
    try {
      Properties properties = PropertiesLoaderUtils.loadAllProperties(propertyFileName);
      processProperties(properties);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static String getProperty(String name) {
    return propertiesMap.get(name).toString();
  }

  public static Map<String, String> getAllProperty() {
    return propertiesMap;
  }
}

Applaction.java 启动类

package com.zyd.property;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zyd.property.config.PropertiesListenerConfig;
import com.zyd.property.listener.PropertiesListener;

/**
 * @author <a href="mailto:yadong.zhang0415@gmail.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >yadong.zhang</a>
 * @date 2017年6月1日 下午3:49:30 
 * @version V1.0
 * @since JDK : 1.7
 */
@SpringBootApplication
@RestController
public class Applaction {
  /**
   * 
   * 第四种方式:通过注册监听器(`Listeners`) + `PropertiesLoaderUtils`的方式
   * 
   * @author zyd
   * @throws UnsupportedEncodingException
   * @since JDK 1.7
   */
  @RequestMapping("/listener")
  public Map<String, Object> listener() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.putAll(PropertiesListenerConfig.getAllProperty());
    return map;
  }

  public static void main(String[] args) throws Exception {
    SpringApplication application = new SpringApplication(Applaction.class);
    // 第四种方式:注册监听器
    application.addListeners(new PropertiesListener("app-config.properties"));
    application.run(args);
  }
}

访问结果:

{"com.zyd.name":"zyd","com.zyd.address":"Beijing","com.zyd.title":"使用Listeners + PropertiesLoaderUtils获取配置文件","com.zyd.type":"Springboot - Listeners","com.zyd.company":"in"}

到此这篇关于SpringBoot四种读取properties文件的方式(小结)的文章就介绍到这了,更多相关SpringBoot读取properties文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java中多线程同步类 CountDownLatch

    Java中多线程同步类 CountDownLatch

    本篇文章主要介绍了Java中多线程同步类 CountDownLatch的相关知识,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-05-05
  • 基于Java回顾之多线程同步的使用详解

    基于Java回顾之多线程同步的使用详解

    在这篇文章里,我们关注线程同步的话题。这是比多线程更复杂,稍不留意,我们就会“掉到坑里”,而且和单线程程序不同,多线程的错误是否每次都出现,也是不固定的,这给调试也带来了很大的挑战
    2013-05-05
  • Java毕业设计实战项目之在线服装销售商城系统的实现流程

    Java毕业设计实战项目之在线服装销售商城系统的实现流程

    基础掌握怎么样,用实战检验就知道了,本篇文章手把手带你用java+SpringBoot+Maven+Vue+mysql实现一个在线服装销售商城系统,大家可以在过程中查缺补漏,提升水平
    2022-01-01
  • Spring框架配置java web实现实例化

    Spring框架配置java web实现实例化

    这篇文章主要介绍了Spring框架配置java web实现实例化,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Spring Boot使用JSR-380进行校验的示例

    Spring Boot使用JSR-380进行校验的示例

    这篇文章主要介绍了Spring Boot使用JSR-380进行校验,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • 基于LinkedHashMap实现LRU缓存

    基于LinkedHashMap实现LRU缓存

    LinkedHashMap是Java集合中一个常用的容器,它继承了HashMap, 是一个有序的Hash表。那么该如何基于LinkedHashMap实现一个LRU缓存呢?本文将介绍LinkedHashMap的实现原理,感兴趣的同学可以参考一下
    2023-05-05
  • 详解spring面向切面aop拦截器

    详解spring面向切面aop拦截器

    spring中有很多概念和名词,比如过滤器、拦截器、aop等。这篇文章主要介绍了详解spring面向切面aop拦截器,有兴趣的可以了解一下。
    2017-03-03
  • 快速了解Hibernate中的Session

    快速了解Hibernate中的Session

    这篇文章主要介绍了快速了解Hibernate中的Session,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • spring cloud如何集成nacos配置中心

    spring cloud如何集成nacos配置中心

    这篇文章主要介绍了spring cloud如何集成nacos配置中心操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Java实现的日期处理类完整实例

    Java实现的日期处理类完整实例

    这篇文章主要介绍了Java实现的日期处理类,结合完整实例形式分析了Java针对日期的获取、运算、转换等相关操作技巧,需要的朋友可以参考下
    2017-09-09

最新评论