版权声明:作者:星云 交流即分享,分享才能进步!喜欢我的文章,可在博客左侧扫码赞赏~ https://blog.csdn.net/hadues/article/details/89408533
Spring Boot 中自定义属性是一个非常常用的技术。
这节课我们就一起来学习下.
1.添加项目依赖
- 添加web依赖因为要在controller中进行测试
- 添加spring-boot-configuration-processor是自定义属性文件必须的
- spring-boot-starter-test 传递依赖了日志框架SLF4J +Logback
<dependencies>
<!--just for test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--自定义配置文件必须有此依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 传递依赖引入了日志框架,打印日志需要和测试类需要 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2. 为自定义配置属性自定义一个类
MyCustomProperties.java
package com.xingyun.springbootwithconfigurationprocessorsample.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 自定义属性前缀
* */
@ConfigurationProperties(prefix="com.xingyun.custom")
public class MyCustomProperties {
private String serverAddress; //对应的属性就是 com.xingyun.custom.server-address
private String port;//对应的属性就是 com.xingyun.custom.port
private String username;//对应属性就是 com.xingyun.custom.username
private String password;//对应属性就是 com.xingyun.custom.password
//getter and setter
public String getServerAddress() {
return serverAddress;
}
public void setServerAddress(String serverAddress) {
this.serverAddress = serverAddress;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
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;
}
}
值得注意的是,这里的变量名称不可以有下划线 否则会出错,
更多详情请看: @ConfigurationProperties(prefix =“xxx”)的值取出为空
3.创建一个文件夹config 激活自定义配置文件
在config 文件夹下新建CustomPropertiesConfig.java 类
package com.xingyun.springbootwithconfigurationprocessorsample.config;
import com.xingyun.springbootwithconfigurationprocessorsample.properties.MyCustomProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* 激活自定义属性扫描文件
* 多个自定义配置文件以逗号隔开配置
* */
@EnableConfigurationProperties({
MyCustomProperties.class
})
/**
* 注解表明是一个Spring 配置类
* */
@Configuration
public class CustomPropertiesConfig {
}
4.application-dev.properties 中使用它
application-dev.properties
# Port
server.port=8080
server.servlet.context-path=/
# Spring Boot 日志
# SpringBoot默认使用的的日志框架是slf4j + Logback
# Logback 没有 FATAL 级别,对应着 ERROR
# 日志级别从低到高
# TRACE < DEBUG < INFO < WARN < ERROR
## 默认配置INFO、WARN、ERROR级别的日志输出到控制台
logging.level.root=info
##配置自己的应用程序日志级别,com.xingyun 是自己应用程序的根包
logging.level.com.xingyun=debug
##设置其他全部的日志等级
logging.level.*=warn
# 自定义配置属性
com.xingyun.custom.server-address=192.168.1.1
com.xingyun.custom.port=5670
com.xingyun.custom.username=root
com.xingyun.custom.password=toor
5.新建一个Controller 测试使用
HomeController.java
package com.xingyun.springbootwithconfigurationprocessorsample.controller;
import com.xingyun.springbootwithconfigurationprocessorsample.properties.MyCustomProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
Logger logger= LoggerFactory.getLogger(HomeController.class);
@GetMapping(value = "/")
public String homePage(){
return "Hello World";
}
@Autowired
public MyCustomProperties myCustomProperties;
@GetMapping(value = "/test.do")
public String testCustomProperties(){
logger.debug("自定义属性测试使用开始");
logger.debug("server address:{}",myCustomProperties.getServerAddress());
logger.debug("port:{}",myCustomProperties.getPort());
logger.debug("username:{}",myCustomProperties.getUsername());
logger.debug("password:{}",myCustomProperties.getPassword());
logger.debug("自定义属性测试使用结束");
return "test success";
}
}
6.访问URL
访问请求 http://127.0.0.1:8080/test.do
打印结果如下:
GitHub源码下载
本篇完~
更多关注我的这个系列的专栏:Spring Boot 2.x 最佳实践手册