1

I have the following spring component. I would like to find a more elegant way of injecting the config value.

@Component
public class Clazz {

    @Value("${config.value.foo:#{null}}")
    public String foo;

    @PostConstruct
    public validateFoo() throws ConfigException {
        if (foo == null || "".equals(foo)) {
            throw new ConfigException("Please provide config");
        }
    }
}

I use yaml configs. If I don't add #{null} the config name (config.value.foo) will be injected to String. Also, I would like the spring boot app to not start if the config is null or empty.

Is there another annotation that will inject the value from config and throw an exception if the value is not configured or null?

Edit:

As stated in comments @Value default behavior is to throw exception on missing config. I tested this and on a new project with same config it works. If I remove the config value I get: java.lang.IllegalArgumentException: Could not resolve placeholder I suppose I don't get an exception because of some library I imported.

Liviu Stirb
  • 5,876
  • 3
  • 35
  • 40
  • That is already the default behavior if an `@Value` cannot be resolved it will blow up. If that isn't the case something is configured wrong in your setup. – M. Deinum Feb 23 '17 at 09:16
  • @M.Deinum it doesn't throw any exception. I'm using spring boot and yaml configs. It just injects the config name – Liviu Stirb Feb 23 '17 at 09:18
  • Then you are doing something weird in your configuration something broke the default behavior. – M. Deinum Feb 23 '17 at 10:26
  • if you add the yaml file to the question, it would help. @M. Deinum is right, it should throw an exception and fail to start. – Sergio Feb 23 '17 at 13:55
  • @Sergio you are right. tested the config in a new spring-boot project and it works, so it must be something in my project. I think it may be some library I'm using. I will try to add them one by one and see what's triggering this. Thank you – Liviu Stirb Feb 24 '17 at 09:19

1 Answers1

0

This may happen if there is some code like:

PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();
c.setIgnoreUnresolvablePlaceholders(true);

I have no such code in my project so I added a breakpoint in setIgnoreUnresolvablePlaceholders This method was called:

"main@1" prio=5 tid=0x1 nid=NA runnable
  java.lang.Thread.State: RUNNABLE
      at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.setIgnoreUnresolvablePlaceholders(PlaceholderConfigurerSupport.java:178)
      at springfox.documentation.swagger.configuration.SwaggerCommonConfiguration.swaggerProperties(SwaggerCommonConfiguration.java:38)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-1)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

So the issue was triggered by springfox. I also disabled swagger (removed @EnableSwagger2) from my project and then @Value worked as expected and threw Exception on missing config

Liviu Stirb
  • 5,876
  • 3
  • 35
  • 40