1. 程式人生 > >spring boot讀取自定義配置類

spring boot讀取自定義配置類

原理:在本地或者專案的配置檔案裡 寫了一些屬性,把這些屬性封裝到編寫的類裡,什麼地方需要就把該類注入即可

spring boot 1.5版本之前的寫法

第一步 定義配置類

配置類要實現版本號要有get/set方法 可以用@Data實現省去set與get lokback功能

只需要一個@ConfigurationProperties註解即可

而且@ConfigurationProperties 裡還可以加入位置屬性,

                                        locations = "classpath:/author/authorA.properties"     位置屬性 

這裡是從配置中心獲取的配置就省略該屬性了

@ConfigurationProperties(ignoreUnknownFields = true, prefix = "manageMongo")
public class ManageMongoProperties implements Serializable{
	private static final long serialVersionUID = 20180906164701L;
	private String host ;
	private Integer port = 27107;
	private Integer connectionsPerHost = 27107;
	private Integer maxWaitTime = 27107;
	private Integer socketTimeout = 27107;
	private Integer maxConnectionLifeTime = 27107;
	private Integer connectTimeout = 27107;
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public Integer getPort() {
		return port;
	}
	public void setPort(Integer port) {
		this.port = port;
	}
	public Integer getConnectionsPerHost() {
		return connectionsPerHost;
	}
	public void setConnectionsPerHost(Integer connectionsPerHost) {
		this.connectionsPerHost = connectionsPerHost;
	}
	public Integer getMaxWaitTime() {
		return maxWaitTime;
	}
	public void setMaxWaitTime(Integer maxWaitTime) {
		this.maxWaitTime = maxWaitTime;
	}
	public Integer getSocketTimeout() {
		return socketTimeout;
	}
	public void setSocketTimeout(Integer socketTimeout) {
		this.socketTimeout = socketTimeout;
	}
	public Integer getMaxConnectionLifeTime() {
		return maxConnectionLifeTime;
	}
	public void setMaxConnectionLifeTime(Integer maxConnectionLifeTime) {
		this.maxConnectionLifeTime = maxConnectionLifeTime;
	}
	public Integer getConnectTimeout() {
		return connectTimeout;
	}
	public void setConnectTimeout(Integer connectTimeout) {
		this.connectTimeout = connectTimeout;
	}
	
}

第二步  在monog資料來源的配置類裡使用該配置類

@Configuration
@EnableConfigurationProperties(ManageMongoProperties.class)//這個註解必須加 1.5版本之前的寫        
                                                           //法 有點開啟配置類例項化的意思
public class ManageMongoConfig {
	@Autowired
	private ManageMongoProperties cfg ;//注入配置類
	@Bean(name = "manageMongoClient")
	public MongoClient manageMongoTemplate() throws Exception {
		MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
		builder.connectionsPerHost(cfg.getConnectionsPerHost());//使用配置類
		builder.maxWaitTime(cfg.getMaxWaitTime());
		builder.maxConnectionLifeTime(cfg.getMaxConnectionLifeTime());
		builder.connectTimeout(cfg.getConnectTimeout());
		MongoClientOptions mongoClientOptions = builder.build();
		List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
		ServerAddress serverAddress = new ServerAddress(cfg.getHost(), cfg.getPort());
        serverAddresses.add(serverAddress);
		MongoClient mongoClient = new MongoClient(serverAddress,mongoClientOptions);
		return mongoClient;
	}
}

也可以在controller裡注入該配置類

1.5版本以後的寫法

配置類作為組建了,必須加入@Componnet註解,且@ConfigurationProperties取消location屬性,想指定配置檔案位置的話

那就使用@PropertySource ,monog資料來源的配置類裡使用該配置類,取消@EnableConfigurationProperties註解,直接注入即可