The relationship between Spring beans

Source: Internet
Author: User

Relationships between beans: inheritance and dependency
Inheritance of bean Configuration

    • Spring allows inheritance of the bean's configuration, the inherited Bean is called the parent bean, and the bean that inherits the parent bean is called the Child Bean.
    • The child bean inherits the configuration from the parent bean, including the Bean's property configuration
    • The child bean can also overwrite the configuration inherited from the parent bean.
    • The parent bean can be configured as a template or as a bean instance, and if you just want to use the parent bean as a model, you can set the abstract property of <bean> to true so that spring will not instantiate the bean
    • Not all attributes in the <bean> element will be inherited. such as: Autowire,abstract, etc.
    • You can also omit the class property of the parent bean, let the child bean specify its own class, and share the same property configuration, but this time the abstract must be set to True


Dependent Bean Configuration
Spring allows the user to set the bean's predecessor-dependent bean through the Depends-on property, which is created before the bean is instantiated by the predecessor-dependent bean.
If the predecessor relies on more than one bean, you can configure the bean's name in the form of a comma, a space

Example:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xmlns:p= "http://www.springframework.org/schema/p"5 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >6     7     <!--abstract Bean:bean A bean with a true attribute, such that the bean cannot be instantiated by the IOC and can only be used for inheritance configuration8 if the class attribute of a bean is not specified, the bean must be an abstract bean9       -Ten     <BeanID= "Address"  One p:city= "Beijing"P:street= "Huilongguan"Abstract= "true"></Bean> A     <!--Bean Configuration Inheritance: Use the bean's parent property to specify which Bean's configuration to inherit from - -     <BeanID= "Address2"class= "Com.yl.autowire.Address"Parent= "Address"></Bean> -      the     <BeanID= "ADDRESS3"class= "Com.yl.autowire.Address"  - Parent= "Address2"P:street= "Wudaokou"></Bean> -          -     <BeanID= "Car"class= "Com.yl.autowire.Car"  + P:brand= "Audi"P:price= "300000"></Bean> -     <!--requires that you have an associated car! when you configure a person In other words, this bean is dependent on the bean of car . - +     <BeanID= "Person"class= "Com.yl.autowire.Person"  A P:name= "Tom"P:address-ref= "Address2"depends-on= "Car"></Bean> at </Beans>

The relationship between Spring beans

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.