spring boot @ConfigurationProperties @EnableConfigurationProperties 配置注解使用
程序员文章站
2022-04-27 20:25:24
...
- @ConfigurationProperties使用
application-dev.yml
my:
servers:
port: 8080
threadPool:
maxThreads: 100
minThreads: 8
idleTimeout: 6000
@ConfigurationProperties 使用
**
* Created by jiyang on 16:10 2017/12/15
*/
@Controller
@RequestMapping("/tester")
@Api(value = "测试页面", description = "测试页面相关接口")
@ConfigurationProperties(prefix = "my.servers")
public class TestController {
@Getter
@Setter
private int port;
@Getter
@Setter
private Map<String,Object> threadPool;
}
//file MyWebServerConfigurationProperties.java
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "my.webserver")
public class MyWebServerConfigurationProperties {
private int port;
private ThreadPool threadPool;
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public ThreadPool getThreadPool() {
return threadPool;
}
public void setThreadPool(ThreadPool threadPool) {
this.threadPool = threadPool;
}
public static class ThreadPool {
private int maxThreads;
private int minThreads;
private int idleTimeout;
public int getIdleTimeout() {
return idleTimeout;
}
public void setIdleTimeout(int idleTimeout) {
this.idleTimeout = idleTimeout;
}
public int getMaxThreads() {
return maxThreads;
}
public void setMaxThreads(int maxThreads) {
this.maxThreads = maxThreads;
}
public int getMinThreads() {
return minThreads;
}
public void setMinThreads(int minThreads) {
this.minThreads = minThreads;
}
}
}
// file: MyWebServerConfiguration.java
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@Configuration
@EnableConfigurationProperties(MyWebServerConfigurationProperties.class)
public class MyWebServerConfiguration {
@Autowired
private MyWebServerConfigurationProperties properties;
/**
*下面就可以引用MyWebServerConfigurationProperties类 里的配置了
*/
public void setMyconfig() {
String port = properties.getPort();
// ...........
}
推荐阅读
-
Spring配置shiro时自定义Realm中属性无法使用注解注入的解决办法
-
spring boot使用sharding jdbc的配置方式
-
详解Spring Boot 属性配置和使用
-
spring boot starter actuator(健康监控)配置和使用教程
-
Spring Boot 配置和使用多线程池的实现
-
spring boot开发遇到坑之spring-boot-starter-web配置文件使用教程
-
spring-boot通过@Scheduled配置定时任务及定时任务@Scheduled注解的方法
-
基于Spring Boot不同的环境使用不同的配置方法
-
详解Spring Boot Profiles 配置和使用
-
spring boot starter actuator(健康监控)配置和使用教程