Spring boot 检索YML/YAML属性

Spring boot 检索YML/YAML属性,spring-boot,yaml,Spring Boot,Yaml,我有一个外部yaml属性文件,我已经加载,我想检索。但获得YAML的建议方法如下: @Value(“${some.var}”); 这不管用 我正在加载文件,如下所示: @Bean 公共静态属性资源占位符配置器属性(){ 字符串userHome=System.getProperty('user.home'); ArrayList位置=新的ArrayList( Arrays.asList( “${userHome}/.boot/beapi_server.yml”, “${userHome}/.bo

我有一个外部yaml属性文件,我已经加载,我想检索。但获得YAML的建议方法如下:

@Value(“${some.var}”);
这不管用

我正在加载文件,如下所示:

@Bean
公共静态属性资源占位符配置器属性(){
字符串userHome=System.getProperty('user.home');
ArrayList位置=新的ArrayList(
Arrays.asList(
“${userHome}/.boot/beapi_server.yml”,
“${userHome}/.boot/beapi.yml”,
“${userHome}/.boot/beapi_db.yml”,
“${userHome}/.boot/beapi_api.yml”
)
);
收藏。反向(位置);
PropertySourcesPlaceholderConfigurer PropertySourcesPlaceholderConfigurer=新的PropertySourcesPlaceholderConfigurer();
用于(字符串位置:位置){
字符串finalLocation=location.toString();
YamlPropertiesFactoryBean yaml=新的YamlPropertiesFactoryBean();
setResources(新文件系统资源(finalLocation));
propertySourcesPlaceholderConfigurer.setProperties(Objects.requireNonNull(yaml.getObject());
}
返回属性资源占位符配置器;
}
执行器/ConfigProp也不显示属性


我做错了什么?有点沮丧。

我也在使用yaml配置。在我的例子中,您必须创建一个配置类,然后才能自动连接它

这是我的代码:

PdfConfig.java

@Component
@ConfigurationProperties(prefix = "pdf")
@EnableConfigurationProperties
public class PdfConfig {

    private String licensePath;
    private Watermark watermark;

    // setter getter goes here

}
PdfServiceImpl.java

@Service
public class PdfServiceImpl implements PdfService {

    @Autowired
    private PdfConfig pdfConfig;
}
application-local.yml

#### Pdf Config ####
pdf:
  license-path: classpath:license/development/itextkey.xml
  watermark:
    image-path: classpath:static/img/logo.png
    opacity: 0.2f
    enabled: true
如果您想了解如何设置spring yaml,可以访问此链接


希望这有帮助

好的,所以我想把答案贴出来让其他人看

将外部文件加载到PropertySources后,您现在可以通过引用YML结构前缀头通过@ConfigurationProperties注释访问它们,然后直接通过赋值访问属性

// 'tomcat' if the top level prefix in my yml file
@ConfigurationProperties(prefix="tomcat")
class something{

    ArrayList jvmArgs

}
现在让我们看看我的yml:

tomcat:
    jvmArgs:
        -'-Xms1536m'
        -'-Xmx2048m'
        -'-XX:PermSize=256m'
        -'-XX:MaxPermSize=512m'
        -'-XX:MaxNewSize=256m'
        -'-XX:NewSize=256m',
        -'-XX:+CMSClassUnloadingEnabled'
        -'-XX:+UseConcMarkSweepGC'
        -'-XX:+CMSIncrementalMode'
        -'-XX:+CMSIncrementalPacing'
        -'-XX:CMSIncrementalDutyCycle=10'
        -'-XX:+UseParNewGC'
        -'-XX:MaxGCPauseMillis=200'
        -'-XX:MaxGCMinorPauseMillis=50'
        -'-XX:SurvivorRatio=128'
        -'-XX:MaxTenuringThreshold=0'
        -'-server'
        -'-noverify'
        -'-Xshare:off'
        -'-Djava.net.preferIPv4Stack=true'
        -'-XX:+EliminateLocks'
        -'-XX:+ExplicitGCInvokesConcurrent'
        -'-XX:+UseBiasedLocking'
        -'-XX:+UseTLAB'
我们看到这将直接将值jvmArgs分配给上面类中的ArrayList jvmArgs。简单


所以,非常简单和优雅的解决方案,一旦你知道如何。。。但是知道这是怎么回事,不是吗:)

你把注释放在哪里了?它是在Springbean中的类吗?我在MVCConfig类(标记为@Configuration)和应用程序类中进行了尝试。两者都不会显示属性此处缺少很多属性-例如,bean的配置为
@ConfigurationProperties
。此外,OP正在加载自定义的附加YAML,而您的(不完整的)示例主要关注
application.YAML
。啊,我的错。忘了包括注释。我将编辑我的代码。谢谢伙计@Boristeider