SpringBoot读取自定义配置文件
不引入依赖(spring-boot-configuration-processor)
数组获取方式
准备my.properties
和my.yml
list1=11111,1,2321,4,4325,645,/sahnd/dsauyuw
list2: >
dsadsad,
fdsewrwar,
sdaadsadsadsa
ProConfig
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.List;
/**
* TODO
*
* @author WJM
* @description
* @date 2021/4/10 10:13
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Configuration
@PropertySource("classpath:/my.properties")
public class ProConfig {
@Value("#{'${list1}'.split(',')}")
private List<String> list1;
}
读取自定义yml需要创建一个类
YamlPropertySourceFactory
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/**
* TODO
*
* @author WJM
* @description
* @date 2021/4/10 10:35
*/
public class YamlPropertySourceFactory implements PropertySourceFactory {
//这个方法有2个入参,分别为资源名称和资源对象,这是第一步
@Override
public PropertySource< ? > createPropertySource(String name, EncodedResource resource) throws IOException {
//这是第二步,根据流对象产出Properties对象
Properties propertiesFromYaml = loadYamlIntoProperties(resource);
String sourceName = name != null ? name : resource.getResource().getFilename();
assert sourceName != null;
//这是第三部,根据Properties对象,产出PropertySource对象,放入到Spring中
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
}
private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
try {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
} catch (IllegalStateException e) {
Throwable cause = e.getCause();
if (cause instanceof FileNotFoundException) {
throw (FileNotFoundException) e.getCause();
}
throw e;
}
}
}
YmlConfig
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.List;
/**
* TODO
*
* @author WJM
* @description
* @date 2021/4/10 10:29
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Configuration
@PropertySource(factory =YamlPropertySourceFactory.class,value = "classpath:my.yml")
public class YmlConfig {
@Value("${list2}")
private List<String> list2;
}
Test读取
@SpringBootTest
class MyPropertiesApplicationTests {
@Resource
private ProConfig proConfig;
@Resource
private YmlConfig ymlConfig;
@Test
void contextLoads() {
System.out.println(ymlConfig.toString());
System.out.println(proConfig.toString());
}
}
结果
YmlConfig{list=[dsadsad, fdsewrwar, sdaadsadsadsa]}
ProConfig{list=[11111, 1, 2321, 4, 4325, 645, /sahnd/dsauyuw]}
注:Map暂时搞不清楚,请使用一对一的JavaBean
引入依赖(spring-boot-configuration-processor)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
注:使用@ConfigurationProperties(prefix = “前缀”)需要添加前缀
懒人版配置
新建一个application-my.yml
或者application-my.properties
在application.yml或者application.properties加
yml
spring:
profiles:
include: my
--------------------------------------
properties
spring.profiles.include=my
application-my.yml
yml:
name: 张三
map:
name: sagdwa
age: 18
sex: 0
list3:
- dsajhdsa
- dsjahjdhwa
- dshjgahjghwur
- dsujahuwdw
application-my1.properties
pro.map.name=张三
pro.map.age=18
pro.map.sex=0
pro.list4[0]=hjsahjdhjsa
pro.list4[1]=dwadasfwaf
pro.list4[2]=fsdadwad
pro.list4[3]=ewqrsafsa
ProConfig
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Map;
/**
* TODO
*
* @author WJM
* @description
* @date 2021/4/10 10:13
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "pro")
//@PropertySource("classpath:/my.properties")
public class ProConfig {
private Map<String, String> map;
private List<String> list4;
}
YmlConfig
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Map;
/**
* TODO
*
* @author WJM
* @description
* @date 2021/4/10 10:29
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "yml")
//@PropertySource(factory =YamlPropertySourceFactory.class,value = "classpath:my.yml")
public class YmlConfig {
private Map<String, String> map;
private List<String> list3;
}
Test输出结果
YmlConfig(map={name=sagdwa, age=18, sex=0}, list3=[dsajhdsa, dsjahjdhwa, dshjgahjghwur, dsujahuwdw])
ProConfig(map={name=张三, age=18, sex=0}, list4=[hjsahjdhjsa, dwadasfwaf, fsdadwad, ewqrsafsa])
自定义配置
YML
在读取配置类上加注解
@ConfigurationProperties(prefix = "yml")
指定yml
@PropertySource(factory =YamlPropertySourceFactory.class,value = "classpath:my.yml")
properties
@ConfigurationProperties(prefix = “pro”)
properties不需要factory
@PropertySource(“classpath:/my.properties”)
配置文件写法和上面懒人版一样,相比不引入依赖写法会好看一些,并且可以读取map