Spring Learning (ix)-----spring using @required annotation dependency Check

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.