如何从 Java 中的 application.yml 文件中读取

coc*_*Pha 1 spring yaml spring-mvc spring-boot

我写了一个电子邮件程序。但是我不得不更改一些配置,所以在我的项目中我需要知道如何从 yaml 读取我的 loginUrl

我的代码如下;

userNotificationEmail.setIsActive("F");
                                userNotificationEmailRepository.save(userNotificationEmail);
                                activationResultType.setResultCode(ActivationResultCode.SUCCESSFUL);
                                activationResultType.setUser(userMapper.fromUser(userEntity));
                                activationResultType.setLoginUrl(loginUrl); 
                                return activationResultType;
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 5

如果您的密钥是loginUrl(在您的 yaml 文件中),您可以@Value在 Spring 组件中使用注解注入其值。

@Value("${loginUrl}")
private String loginUrl;
Run Code Online (Sandbox Code Playgroud)

如果是二级属性,则路径为@Value("${yourFirstKey.loginUrl}").

有关使用 Spring Boot 读取 YAML 文件的更多文档