一:EnvironmentPostProcessor接口的使用,使用該接口,可以實現(xiàn)動態(tài)(就是根據(jù)業(yè)務邏輯)的加載properties文件。
創(chuàng)新互聯(lián)是一家專業(yè)提供劍河企業(yè)網站建設,專注與成都網站建設、網站建設、html5、小程序制作等業(yè)務。10年已為劍河眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網站制作公司優(yōu)惠進行中。
@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("獲取到的參數(shù)是:"+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支持數(shù)組和集合的配置;
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;
/**
* 支持獲取數(shù)組,集合
* 配置方式為: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
*
Title: TomcatProperties
*
Description:
*
Company: www.itcast.cn
* @version 1.0
*/
@Component
@ConfigurationProperties("ds")
public class TomcatProperties {
private List
private String[] ports;
public String[] getPorts() {
return ports;
}
public void setPorts(String[] ports) {
this.ports = ports;
}
public List
return hosts;
}
public void setHosts(List
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;
/** 方式一:
* 啟動參數(shù)來控制生效的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
的命名
*
Title: App2
*
Description:
*
Company: www.itcast.cn
* @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")來使用