springboot的propeties配置高级应用

一:EnvironmentPostProcessor接口的使用,使用该接口,可以实现动态(就是根据业务逻辑)的加载properties文件。

@Component

public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

@Override

public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {

try(InputStream inputStream = new FileInputStream(“E:/tmp/springboottest.properties”)){

Properties source = new Properties();

source.load(inputStream);

PropertiesPropertySource propertySource = new PropertiesPropertySource(“my”, source);

environment.getPropertySources().addLast(propertySource);

}catch (IOException e) {

e.printStackTrace();

}

}

}

然后获取配置文件中的内容:

public static void main(String[] args) {

ConfigurableApplicationContext context = SpringApplication.run(App.class, args);

context.getBean(Runnable.class).run();

//String property = context.getEnvironment().getProperty(“local.ip”);

//System.out.println(“获取到的参数是:”+property);

//context.getBean(UserConfig.class).show();

//context.getBean(Dbconfig.class).show();

//context.getBean(DataSourceProperties.class).show();

System.out.println(context.getBean(TomcatProperties.class));

System.out.println(context.getEnvironment().getProperty(“spring.boot.name”));

context.close();

}

二:springboot支持数组和集合的配置;

package com.zcp.springstart;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;

/**

 * 支持获取数组,集合

 * 配置方式为:name[index]=value

 * 在application.properties文件中的写法:

 * data.url=127.0.0.1

data.port=1234

data.name=123

data.password=root.post

ds.hosts[0]=128.128.128.0

ds.hosts[1]=128.128.128.1

ds.hosts[2]=128.128.128.2

ds.hosts[3]=128.128.128.3

ds.ports[0]=8888

ds.ports[1]=8889

ds.ports[2]=8890

ds.ports[3]=8891

ds.ports[4]=8892

 * <p>Title: TomcatProperties</p>

 * <p>Description: </p>

 * <p>Company: www.itcast.cn</p> 

 * @version 1.0

 */

@Component

@ConfigurationProperties(“ds”)

public class TomcatProperties {

private List<String> hosts = new ArrayList<>();

private String[] ports;

public String[] getPorts() {

return ports;

}

public void setPorts(String[] ports) {

this.ports = ports;

}

public List<String> getHosts() {

return hosts;

}

public void setHosts(List<String> hosts) {

this.hosts = hosts;

}

@Override

public String toString() {

return “TomcatProperties [hosts=” + hosts + “, ports=” + Arrays.toString(ports) + “]”;

}

}

三:读取的application-*.properties配置信息

package com.zcp.springstart;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.ConfigurableApplicationContext;

/** 方式一:

 * 启动参数来控制生效的profile.可以同时激活多个profile

 * –spring.profiles.active=dev,test

 * 

 * 方式二:

 * 

 * SpringApplication springApplication = new SpringApplication(App2.class);

//springApplication.setAdditionalProfiles(“dev”);

springApplication.setAdditionalProfiles(“dev”,”test”);

说明:

application-dev.properties

application-test.properties

的命名

 * <p>Title: App2</p>

 * <p>Description: </p>

 * <p>Company: www.itcast.cn</p> 

 * @version 1.0

 */

@SpringBootApplication

public class App2 {

public static void main(String[] args) {

SpringApplication springApplication = new SpringApplication(App2.class);

//springApplication.setAdditionalProfiles(“dev”);

springApplication.setAdditionalProfiles(“dev”,”test”);

ConfigurableApplicationContext context = springApplication.run(args);

System.out.println(“>>>>>>>>>”);

System.out.println(context.getEnvironment().getProperty(“db123.url”));

System.out.println(context.getEnvironment().getProperty(“db123.url2”));

System.out.println(“>>>>>>>>>>.”);

context.close();

}

}

如果是类或者方法,可以通过注解@Profile(“test”)来使用

原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/198005.html

(0)
上一篇 2021年11月17日
下一篇 2021年11月17日

相关推荐

发表回复

登录后才能评论