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 & Arrow " /> <Constructor-argtype= "Double"value= " the" /></Bean>
Spring (3.2.3)-Beans (2): Attribute Injection & Construction Injection