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 serverAddresses = new ArrayList();
		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注解,直接注入即可

spring boot读取自定义配置类_第1张图片 

 

spring boot读取自定义配置类_第2张图片 

 

spring boot读取自定义配置类_第3张图片 

 

你可能感兴趣的:(spring,boot)