"Spring" Construcotrer injection and setter injection in different ways of XML notation

Source: Internet
Author: User
Tags ming

Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka

This paper mainly explains the 4 different ways of writing and sette of constructor injection in spring and 3 different ways to do it.

First, constructor injected 4 different ways

By constructing method injection, the disadvantage of the parameter value set injection to the construction method is that it is impossible to clearly express which attributes are required and which are optional, and the advantage of construct injection is that it is impossible to instantiate incomplete or unusable beans by constructing a forced dependency relationship.

1th method: Direct value of the transfer

<!--constructor method injection notation 1, direct value--><bean id= "Student1" class= "Com.mucfc.beanfactory.Student" >< Constructor-arg value= "Xiao Ming"/><constructor-arg value= "2001"/></bean>
Directly to the parameter assignment, this method is also sorted according to the order, so once the position is swapped, there will be a bug, this method is very primitive

The 2nd method: According to the index assignment, the index is beginning with 0:

<!--constructor Method injection notation 2, according to index assignment--><bean id= "Student2" class= "Com.mucfc.beanfactory.Student" >< Constructor-arg index= "0" value= "a Dog"/><constructor-arg index= "1" value= "2002"/></bean>
The 3rd method is to pass a value based on the owning type

This method is basically not suitable, because a class can have several of the same basic types of variables, it is easy to confuse the value passed to which parameter, so do not use this method:

<!--constructor Method injection notation 3, according to the type of the value--><bean id= "Student3" class= "Com.mucfc.beanfactory.Student" >< Constructor-arg type= "String" value= "white"/><constructor-arg type= "int" value= "2003"/></bean> 
The 4th method: Pass the value according to the name of the parameter: (Recommended usage)

<!--constructor Method injection 4, according to the name of the parameter to pass the value: Remember that the incoming name is the constructor's parameter name (recommended usage)--    <bean id= "Student4" class= " Com.mucfc.beanfactory.Student "><constructor-arg name=" name "value=" Earth "/><constructor-arg name=" id " Value= "4503"/></bean>


In these methods I feel that this method is the most practical, he is based on the name of the value, so basically as long as the name is right, this value can be obtained to

Second, setter injection 3 different ways

1.

<!--setter method injection notation 1, remember to have a parameterless constructor--><bean id= "Student5" class= "Com.mucfc.beanfactory.Student" >< Property Name= "Std_name" ><value> daily </value></property><property name= "std_id" >< Value>2005</value></property></bean>

2.

<!--setter Method injection notation 2, remember to have a parameterless constructor--><bean id= "Student6" class= "Com.mucfc.beanfactory.Student" >< Property Name= "Std_name" value= "Water"/><property name= "std_id" value= "3009"/></bean>

3.

<!--setter Method injection notation 7, remember to have a parameterless constructor  --><bean id= "student7" class= "Com.mucfc.beanfactory.Student" P:std_name = "pin ground" p:std_id= "3445"/>
Recommend the 2nd or 3rd, 1th kind of code to write more, look straight to not so smooth.


Iii. Examples of Use

Create a new Java Project project, name it, and load the spring jar file and the commons-logging jar file in.

I don't know, "Spring" Spring configuration and a spring HelloWorld

Create a new package and add a Student.java with the following code:

/** * Functional test constructor and setter injection in different ways * author Lin Bingwen ([email protected] Blog: Http://blog.csdn.net/evankaka) * Time 2015.4.4 * * Package Com.mucfc.beanfactory;public class Student {private String std_name;private int std_id;      Public Student () {        }public Student (String name,int id) {     std_name=name;     Std_id=id;} Public String Getstd_name () {return std_name;} public void Setstd_name (String std_name) {this.std_name = Std_name;} public int getstd_id () {return std_id;} public void setstd_id (int std_id) {this.std_id = std_id;} Public String toString () {return "Student Name:" +std_name+ "Student Number:" +STD_ID;}}

In the current Project Src folder to add Files Applicationcontext.xml, is the project-"right-" new->other->xml ....

The code is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring- Beans-3.0.xsd "><!--constructor method injection notation 1, direct value--><bean id=" Student1 "class=" Com.mucfc.beanfactory.Student "><constructor-arg value=" Xiao Ming "/><constructor-arg value=" 2001 "/></ bean><!--constructor Method injection notation 2, according to the index assignment--><bean id= "Student2" class= "Com.mucfc.beanfactory.Student" > <constructor-arg index= "0" value= "a Dog"/><constructor-arg index= "1" value= "2002"/></bean><!-- constructor method injection notation 3, according to the type of the value--><bean id= "Student3" class= "Com.mucfc.beanfactory.Student" >< Constructor-arg type= "String" value= "white"/><constructor-arg type= "int" value= "2003"/></bean><!-- constructor method injection notation 4, according to the parametersName value: Remember that the incoming name is the parameter name of the constructor (recommended usage)--<bean id= "Student4" class= "Com.mucfc.beanfactory.Student" >< Constructor-arg name= "name" value= "Earth"/><constructor-arg name= "id" value= "4503"/></bean><!-- Setter Mode injection notation 1, remember to have a parameterless constructor--><bean id= "Student5" class= "com.mucfc.beanfactory.Student" ><property name= " Std_name "><value> daily </value></property><property name=" std_id "><value>2005</ value></property></bean><!--Setter Method injection notation 2, remember to have a parameterless constructor--><bean id= "Student6" class= " Com.mucfc.beanfactory.Student "><property name=" std_name "value=" Water "/><property name=" std_id "value=" 3009 "/></bean><!--Setter Method Injection 7, remember to have a parameterless constructor--><bean id=" student7 "class=" Com.mucfc.beanfactory.Student "p:std_name=" pin ground "p:std_id=" 3445 "/></beans>
Well, the IOC containers are all configured. Here is the use of, directly read the code:

/** * Functional test constructor and setter injection in different ways * author Lin Bingwen ([email protected] Blog: http://blog.csdn.net/evankaka) * Time 2015.4.4 */ Package Com.mucfc.beanfactory;import Org.springframework.beans.factory.xml.xmlbeanfactory;import Org.springframework.core.io.classpathresource;public class Test {public static void main (string[] args) { Xmlbeanfactory bfactory = new Xmlbeanfactory (New Classpathresource ("Applicationcontext.xml")); Student stu1 = (Student) bfactory.getbean ("Student1"); Student STU2 = (Student) bfactory.getbean ("Student2"); Student stu3 = (Student) bfactory.getbean ("Student3"); Student Stu4 = (Student) bfactory.getbean ("Student4"); Student Stu5 = (Student) bfactory.getbean ("student5"); Student Stu6 = (Student) bfactory.getbean ("Student6"); Student Stu7 = (Student) bfactory.getbean ("student7"); System.out.println ("-------------constructor method injected-------------"); System.out.println (STU1); System.out.println (STU2); System.out.println (STU3); System.out.println (STU4); System.out.println ("-------------setter way to inject writeMethod, remember to have a parameterless constructor-------------"); System.out.println (STU5); System.out.println (STU6); System.out.println (STU7);}}

Output Result:


This is the final result, isn't it simple? If you think this article is useful to you, then help me to vote ~ ~ ~ Thank you ~ ~


Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka

"Spring" Construcotrer injection and setter injection in different ways of XML notation

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.