Springboot读取(多个)yml文件

#application.yml 文件
wechat:
  mp:
    configs:
    - appId: appid1
      secret: arr1_secret
      token: arr1_token
      aesKey: arr1_key
    - appId: appid2
      secret: arr2_secret
      token: arr2_token
      aesKey: arr2_key
server:
  port: 8989
  servlet:
    #context-path: /SpringDemo/
    context-path: /  
spring: 
  profiles: 
   active: demo  #引入另外一个同路径下yml配置文件,该配置文件必须以application-开头
#application-demo.yml文件内容
props: #自定义的属性和值  
  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
//读取配置文件之一:WxMpProperties 
package com.example.demo.beans;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lombok.Data;

//@Data
@Component
@ConfigurationProperties(prefix = "wechat.mp")
public class WxMpProperties {

	private List configs = new ArrayList();

	public List getConfigs() {
		return configs;
	}

	public void setConfigs(List configs) {
		this.configs = configs;
	}

	// @Data
	public static class MpConfig {

		private String appId;
		private String secret;
		private String token;
		private String aesKey;

		public String getAppId() {
			return appId;
		}

		public void setAppId(String appId) {
			this.appId = appId;
		}

		public String getSecret() {
			return secret;
		}

		public void setSecret(String secret) {
			this.secret = secret;
		}

		public String getToken() {
			return token;
		}

		public void setToken(String token) {
			this.token = token;
		}

		public String getAesKey() {
			return aesKey;
		}

		public void setAesKey(String aesKey) {
			this.aesKey = aesKey;
		}
	}
}
//读取配置文件之二:YmlConfig
package com.example.demo.beans;

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="props")
public class YmlConfig {  
    private String simpleProp;    
    private String[] arrayProps;    
    private List> listProp1 = new ArrayList<>(); 
    private List listProp2 = new ArrayList<>(); 
    private Map mapProps = new HashMap<>();
        
    public String getSimpleProp() {    
        return simpleProp;    
    }    
        
    public void setSimpleProp(String simpleProp) {    
        this.simpleProp = simpleProp;    
    }    
        
    public List> getListProp1() {    
        return listProp1;    
    }    
    public List getListProp2() {    
        return listProp2;    
    }    
    
    public String[] getArrayProps() {    
        return arrayProps;    
    }    
    
    public void setArrayProps(String[] arrayProps) {    
        this.arrayProps = arrayProps;    
    }    
    
    public Map getMapProps() {    
        return mapProps;    
    }    
    
    public void setMapProps(Map mapProps) {    
        this.mapProps = mapProps;    
    }   
}  
package com.example.demo;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;

import com.example.demo.beans.WxMpProperties;
import com.example.demo.beans.YmlConfig;
import com.example.demo.beans.WxMpProperties.MpConfig;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@SpringBootTest
public class ReadApplicationYmlTests {


	@Value("${server.port}")
	private String port;
	
	@Value("${server.port}")
	private static String port2;
	
    @Autowired
    private Environment env;
	
	@Autowired
	private WxMpProperties properties;
	
	@Autowired
	private YmlConfig ymlConfig;
	
	@Test
	void contextLoads() {
		
		System.out.println(port);//-1   未取到值
		System.out.println(env.getProperty("server.port"));//-1   未取到值
		System.out.println(port2);//-1   未取到值
		//@RestController 控制层可以成功获取正确值 8989
		
		List configs = properties.getConfigs();
		System.out.println(configs.get(0).getAppId() + "--" + configs.get(0).getAesKey());
	}
	
	@Test
    public void testDisplayYmlValue() throws JsonProcessingException {  
        System.out.println("simpleProp: " + ymlConfig.getSimpleProp());    
          
        ObjectMapper objectMapper = new ObjectMapper();  
        System.out.println("arrayProps: " + objectMapper.writeValueAsString(ymlConfig.getArrayProps()));    
        System.out.println("listProp1: " + objectMapper.writeValueAsString(ymlConfig.getListProp1()));    
        System.out.println("listProp2: " + objectMapper.writeValueAsString(ymlConfig.getListProp2()));    
        System.out.println("mapProps: " + objectMapper.writeValueAsString(ymlConfig.getMapProps()));    
  
    }
}

 

你可能感兴趣的:(SpringBoot系列)