In spring I can retrieve property(defined in properties file) using getProperty
method(of Environment) Eg
@PropertySource("classpath:app.properties")
public class Config{
@Autowired
Environment env;
@Bean
public Foo foo(){
env.getProperty("foo.isEligible")
//.... return foo.
}
}
however if the property is not defined then it returns null
. We could make a null check and throw exception explicitly but is there some built-in method in spring to achieve it, so that application will throw exception if the property requested via getProperty
method is not defined . I am using spring 4.
You can use getRequiredProperty
which throws IllegalStateException if property is undefined. Eg
env.getRequiredProperty("foo");
If you want an error at Spring startup time, if you used something like
@Value("${foo.isEligible}")
protected boolean isFooEligible;
The Spring ApplicationContext
will fail to startup if it can't resolve this SpEL expression
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.