springBoot 1.x和2.x 获取application.yml参数的方法

Posted 栗子~~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springBoot 1.x和2.x 获取application.yml参数的方法相关的知识,希望对你有一定的参考价值。

文章目录

前言

  如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。
  而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!


springBoot 1.x和2.x 获取application.yml参数的方法

对于springBoot来说不同版本、写法不太一样

01 springBoot 1.x 版本

@Component
@Order(1)
public class StartUpRunnerTest implements CommandLineRunner 
    @Autowired
    Environment environment;
    @Override
    public void run(String... strings) throws Exception 
        RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource.");
        Map<String, Object> subProperties = propertyResolver.getSubProperties(null);
        String username = propertyResolver.getProperty("username", String.class);
        System.out.printf(username);
    

02 升级springBoot 2.x 版本后RelaxedPropertyResolver不可用的替代方式

Iterable<ConfigurationPropertySource> sources = ConfigurationPropertySources.get(environment);
Binder binder = new Binder(sources);
BindResult<Properties> bindResult = binder.bind("spring.thymeleaf",Properties.class);
Properties properties = bindResult.get();

以上是关于springBoot 1.x和2.x 获取application.yml参数的方法的主要内容,如果未能解决你的问题,请参考以下文章

关于SpringBoot 1.x和2.x版本差别

idea spring boot 1.x junit单元测试

再见 Spring Boot 1.X ,Spring Boot 2.X 走向舞台中心

多数据源 Spring 1.x 和 spring 2.x区别

springboot整合feign

《SpringBoot静态资源访问》