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.