SpringBoot的读取properties文件的方式
- 转载:https://www.imooc.com/article/18252
一、@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">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">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
- package com.zyd.property;
- 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">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"}
这是几种较为常见的方式,其中第一种,必须在需要引用的类上加上@EnableConfigurationProperties注解,配合@ConfigurationProperties一起使用
SpringBoot的读取properties文件的方式的更多相关文章
- SpringBoot @Value读取properties文件的属性
SpringBoot在application.properties文件中,可以自定义属性. 在properties文件中如下示: #自定义属性 mail.fromMail.addr=lgr@163.c ...
- SpringBoot:四种读取properties文件的方式
前言 在项目开发中经常会用到配置文件,配置文件的存在解决了很大一份重复的工作.今天就分享四种在Springboot中获取配置文件的方式. 注:前三种测试配置文件为springboot默认的applic ...
- SpringBoot四种读取properties文件的方式
环境:IDEA jdk1.8 SpringBoot2.1.4 例,如下默认application.properties文件 一.使用`@ConfigurationProperties`注解将配置文 ...
- 五种方式让你在java中读取properties文件内容不再是难题
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- 【开发笔记】- Java读取properties文件的五种方式
原文地址:https://www.cnblogs.com/hafiz/p/5876243.html 一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供j ...
- Java 读取 .properties 文件的几种方式
Java 读取 .properties 配置文件的几种方式 Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 ...
- springBoot使用@Value标签读取*.properties文件的中文乱码问题
上次我碰到获取properties文件中的中文出现乱码问题. 查了下资料,原来properties默认的字符编码格式为asci码,所以我们要对字符编码进行转换成UTF-8格式 原先代码:@Proper ...
- 用类加载器的5种方式读取.properties文件
用类加载器的5中形式读取.properties文件(这个.properties文件一般放在src的下面) 用类加载器进行读取:这里采取先向大家讲读取类加载器的几种方法:然后写一个例子把几种方法融进去, ...
- SpringBoot11 读取properties文件、发送邮件
1 读取properties文件 1.1 ResourceBundle 帮助我们事先国际化 1.1.1 前提 properties文件的命名方式必须体现除语言和国别 例如:test_zh_CN.pro ...
随机推荐
- telnet ip/域名 端口 是否成功
有时候会ping ip 通,但是telnet不通,可能端口未开. telnet不成功,则显示不能打开到主机的链接,链接失败 . telnet成功,则进入telnet页面(全黑的),证明端口可用.
- 用photoshop 把视频镜头做成GIF图片
https://jingyan.baidu.com/article/47a29f2432e113c0142399b0.html
- 统计请求最高的TOP 5
cat access.log |awk -F "," '{print$14}'|awk -F "\"" '{print$4}'|sort |uniq ...
- hdoj1004(查找众多字符串中个数最多的字符串)
Let the Balloon Rise. 最近开始刷hdoj,想通过写博客做做笔记,记录写过代码. Problem Description Contest time again! How excit ...
- Maven clean基本命令
转自--------------------------------------https://www.cnblogs.com/hiver/p/7850954.html 假设现有项目结构如下 dail ...
- CentOS 几种重启方式的区别
Linux centos重启命令: 1.reboot 普通重启 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(root用户 ...
- 1009 数字1的数量 数位dp
1级算法题就这样了,前途渺茫啊... 更新一下博客,我刚刚想套用数位dp的模板,发现用那个模板也是可以做到,而且比第二种方法简单很多 第一种方法:我现在用dp[pos][now]来表示第pos位数字为 ...
- linux 重定向命令详解(如1>/dev/null 2>&1)
基础 0:表示标准输入stdin 1:表示标准输出stdout,系统默认为1,可省略(即1>/dev/null等价于>/dev/null) 2:表示标准错误stderr >:表示重定 ...
- HDU-1459.非常可乐(BFS )
这道题TLE了很多次,原来一直以为将数字化为最简可以让运算更快,但是去了简化之后才发现,真正耗时的就是化简....还和队友学到了用状态少直接数组模拟刚就能过... 本题大意:给出可乐的体积v1,给出两 ...
- [Unity3D]降低向Shader中传值的开销
Unity3D中提供了很多API用于向shader传值,这篇文章对比测试了两类不同的使用方法的性能. 正文 Unity3D中,通过C#代码向shader传值有两种方式. 一种是面向具体的materia ...