Spring (3.2.3)-Beans (2): Attribute Injection & Construction Injection

Source: Internet
Author: User

Dependency Injection is the process of running a program that requires additional object collaboration (accessing its properties or calling its methods) without creating the callee in the code, but rather relying on the injection of the outer container.

Attribute injection (Setter injection)

Attribute injection refers to an IoC container that uses a setter method of bean properties to inject an instance of bean dependency. Using attribute injection should provide a setter method for the Bean with an argument-free constructor and property.

<BeanID= "Magicwand"class= "Com.huey.dream.bean.Weapon">    < Propertyname= "type"value= "Magic Wand" />    < Propertyname= "weightkg"value= "9.5" /></Bean>

Construction Injection (Constructor injection)

Construct injection refers to an IoC container that uses a bean constructor to inject an instance of bean dependency. Using construct injection should provide at least one constructor with a parameter for the Bean.

Match according to the name of the parameter
<BeanID= "Pistol"class= "Com.huey.dream.bean.Weapon" >        <Constructor-argname= "type"value= "Pistol" />        <Constructor-argname= "weightkg"value= "1.2" /></Bean>
Match according to parameter position
<BeanID= "Sword"class= "Com.huey.dream.bean.Weapon" >    <Constructor-argIndex= "0"value= "Sword" />    <Constructor-argIndex= "1"value= "19.8" /></Bean>
Match by parameter type
<BeanID= "Bow"class= "Com.huey.dream.bean.Weapon" >    <Constructor-argtype= "String"value= "Bow &amp; Arrow " />    <Constructor-argtype= "Double"value= " the" /></Bean>

Spring (3.2.3)-Beans (2): Attribute Injection & Construction Injection

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.