springboot 3 application yml properties @value

261 Views Asked by At

in my application.yml I have the following

cors-allowed-origins:
  url-origins:
     - http://host.docker.internal:81/
     - http://localhost:8081/*
     - http://digitale.com/
     - http://share.com/

th record

@ConfigurationProperties( "cors-allowed-origins" )
public record CrosAllowedOrigins(List<String> urlOrigins) {

}

Could you please help me out or even tell me what s wrong thanks

in the class where I injected the values

public class SecurityConfig {
    @Value("${cors-allowed-origins.url-origins}")
    private List<String> allowedOrigins;

and I have alredy enabled the properties in the main class

 @EnableConfigurationProperties({ CrosAllowedOrigins.class})

it builds perfectly but when I run the app I get this error :

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'cors-allowed-origins.url-origins' in value "${cors-allowed-origins.url-origins}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) ~[spring-core-6.0.13.jar:6.0.13]
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-6.0.13.jar:6.0.13]
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239) ~[spring-core-6.0.13.jar:6.0.13]
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-6.0.13.jar:6.0.13]
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:200) ~[spring-context-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:918) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1358) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:764) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:747) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.0.13.jar:6.0.13]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:492) ~[spring-beans-6.0.13.jar:6.0.13]
    ... 17 common frames omitted
1

There are 1 best solutions below

0
On

I found a way to fix the issue, so instead of using the @Autowired annotation of Spring with @Value, I injected the values via the constructor, and it works perfectly.

@RequiredArgsConstructor public class SecurityConfig { // @Value("${cors-allowed-origins.url-origins}") private final CrosAllowedOrigins allowedOrigins;