1. 转载:https://www.imooc.com/article/18252
    一、@ConfigurationProperties方式
  2. 自定义配置类:PropertiesConfig.java
  3.  
  4. package com.zyd.property.config;
  5.  
  6. import java.io.UnsupportedEncodingException;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11.  
  12. import org.springframework.boot.context.properties.ConfigurationProperties;
  13. //import org.springframework.context.annotation.PropertySource;
  14. import org.springframework.stereotype.Component;
  15.  
  16. /**
  17. * 对应上方配置文件中的第一段配置
  18. * @author <a href="mailto:yadong.zhang0415@gmail.com">yadong.zhang</a>
  19. * @date 2017年6月1日 下午4:34:18
  20. * @version V1.0
  21. * @since JDK : 1.7
  22. */
  23. @Component
  24. @ConfigurationProperties(prefix = "com.zyd")
  25. // PropertySource默认取application.properties
  26. // @PropertySource(value = "config.properties")
  27. public class PropertiesConfig {
  28.  
  29. public String type3;
  30. public String title3;
  31. public Map<String, String> login = new HashMap<String, String>();
  32. public List<String> urls = new ArrayList<>();
  33.  
  34. public String getType3() {
  35. return type3;
  36. }
  37.  
  38. public void setType3(String type3) {
  39. this.type3 = type3;
  40. }
  41.  
  42. public String getTitle3() {
  43. try {
  44. return new String(title3.getBytes("ISO-8859-1"), "UTF-8");
  45. } catch (UnsupportedEncodingException e) {
  46. e.printStackTrace();
  47. }
  48. return title3;
  49. }
  50.  
  51. public void setTitle3(String title3) {
  52. this.title3 = title3;
  53. }
  54.  
  55. public Map<String, String> getLogin() {
  56. return login;
  57. }
  58.  
  59. public void setLogin(Map<String, String> login) {
  60. this.login = login;
  61. }
  62.  
  63. public List<String> getUrls() {
  64. return urls;
  65. }
  66.  
  67. public void setUrls(List<String> urls) {
  68. this.urls = urls;
  69. }
  70.  
  71. }
  72. 程序启动类:Applaction.java
  73.  
  74. package com.zyd.property;
  75.  
  76. import java.io.UnsupportedEncodingException;
  77. import java.util.HashMap;
  78. import java.util.Map;
  79.  
  80. import org.springframework.beans.factory.annotation.Autowired;
  81. import org.springframework.boot.SpringApplication;
  82. import org.springframework.boot.autoconfigure.SpringBootApplication;
  83. import org.springframework.web.bind.annotation.RequestMapping;
  84. import org.springframework.web.bind.annotation.RestController;
  85.  
  86. import com.zyd.property.config.PropertiesConfig;
  87.  
  88. /**
  89. * @author <a href="mailto:yadong.zhang0415@gmail.com">yadong.zhang</a>
  90. * @date 2017年6月1日 下午3:49:30
  91. * @version V1.0
  92. * @since JDK : 1.7
  93. */
  94. @SpringBootApplication
  95. @RestController
  96. public class Applaction {
  97.  
  98. @Autowired
  99. private PropertiesConfig propertiesConfig;
  100.  
  101. /**
  102. *
  103. * 第一种方式:使用`@ConfigurationProperties`注解将配置文件属性注入到配置对象类中
  104. *
  105. * @author zyd
  106. * @throws UnsupportedEncodingException
  107. * @since JDK 1.7
  108. */
  109. @RequestMapping("/config")
  110. public Map<String, Object> configurationProperties() {
  111. Map<String, Object> map = new HashMap<String, Object>();
  112. map.put("type", propertiesConfig.getType3());
  113. map.put("title", propertiesConfig.getTitle3());
  114. map.put("login", propertiesConfig.getLogin());
  115. map.put("urls", propertiesConfig.getUrls());
  116. return map;
  117. }
  118.  
  119. public static void main(String[] args) throws Exception {
  120. SpringApplication application = new SpringApplication(Applaction.class);
  121. application.run(args);
  122. }
  123. }
  124. 访问结果:
  125.  
  126. {"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"}
  127. 二、使用@Value注解方式
  128. 程序启动类:Applaction.java
  129.  
  130. package com.zyd.property;
  131.  
  132. import java.io.UnsupportedEncodingException;
  133. import java.util.HashMap;
  134. import java.util.Map;
  135.  
  136. import org.springframework.beans.factory.annotation.Value;
  137. import org.springframework.boot.SpringApplication;
  138. import org.springframework.boot.autoconfigure.SpringBootApplication;
  139. import org.springframework.web.bind.annotation.RequestMapping;
  140. import org.springframework.web.bind.annotation.RestController;
  141.  
  142. /**
  143. * @author <a href="mailto:yadong.zhang0415@gmail.com">yadong.zhang</a>
  144. * @date 2017年6月1日 下午3:49:30
  145. * @version V1.0
  146. * @since JDK : 1.7
  147. */
  148. @SpringBootApplication
  149. @RestController
  150. public class Applaction {
  151.  
  152. @Value("${com.zyd.type}")
  153. private String type;
  154.  
  155. @Value("${com.zyd.title}")
  156. private String title;
  157.  
  158. /**
  159. *
  160. * 第二种方式:使用`@Value("${propertyName}")`注解
  161. *
  162. * @author zyd
  163. * @throws UnsupportedEncodingException
  164. * @since JDK 1.7
  165. */
  166. @RequestMapping("/value")
  167. public Map<String, Object> value() throws UnsupportedEncodingException {
  168. Map<String, Object> map = new HashMap<String, Object>();
  169. map.put("type", type);
  170. // *.properties文件中的中文默认以ISO-8859-1方式编码,因此需要对中文内容进行重新编码
  171. map.put("title", new String(title.getBytes("ISO-8859-1"), "UTF-8"));
  172. return map;
  173. }
  174.  
  175. public static void main(String[] args) throws Exception {
  176. SpringApplication application = new SpringApplication(Applaction.class);
  177. application.run(args);
  178. }
  179. }
  180. 访问结果:
  181.  
  182. {"title":"使用@Value获取配置文件","type":"Springboot - @Value"}

这是几种较为常见的方式,其中第一种,必须在需要引用的类上加上@EnableConfigurationProperties注解,配合@ConfigurationProperties一起使用

SpringBoot的读取properties文件的方式的更多相关文章

  1. SpringBoot @Value读取properties文件的属性

    SpringBoot在application.properties文件中,可以自定义属性. 在properties文件中如下示: #自定义属性 mail.fromMail.addr=lgr@163.c ...

  2. SpringBoot:四种读取properties文件的方式

    前言 在项目开发中经常会用到配置文件,配置文件的存在解决了很大一份重复的工作.今天就分享四种在Springboot中获取配置文件的方式. 注:前三种测试配置文件为springboot默认的applic ...

  3. SpringBoot四种读取properties文件的方式

    环境:IDEA jdk1.8 SpringBoot2.1.4 例,如下默认application.properties文件   一.使用`@ConfigurationProperties`注解将配置文 ...

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

    一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...

  5. 【开发笔记】- Java读取properties文件的五种方式

    原文地址:https://www.cnblogs.com/hafiz/p/5876243.html 一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供j ...

  6. Java 读取 .properties 文件的几种方式

    Java 读取 .properties 配置文件的几种方式   Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 ...

  7. springBoot使用@Value标签读取*.properties文件的中文乱码问题

    上次我碰到获取properties文件中的中文出现乱码问题. 查了下资料,原来properties默认的字符编码格式为asci码,所以我们要对字符编码进行转换成UTF-8格式 原先代码:@Proper ...

  8. 用类加载器的5种方式读取.properties文件

    用类加载器的5中形式读取.properties文件(这个.properties文件一般放在src的下面) 用类加载器进行读取:这里采取先向大家讲读取类加载器的几种方法:然后写一个例子把几种方法融进去, ...

  9. SpringBoot11 读取properties文件、发送邮件

    1 读取properties文件 1.1 ResourceBundle 帮助我们事先国际化 1.1.1 前提 properties文件的命名方式必须体现除语言和国别 例如:test_zh_CN.pro ...

随机推荐

  1. 【MongoDB】关于无法连接mongo的问题

    今天使用MongoDB的时候发现直接输入mongo提示连接失败 首先想到的可能是服务还没启动 当我随便打开一个cmd输入net start MongoDB 提示:net start mongodb 发 ...

  2. php 读取网站页面源码的经典函数

    Snoopy.class.php下载 include "inc/Snoopy.class.php"; //读取网页,返回网页源文件内容 function read_url($str ...

  3. SQL Server 2008 R2官方中文版下载

    SQL Server 2008是一个重大的产品版本,它推出了许多新的特性和关键的改进,使得它成为至今为止的最强大和最全面的SQL Server版本. 在现今数据的世界里,公司要获得成功和不断发展,他们 ...

  4. 二:python 对象类型概述

    1,为什么使用内置类型: a)内置对象使程序更容易编写 b)内置对象是扩展的组件 c)内置对象往往比定制的数据结构更加高效 d)内置对象是语言的标准的一部分 2,python  的主要内置对象 对象类 ...

  5. DES算法实现(C++版)

    #include "memory.h" #include "stdio.h" enum {encrypt,decrypt};//ENCRYPT:加密,DECRY ...

  6. CUDA 编程的基本模式

    reproduced from: http://www.cnblogs.com/muchen/p/6306747.html 前言 本文将介绍 CUDA 编程的基本模式,所有 CUDA 程序都基于此模式 ...

  7. Python delattr() 函数

    Python delattr() 函数  Python 内置函数 描述 delattr 函数用于删除属性. delattr(x, 'foobar') 相等于 del x.foobar. 语法 dela ...

  8. 树形DP+RMQ+尺取法 hdu4123

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 参考博客:两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race ...

  9. 解决自定义classloader后无法使用maven install

    @上篇博客中探讨了web项目利用自定义classloader进行解密,利用的是编译后的文件直接运行程序一切正常 今天博主在探讨加密后进行混淆时,打包程序报程序包org.apache.catalina. ...

  10. spring boot 2.0 与FASTDFS进行整合

    只支持在spring-boot 2.0以及以上版本中使用 1.pom.xml 里引入FASTDFS的依赖,toobato与fastdfs作者一起,将fastdfs的功能进行了重构与简化 <!-- ...