Spring Learning (ix)-----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.
PackageCom.yiibai.common;Importorg.springframework.beans.factory.annotation.Required; Public classCustomer {Privateperson person ; Private inttype; PrivateString Action; PublicPerson Getperson () {returnPerson ; } @Required Public voidSetperson (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/context http:// 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/beans http://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" Valu e= "1"/> </bean> <bean id= "Personbean"class= "Com.yiibai.common.Person" > <property name= "name" value= "Yiibai"/> <property name= "Address" va Lue= "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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "><Beanclass= "Org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> <bean id= " Customerbean "class= "Com.yiibai.common.Customer" > <property name= "Action" value= "buy"/> <property name= "type" Valu e= "1"/> </bean> <bean id= "Personbean"class= "Com.yiibai.common.Person" > <property name= "name" value= "Yiibai"/> <property name= "Address" va Lue= "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: 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
Spring Learning (ix)-----spring using @required annotation dependency Check