springboot加载⾃定义yml⽂件Springboot加载⾃定义yml⽂件配置的⽅法
ConfigurationProperties注解的locations属性在1.5.X以后没有了,不能指定locations来加载yml⽂件
PropertySource注解不⽀持yml⽂件加载,详细见官⽅⽂档:
Spring Framework有两个类加载YAML⽂件,YamlPropertiesFactoryBean和YamlMapFactoryBean
spring怎么读取yaml可以通过PropertySourcePlaceholderConfigurer来加载yml⽂件,暴露yml⽂件到spring environment
/**
* @author WGR
* @create 2020/7/24 -- 15:31
*/
@Configuration
public class SpringBootConfigura {
// 加载YML格式⾃定义配置⽂件
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("l"));//File引⼊
configurer.Object());
return configurer;
}
}
配置⽂件:
my:
servers:
- ample
- ample
-
${random.value}
测试
@Controller
public class SpringBootTest {
@Autowired
Config config;
@GetMapping("/testConfig")
@ResponseBody
public String testConfig(){
Servers().toString();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论