繁体   English   中英

检索 YML / YAML 属性

[英]Retrieve YML / YAML properties

我有一个外部 yaml 属性文件,我已经加载并且想要检索。 但是获得 YAML 的建议方法是这样的:

@Value("${some.var}");

这不起作用。

我正在像这样加载文件:

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {       
    String userHome = System.getProperty('user.home');
    ArrayList<String> locations = new ArrayList<String>(
            Arrays.asList(
                    "${userHome}/.boot/beapi_server.yml",
                    "${userHome}/.boot/beapi.yml",
                    "${userHome}/.boot/beapi_db.yml",
                    "${userHome}/.boot/beapi_api.yml"
            )
    );
    Collections.reverse(locations);
    
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    for (String location : locations) {
        String finalLocation = location.toString();
    
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new FileSystemResource(finalLocation));
            propertySourcesPlaceholderConfigurer.setProperties(Objects.requireNonNull(yaml.getObject()));
    }
    return propertySourcesPlaceholderConfigurer;
}

执行器/configprops 也不显示属性。

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

好的,所以我想通了,并想发布答案供其他人查看。

将外部文件加载到您的 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 分配给上面 class 中的 ArrayList JvmArgs。 简单的。

所以,一旦你知道如何,相当简单而优雅的解决方案......但知道诀窍不是:)

我也在使用 yaml 配置。 在我的例子中,你必须创建一个配置 class 然后你可以自动装配它。

这是我的代码:

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;
}

应用程序-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,你可以 go 到这个链接spring-yaml

希望这有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM