[英]Spring Bean Configuration: How to mark beans as mandatory/optional?
I am making heavy use of Springs XML Configuration for Java Beans. 我大量使用Java Bean的Springs XML配置。 Is there a best practice to mark a property of a bean as optional or required? 是否有最佳实践将bean的属性标记为可选或必需? I am currently initializing the optional beans in the default constructor. 我目前正在初始化默认构造函数中的可选bean。 If one of the required properties it not set in most cases a NullPointerException will be thrown, but that doesn't really seem to be a good solution. 如果大多数情况下未设置必需属性之一,则将抛出NullPointerException,但这似乎并不是一个好的解决方案。
Sincereley, Heinrich 海因里希(Sincereley)
您可以在必填字段上使用@Required
批注。
如果未使用AOP,则处理强制属性的一种方法是将bean类声明为实现InitializingBean
,并测试是否已在afterPropertiesSet()
方法中设置了所有强制属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.