我用SpringBoot和創建了多模塊專案Maven。
我已將application.properties檔案更改為application.yml檔案。
Application.yml 檔案在里面 src/main/resources
在服務課上,我正在嘗試做
@Value("${keycloak.realm}")
private String REALM;
我遇到的第一個錯誤是:
Could not resolve placeholder 'keycloak.realm' in value "${keycloak.realm}"
當我將@PropertySource(value = "classpath:application.yml")注釋放置到呼叫@Value注釋的同一服務時,我收到以下錯誤:
class path resource [application.yml] cannot be opened because it does not exist
這是服務類:
@Slf4j
@Service
@PropertySource(value = "classpath:application.yml")
@RequiredArgsConstructor
public class KeyCloakServiceImpl implements KeyCloakService {
@Value("${keycloak.realm}")
private String REALM;
我在這里錯過了什么?
uj5u.com熱心網友回復:
默認情況下,@PropertySource不查找 yaml 檔案。為此,您必須告訴@PropertySource加載具有以下factory屬性的 yaml 檔案:
@PropertySource(value = "classpath:foo.yml", factory = YamlFactory.class)
public class SomeClass() { }
的實作YamlFactory.class可以是:
public class YamlFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource)
throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(encodedResource.getResource());
Properties properties = factory.getObject();
return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
}
}
欲了解更多資訊:https : //www.baeldung.com/spring-yaml-propertysource
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/357282.html
上一篇:在階段定義插件目標
