springboot的propeties配置高级应用

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

创新互联是一家专业提供剑河企业网站建设,专注与成都网站建设、网站建设html5、小程序制作等业务。10年已为剑河众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

@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

 *

Title: TomcatProperties

 *

Description:

 *

Company: www.itcast.cn

 

 * @version 1.0

 */

@Component

@ConfigurationProperties("ds")

public class TomcatProperties {

private List hosts = new ArrayList<>();

private String[] ports;

public String[] getPorts() {

return ports;

}

public void setPorts(String[] ports) {

this.ports = ports;

}

public List getHosts() {

return hosts;

}

public void setHosts(List 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

的命名

 *

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")来使用


网站标题:springboot的propeties配置高级应用
分享地址:http://scyanting.com/article/gedcgp.html