SpringBoot 註解@ConfiguretionProperties
很多情況下我們會把配置檔案的資訊,讀取並自動封裝成實體類,我們在程式碼裡面使用就不用每次使用的時候去@Value,這時候我們就可以使用@ConfigurationProperties,它可以把同類的配置資訊自動封裝成實體類。
application.yml 配置資訊:
spring:
redis:
password: ys_123
clusterNodes: 10.108.10.46:6379
expireSeconds: 120
commandTimeout: 10000
pool:
maxActive: 5000
maxIdle: 30
minIdle: 5
maxWait: 3000
maxAttempts: 1
我們可以定義一個實體類在裝載配置檔案資訊:
package com.example.redis.redis;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "spring.redis", location = "classPath:/config/application.yml")//1.5版本之前有使用location屬性指定配置檔案
@PropertySource("classPath:/config/application.yml")//1.5版本之後去除了location屬性,需要使用@PropertySource註解
public class RedisProperties {
private int expireSeconds;
private String clusterNodes;
private String password;
private int commandTimeout;
private Map<String,Integer> pool = new HashMap<>();
public int getExpireSeconds() {
return expireSeconds;
}
public void setExpireSeconds(int expireSeconds) {
this.expireSeconds = expireSeconds;
}
public String getClusterNodes() {
return clusterNodes;
}
public void setClusterNodes(String clusterNodes) {
this.clusterNodes = clusterNodes;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getCommandTimeout() {
return commandTimeout;
}
public void setCommandTimeout(int commandTimeout) {
this.commandTimeout = commandTimeout;
}
public Map<String, Integer> getPool() {
return pool;
}
public void setPool(Map<String, Integer> pool) {
this.pool = pool;
}
}
我們還可以把@ConfigurationProperties還可以直接定義在@bean的註解上,這是bean實體類就不用@Component和@ConfigurationProperties了
package com.example.redis;
import com.example.redis.redis.RedisProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableCaching
public class RedisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(RedisDemoApplication.class, args);
}
@Bean
@ConfigurationProperties(prefix = "com.example.demo")
public RedisProperties people() {
return new RedisProperties();
}
}
呼叫
@Autowired RedisProperties conn;
public String test(){
String password = conn.getPassword();
return "hello task !!";
}
相關文章
- SpringBoot註解Spring Boot
- 【SpringBoot系列】SpringBoot註解詳解Spring Boot
- Springboot 常用註解Spring Boot
- SpringBoot系列 註解大全Spring Boot
- 註解@PropertySource使用 springbootSpring Boot
- 002 SpringBoot 常用註解Spring Boot
- SpringBoot(15)—@Conditional註解Spring Boot
- springboot常用註解記錄Spring Boot
- springboot的@ConditionalOnBean註解Spring BootBean
- SpringBoot主配置和註解Spring Boot
- springboot搭建以及常用註解Spring Boot
- SpringBoot註解大全(詳細)Spring Boot
- springboot介面接參註解詳解Spring Boot
- Spring註解篇,學完註解深入瞭解SpringBoot更容易Spring Boot
- SpringBoot 註解:全家桶快速通Spring Boot
- SpringBoot(14)—註解裝配BeanSpring BootBean
- SpringBoot 註解呼叫Redis快取Spring BootRedis快取
- springBoot自定義註解的使用Spring Boot
- SpringBoot自定義校驗註解Spring Boot
- Spring/SpringBoot常用註解總結Spring Boot
- springboot註解方式使用redis快取Spring BootRedis快取
- 【SpringBoot實戰】核心配置和註解Spring Boot
- SpringBoot基於註解方式配置FilterSpring BootFilter
- java中SpringBoot定時器註解JavaSpring Boot定時器
- SpringBoot註解最全詳解(整合超詳細版本)Spring Boot
- SpringBoot魔法堂:@MatrixVariable引數註解使用詳解Spring Boot
- SpringBoot原始碼解讀系列三——引導註解Spring Boot原始碼
- Springboot註解@ConfigurationProperties報錯Spring Boot
- SpringBoot自定義註解、AOP列印日誌Spring Boot
- springboot的註解的作用說明(全)Spring Boot
- SpringBoot + MyBatis(註解版),常用的SQL方法Spring BootMyBatisSQL
- springboot:非同步註解@Async的前世今生Spring Boot非同步
- SpringBoot dubbo 的簡單使用註解版本Spring Boot
- springboot整合redis及其註解式開發Spring BootRedis
- 一個註解搞定 SpringBoot 介面防刷Spring Boot
- springboot註冊Spring Boot
- SpringBoot2.x系列教程之 SpringBoot常用註解彙總Spring Boot
- SpringBoot註解使用redis做快取總結Spring BootRedis快取