SpringBoot 註解@ConfiguretionProperties

延陵縹緲發表於2018-06-20

        很多情況下我們會把配置檔案的資訊,讀取並自動封裝成實體類,我們在程式碼裡面使用就不用每次使用的時候去@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 !!";
}

相關文章