[英]Spring 4 force property to be defined
In spring I can retrieve property(defined in properties file) using getProperty
method(of Environment) Eg 在春季,我可以使用(环境的)
getProperty
方法检索属性(在属性文件中定义)
@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
. 但是,如果未定义属性,则返回
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 . 我们可以显式地进行null检查并抛出异常,但是在spring中是否有一些内置方法可以实现此目的,因此,如果未定义通过
getProperty
方法请求的属性,则应用程序将抛出异常。 I am using spring 4. 我正在使用Spring 4。
You can use getRequiredProperty
which throws IllegalStateException if property is undefined. 您可以使用
getRequiredProperty
,如果属性未定义,则抛出IllegalStateException。 Eg 例如
env.getRequiredProperty("foo");
If you want an error at Spring startup time, if you used something like 如果您想在Spring启动时出错,请使用类似
@Value("${foo.isEligible}")
protected boolean isFooEligible;
The Spring ApplicationContext
will fail to startup if it can't resolve this SpEL expression 如果无法解析此SpEL表达式,Spring
ApplicationContext
将无法启动
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.