Spring relies on checking that the bean profile is used to determine that all properties of a particular type (base, collection, or object) are set. In most cases, you only need to make sure that a specific property has been set but not all properties: In this case, you need to @Required annotations, see the example below: @Required example Customer object, applicable @required in the Setperson () method to ensure that the person property is set.
Package Com.yiibai.common;import Org.springframework.beans.factory.annotation.required;public class Customer { private person person;private int type;private String action;public person Getperson () {return person;} @Requiredpublic void Setperson (person person) {This.person = person;}}
Simply applying @required annotations does not enforce the checking of this property, and you also need to register a requiredannotationbeanpostprocessor to understand @required annotations in the bean configuration file. Requiredannotationbeanpostprocessor can be enabled in two ways. 1. Envelope <context:annotation-config/> Add the Spring context and <context:annotation-config/> In the bean configuration file.
<beans ... xmlns:context= "Http://www.springframework.org/schema/context" ... http://www.springframework.org/ Schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd ">...<context: Annotation-config/>...</beans>
A complete example,
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:context=" Http://www.springframework.org/schema/context "xsi:schemalocation="/HTTP/ www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp:// Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd " ><context:annotation-config/><bean id= "Customerbean" class= "Com.yiibai.common.Customer" >< Property name= "Action" value= "buy"/><property name= "type" value= "1"/></bean><bean id= "Personbean" class= "Com.yiibai.common.Person" ><property name= "name" value= "Yiibai"/><property name= "Address" value= "Address ABC"/><property name= "Age" value= "/></bean></beans>"
2. Package letter Requiredannotationbeanpostprocessor directly in the Bean configuration file "Requiredannotationbeanpostprocessor".
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans-2.5.xsd "><bean class=" Org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor "/><bean id=" Customerbean "Class=" Com.yiibai.common.Customer "><property name=" action "value=" buy "/><property name=" type "value=" 1 "/></bean><bean id=" Personbean "class=" Com.yiibai.common.Person "><property name=" name "value=" Yiibai "/><property name=" Address "value=" Address ABC "/><property name=" Age "value="/></bean " ></beans>
If you run it, the following error message will be lost because the person's property is not set.
Org.springframework.beans.factory.BeanInitializationException:Property ' person ' was required for bean ' Customerbean '
Conclusion try @required annotations, which is more flexible than relying on the check XML file, because it can be applied to only one specific attribute. Define @required
Please read this article about how to create a new custom @Required-style annotation.
Spring uses @required annotation dependency checking