I want to configure my WebConfig bean to have different CORS origin depending on the profileapplication.properties
.
For some reason the value in my application-local.properties
is not injected into the field of my WebConfig bean.
When I use @Value
here it will work (please check last headline for the working example)
Entry Class
@SpringBootApplication
@EnableEurekaClient
@ComponentScan("com.company.coma")
public class EventWebApplication {
public static void main(String[] args) {
SpringApplication.run(EventWebApplication.class, args);
}
}
WebConfig.java
@Configuration
@ConfigurationProperties("coma.cors")
public class WebConfig extends WebMvcConfigurerAdapter {
private String origin;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping(Resources.Services.APIs.Event.GET_STATUS + "/**")
.allowedOrigins(origin);
}
}
application-local.properties
coma.cors.origin=http://localhost:8080
Execution
starting the application with -Dspring.profiles.active=local
Result
Working example
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Value("${coma.cors.origin}") // this works
private String origin;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping(Resources.Services.APIs.Event.GET_STATUS + "/**")
.allowedOrigins(origin);
}
}