ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring-PropertiesFactoryBean可以从application.yml读取值吗?

2019-11-22 06:30:36  阅读:149  来源: 互联网

标签:spring-java-config spring-boot spring


我的项目具有一个依赖项,该依赖项需要一个可通过@Value批注读取的属性对象集:

@Value("#{myProps['property.1']}")

为此,我正在使用以下命令:

@Bean(name="myProps")
public static PropertiesFactoryBean mapper() {
    PropertiesFactoryBean bean = new PropertiesFactoryBean();
    bean.setLocation(new ClassPathResource("myprops.properties"));
    return bean;
}

这按预期工作.我的属性如下所示:

property.1=http://localhost/foo/bar
property.2=http://localhost/bar/baz
property.3=http://localhost/foo/baz

我正在为此项目使用Spring Boot,因此,我希望能够执行以下操作:

myprops.properties:
    property.1=${base.url}/foo/bar
    property.2=${base.url}/bar/baz
    property.3=${base.url}/foo/baz

然后,我可以基于不同的配置文件配置base.url.

application.yml:

    base:
      url: http://localhost

    ---
    spring:
      profiles: staging
    base:
      url: http://staging

    ---
    spring:
      profiles: production
    base:
      url: http://production

我已经尝试这样做,但是它不起作用.解决方法是,我创建了三个不同的.properties文件(myprops.properties,myprops-staging.properties等),并使用三个不同的@Configuration类加载了它们.这可行,但看起来很麻烦.

@Configuration
public class DefaultConfiguration {

    @Bean(name="myProps")
    public static PropertiesFactoryBean mapper() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new ClassPathResource("myprops.properties"));
        return bean;
    }
}

@Configuration
@Profile("staging")
public class StagingConfiguration {

    @Bean(name="myProps")
    public static PropertiesFactoryBean mapper() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new ClassPathResource("myprops-staging.properties"));
        return bean;
    }
}

@Configuration
@Profile("production")
public class ProductionConfiguration {

    @Bean(name="myProps")
    public static PropertiesFactoryBean mapper() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new ClassPathResource("myprops-production.properties"));
        return bean;
    }
}

是否可以将我的PropertiesFactoryBean配置为从application.yml读取值?如果不是,是否有更简单的方法来使用JavaConfig配置属性?

解决方法:

我最终以编程方式完成了此操作,它使我可以找到想要的行为:

@Value("${base.url}")
private String baseUrl;

@Bean(name = "myProps")
public PropertiesFactoryBean mapper() throws IOException {
    PropertiesFactoryBean bean = new PropertiesFactoryBean();
    bean.setLocation(new ClassPathResource("myprops.properties"));
    bean.afterPropertiesSet();

    // replace ${base.url} in values
    Properties props = bean.getObject();
    Enumeration names = props.propertyNames();

    while (names.hasMoreElements()) {
        String name = names.nextElement().toString();
        String value = props.getProperty(name);
        if (value.contains("${base.url}")) {
            props.setProperty(name, value.replace("${base.url}", baseUrl));
        }
    }

    bean.setLocalOverride(true);
    bean.setProperties(props);
    bean.afterPropertiesSet();

    if (log.isDebugEnabled()) {
        log.debug("Base URL: " + baseUrl);
    }

    return bean;
}

标签:spring-java-config,spring-boot,spring
来源: https://codeday.me/bug/20191122/2057544.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有