2019独角兽企业重金招聘Python工程师标准>>>

今天这篇文章给大家介绍自定义配置的两种方式 第一式: 使用@ConfigurationProperties,且看代码

package com.developlee.customconfig.config;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.Configuration;/*** @author Lensen* @desc* @since 2018/8/22 12:59*/
@Configuration
@ConfigurationProperties(prefix = "one-app")
public class OneAppConfig {@NestedConfigurationPropertypublic Account account = new Account();public String appName;public Account getAccount() {return account;}public void setAccount(Account account) {this.account = account;}public String getAppName() {return appName;}public void setAppName(String appName) {this.appName = appName;}public class Account {private String username;private String password;private String age;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}}
}

很明显,这就是我们要在properties文件中要配置的配置项。 再看第二种方式

/*** @author Lensen* @desc* @since 2018/8/22 13:19*/
@Configuration
public class TwoAppConfig {@Value("${two-app.welcome.message}")public String twoAppWelcomeMessage;@Value("${two-app.welcome.person}")public String twoAppWelcomePerson;public String getTwoAppWelcomeMessage() {return twoAppWelcomeMessage;}public void setTwoAppWelcomeMessage(String twoAppWelcomeMessage) {this.twoAppWelcomeMessage = twoAppWelcomeMessage;}public String getTwoAppWelcomePerson() {return twoAppWelcomePerson;}public void setTwoAppWelcomePerson(String twoAppWelcomePerson) {this.twoAppWelcomePerson = twoAppWelcomePerson;}
}

这个就简单粗暴啦。没有第一种方式结构那么清晰,具体怎么使用,完全取决于项目配置项的关联关系和复杂度,需要大家根据实际情况权衡。 接下来我写了个简单的测试类,来获取我们的配置信息 先看配置文件:

one-app:app-name: OneAPPaccount:username: Lensenpassword: Orclage: 22two-app:welcome:message: welcome to lensen's bolgperson: LENSEN

一个简单的Controller类

package com.developlee.customconfig.controller;import com.developlee.customconfig.config.OneAppConfig;
import com.developlee.customconfig.config.TwoAppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author Lensen* @desc* @since 2018/8/22 16:40*/
@RestController
public class AppController {@Autowiredprivate OneAppConfig oneAppConfig;@Autowiredprivate TwoAppConfig twoAppConfig;@GetMapping("/hello")public ResponseEntity getConfig() {String str1 = "oneAppConfig: " + oneAppConfig.getAppName() + oneAppConfig.getAccount().getUsername()+ oneAppConfig.getAccount().getPassword() + oneAppConfig.getAccount().getAge();String str2 = "twoAppConfig: " + twoAppConfig.getTwoAppWelcomePerson() + twoAppConfig.getTwoAppWelcomeMessage();return new ResponseEntity(str1 +"~~~~~~~"+ str2, HttpStatus.OK);}
}

在地址栏输入http:localhost:8080/hello, 回车 也可以自己指定文件,只需在类上加上注解@PropertySource注解就好了~~

@Configuration
@PropertySource("classpath:my.properties")
public class ThreeConfig {@Value("${my.name}")private String myName;public String getMyName() {return myName;}public void setMyName(String myName) {this.myName = myName;}
}

my.properties文件内容:

my.name=developlee

测试结果:

如果配置文件是yml格式的,则要使用YamlPropertiesFactoryBean来加载并设置到PropertySourcesPlaceholderConfigurer中

// 加载YML格式自定义配置文件@Beanpublic static PropertySourcesPlaceholderConfigurer properties() {PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();yaml.setResources(new FileSystemResource("config.yml"));//File引入
//      yaml.setResources(new ClassPathResource("youryml.yml"));//class引入configurer.setProperties(yaml.getObject());return configurer;

end... 浮躁的社会,浮躁的人生,唯有代码,宁静致远。(又开始装13了,见谅.....)

最后,以上示例代码可在我的github.com中找到。 我的个人公众号:developlee的潇洒人生。 关注了也不一定更新,更新就不得了了。

转载于:https://my.oschina.net/liululee/blog/2250330

精通Spring Boot——第十一篇:使用自定义配置相关推荐

  1. 精通Spring Boot—— 第二十一篇:Spring Social OAuth 登录简介

    2019独角兽企业重金招聘Python工程师标准>>> 1.什么是OAuth OAuth官网介绍是这样的: An open protocol to allow secure auth ...

  2. 精通Spring Boot——第三篇:详解WebMvcConfigurer接口

    2019独角兽企业重金招聘Python工程师标准>>> SpringBoot 确实为我们做了很多事情, 但有时候我们想要自己定义一些Handler,Interceptor,ViewR ...

  3. spring Boot 2 基础篇 。内含 整合一个spring boot 的 小案例

    目录 springBoot2基础篇 前言与开发环境 一.快速创建Boot项目 1.使用spring提供的快速构建 2.基于maven的手动构建 3.在Idea中隐藏指定文件/文件夹 二.SpringB ...

  4. 第七章、Spring Boot MyBatis升级篇

    课时二十七.Spring Boot MyBatis升级篇-注解 缘起:在一节视频中,有这么一段留言:"会不会推出SpringBoot整合Mybaits配置文件sqlMapConfig.xml ...

  5. Spring Boot(十一)Redis集成从Docker安装到分布式Session共享

    2019独角兽企业重金招聘Python工程师标准>>> 一.简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并 ...

  6. 54. spring boot日志升级篇—logback【从零开始学Spring Boot】

    在<44. Spring Boot日志记录SLF4J>章节中有关相关的介绍,这里我们在深入的了解下logback框架. 为什么要使用logback ? --在开发中不建议使用System. ...

  7. Spring Boot教程(一)注解配置与EhCache使用

    2019独角兽企业重金招聘Python工程师标准>>> 快速入门 首先,下载样例工程chapter3-2-2.本例通过spring-data-jpa实现了对User用户表的一些操作, ...

  8. Spring Boot 2.4 对多环境配置的支持更改

    在目前最新的Spring Boot 2.4版本中,对配置的加载机制做了较大的调整.相关的问题最近也被问的比较多,所以今天就花点时间,给大家讲讲Spring Boot 2.4的多环境配置较之前版本有哪些 ...

  9. Spring Boot(一) 自动装配--约定大于配置

    欢迎去我的个人博客--晓坞坐坐,里面有很多有趣的文章,不止是技术.www.lixiaodongisme.com 随着spring体系的发展,我们在开发时需要配置整合的东西也越来越多,在进行开发时,繁琐 ...

最新文章

  1. windows版本下使用xdebug
  2. error;It could not find or load the Qt platform plugin “windows”
  3. 南邮CTF密码学write up
  4. linux crond命令
  5. 洛谷 - P1198 - 最大数 - 线段树
  6. 计算机组成原理sop,MacBERT:MLM as correction BERT
  7. HD1864_最大报销额
  8. 将DataTable 数据插入 SQL SERVER 数据库
  9. 修改结构体中成员的值
  10. CopyOnWriteArrayList原理及算法题存档
  11. 关于字符集的简单介绍
  12. mysql 主从 锁库_mysql 5.7.21 主从集群恢复GTID方式(不锁库)
  13. 新申请了一个博客,这个博客主要用来记录编程学习笔记
  14. Vijos P1848 计数问题
  15. python类和对象的应用:烤地瓜
  16. php实现ctrl+f,Ctrl+F 到底有多好用?这 5 个骚操作,让你变身快捷键达人
  17. win10 机械硬盘 开机速度缓慢的原因排查与优化
  18. 在线CAD平台,MxCAD云图 2021.01.20更新,在线CAD软件
  19. 均匀节点插值与切比雪夫插值以及龙格现象
  20. uip的yeelink实现

热门文章

  1. linux 挂载多余空间,linux 空间不够,磁盘挂载
  2. 数据导出生成word附件使用POI的XWPFTemplate对象
  3. P4592 [TJOI2018]异或 (可持久化Trie)
  4. 高性能MySQL(二)
  5. Python算法——二叉树
  6. UEditor 插入图片大于2M提示文件大小超出范围解决办法
  7. php pcntl 多进程学习
  8. 用计算器计算“异或CRC”
  9. 以嵌入式系统设计师考试成绩,开始嵌入式博客之旅
  10. GL ERROR - after deleteUnusedTextures() glError (0x502)