如何在不使用@PropertySources批注的情况下使用@ConfigurationProperties(prefix =“ foo.bar”)获取前缀属性

我正在尝试在我的spring-boot应用程序中读取属性。我想读取所有带有前缀“ foo.bar”的属性

我尝试了下面的代码,并且工作正常。

@Configuration
@PropertySources({ @PropertySource(" file:${property.location}/somefile.properties") })
public class Someclass{
    @Bean
    @ConfigurationProperties(prefix = "foo.bar")
    public Properties getProperties() {
        return new Properties();
    }
}

但是现在由于某些限制,我不想使用 @PropertySource ,并且我希望我的prefix-properties与上面的结果相同。 现在,我使用PropertyPlaceholderConfigurer而不是@PropertySource来加载属性,如下面的代码所示-

@Bean
public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer() {
    PropertyPlaceholderConfigurer pC = new PropertyPlaceholderConfigurer();
    Resource[] resources = new 
              PathMatchingResourcePatternResolver().getResources(some-path);
     pC.setLocations(array);
     return pC;
}

请建议是否有一种无需使用@PropertySource即可加载这些前缀属性的方法?

zy89380035 回答:如何在不使用@PropertySources批注的情况下使用@ConfigurationProperties(prefix =“ foo.bar”)获取前缀属性

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3152968.html

大家都在问