Spring Learning (ix)-----Spring Dependency Check

Source: Internet
Author: User

In spring, you can use the Dependency check feature to ensure that the required properties can be set or injected. Dependency Check mode 4 dependent check supported mode:
    • None – does not rely on check, which is the default mode.
    • simple– if the base type (int, long,double ...) and collection type (map, list..) Any of the properties are not set, Unsatisfieddependencyexception will be thrown.
    • objects– if any property of the object type is not set, Unsatisfieddependencyexception will be thrown.
    • all– if any of the properties of any type are not set, Unsatisfieddependencyexception will be thrown.

Note: The default mode is None

Example of an example customer and person object.
 PackageCom.yiibai.common; Public classCustomer {Privateperson person ; Private inttype; PrivateString Action; //getter and Setter methods} PackageCom.yiibai.common; Public classPerson {PrivateString name; PrivateString address; Private intAge ; //getter and Setter methods}


1. None depends on checking the spring bean configuration file using "None" Dependency check mode.
<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 ">    class=" Com.yiibai.common.Customer ">        <property name=" action "value=" buy "/>    </bean>     Class= "Com.yiibai.common.Person" >        <property name= "name" value= "Yiibai"/>        <property Name= "Address" value= "address ABC"/>        <property name= "age" value= "/> </bean></beans"    >

If there is no explicitly defined dependency check pattern, it defaults to "none". No dependency checks will be performed. 2. Simple dependency Check the spring bean's configuration file using the "simple" dependency check mode.
<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 "><bean id= "Customerbean"class= "Com.yiibai.common.Customer"Dependency-check= "Simple" > <property name= "person" ref= "Personbean"/> <property name= "action" value= "buy" /> </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>

In the "Type" property (base type or collection type), as has not been set, Unsatisfieddependencyexception will be thrown.
' Customerbean 'class path resource [config/spring-' type 'this for   this bean.

3. Objects relies on checking the "objects" dependency check mode of the spring bean configuration file.
<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 "><bean id= "Customerbean"class= "Com.yiibai.common.Customer"Dependency-check= "Objects" > <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" va Lue= "Address ABC"/> <property name= "age" value= "/> </bean> </beans>"

In the ' Person ' property (object type), not yet set, a unsatisfieddependencyexception will be thrown.
' Customerbean 'class path resource [config/spring-' person 'this for   this bean.


4. All relies on checking the "all" dependency check mode of the spring bean configuration file.
<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 ">    class=" Com.yiibai.common.Customer "          dependency-check=" All ">        <property name=" action "value=" buy " />    </bean>    class= "Com.yiibai.common.Person" >        <property name= "name" Value= "Yiibai"/>        <property name= "Address" value= "address ABC"/>        <property name= "age" value= " "/>    </bean></beans>

For the combination of "simple" and "objects" modes, if any of the properties of the type (prototypes, collections, and objects) are not set, a unsatisfieddependencyexception will be thrown. The global default dependency check is a well-defined dependency check pattern, and each bean configuration is cumbersome and error-prone, and you can set a default dependency check property in the <beans> root element to enforce this rule for the entire Bean class declared within the root element <beans>. However, this default mode is overwritten by the pattern specified by a bean itself.
<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 "   default-dependency-check= "All"> <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>"

In this configuration file, all the bean classes are declared as "all" dependent check mode. @Required Annotations
In most cases, you only need to make sure that a particular property has been set, but there must be all the types (original, collection, or object) properties.

Spring Learning (ix)-----Spring 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.