SpringBoot 自定义properties
来源:互联网 发布:淘宝店铺怎么贷款 编辑:程序博客网 时间:2024/06/03 12:56
spring boot使用application.properties默认了很多配置。但需要自己添加一些配置的时候,我们应该怎么做呢。
若继续在application.properties中添加
如:
- wisely2.name=wyf2
- wisely2.gender=male2
定义配置类:
- @ConfigurationProperties(prefix = "wisely2")
- public class Wisely2Settings {
- private String name;
- private String gender;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- }
若新用新的配置文件
如我新建一个wisely.properties
- wisely.name=wangyunfei
- wisely.gender=male
需定义如下配置类
- @ConfigurationProperties(prefix = "wisely",locations = "classpath:config/wisely.properties")
- public class WiselySettings {
- private String name;
- private String gender;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- }
最后注意在spring Boot入口类加上@EnableConfigurationProperties
- @SpringBootApplication
- @EnableConfigurationProperties({WiselySettings.class,Wisely2Settings.class})
- public class DemoApplication {
- public static void main(String[] args) {
- SpringApplication.run(DemoApplication.class, args);
- }
- }
使用定义的properties
在别的bean中可直接注入
- @Controller
- public class TestController {
- @Autowired
- WiselySettings wiselySettings;
- @Autowired
- Wisely2Settings wisely2Settings;
- @RequestMapping("/test")
- public @ResponseBody String test(){
- System.out.println(wiselySettings.getGender()+"---"+wiselySettings.getName());
- System.out.println(wisely2Settings.getGender()+"==="+wisely2Settings.getGender());
- return "ok";
- }
- }
阅读全文
0 0
- SpringBoot 自定义properties
- SpringBoot使用自定义的properties
- SpringBoot 学习记录(八): properties 属性自定义
- SpringBoot:Spring Boot使用自定义的properties
- springboot-加载自定义的properties文件
- 如何在SpringBoot下读取自定义properties配置文件
- 四、SpringBoot 读取Properties
- springBoot配置文件application.properties
- SpringBoot获取properties配置
- springboot application.properties详解
- SpringBoot 配置文件 application.properties
- springboot之properties
- Springboot配置文件之properties
- springboot-properties配置
- SpringBoot获取properties配置
- 3.SpringBoot-properties配置
- springboot application.properties配置
- SpringBoot Common Application Properties
- ExpandableListView扩展下拉列表
- cygwin中文输入
- ASP.NET MVC实现权限管理以及权限分配
- linux命令说明
- nginx 编译安装与配置
- SpringBoot 自定义properties
- oracle11g 数据库可用内存使用和分配
- Android NDK日志篇(二)
- Android中Fragment生命周期
- 栈和队列的定义
- javascript基础部分的学习
- 自定义View (二) : onMeasure流程 MeasureSpec详解
- Java守护线程
- vue实战笔记--搭建开发环境脚手架