除了之前的配置读取方式,springboot还默认读取application.yml
首先在resource下创建application.yml
在文件里添加如下内容:
myProps: #自定义的属性和值
simpleProp: simplePropValue
arrayProps: 1,2,3,4,5
listProp1:
- name: abc
value: abcValue
- name: efg
value: efgValue
listProp2:
- config2Value1
- config2Vavlue2
mapProps:
key1: value1
key2: value2
创建个类叫myProps:
package com.gcx.spring.springboot2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="myProps") //接收application.yml中的myProps下面的属性
public class MyProps {
private String simpleProp;
private String[] arrayProps;
private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的属性值
private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的属性值
private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的属性值
public String getSimpleProp() {
return simpleProp;
}
//String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要
public void setSimpleProp(String simpleProp) {
this.simpleProp = simpleProp;
}
public List<Map<String, String>> getListProp1() {
return listProp1;
}
public List<String> getListProp2() {
return listProp2;
}
public String[] getArrayProps() {
return arrayProps;
}
public void setArrayProps(String[] arrayProps) {
this.arrayProps = arrayProps;
}
public Map<String, String> getMapProps() {
return mapProps;
}
public void setMapProps(Map<String, String> mapProps) {
this.mapProps = mapProps;
}
}
创建一个Test
package com.gcx.spring.springboot2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Test {
@Autowired
private MyProps myProps;
public void propsTest() {
System.out.println("simpleProp: " + myProps.getSimpleProp());
System.out.println("arrayProps: " +myProps.getArrayProps());
System.out.println("listProp1: " + myProps.getListProp1());
System.out.println("listProp2: " + myProps.getListProp2());
System.out.println("mapProps: " + myProps.getMapProps());
}
}
创建一个主方法运行:
package com.gcx.spring.springboot2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App2.class, args);
/*System.out.println(context.getEnvironment().getProperty("jdbc.name"));*/
context.getBean(Test.class).propsTest();
//System.out.println(context.getEnvironment().getProperty("springboot.name"));
context.close();
}
}
springboot还提供EnvironmentPostProcessor接口来读取配置文件
首先创建MyEnvironmentPostProcessor实现这个EnvironmentPostProcessor
package com.gcx.spring.springboot2;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.stereotype.Component;
@Component
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor{
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
try(InputStream input=new FileInputStream("E:/test/springboot.properties")){
Properties source =new Properties();
source.load(input);
PropertiesPropertySource propertySource =new PropertiesPropertySource("my", source);
environment.getPropertySources().addLast(propertySource);
}catch(IOException e){
e.printStackTrace();
}
}
}
从上面代码看出需要读取一个配置文件,E:/test/springboot.properties
在resource下创建一个META-INF,在这个文件夹下创建spring.factories文件(因为这是人家规定好的)
在spring.factories中添加如下配置
运行如下:
在开发中会有多中环境,我们模拟测试,开发,默认环境
创建三个文件分别是:
每个文件对应的代码
dev
test
默认
当我们不选择这个时会显示
去掉注释后
这样就可以实现一个简单的环境切换
如果嫌代码方式麻烦,可以换一个参数来配置