Spring IOC Injection (III) auto-injection

Source: Internet
Author: User

The first two articles talk about the IOC injected set-mode injection and the constructor-based injection, which now speaks of automatic injection ~

Automatic injection : The container assembles a property in the bean according to some rules

Note : Automatic assembly only works on [object type] and does not work on basic types.

There are two ways of automatic injection:

First: Configure the mount mode in the Beans tab: defadefault-autowire= "Bytype" or defult-autowire= "ByName"
Default-autowire= "Bytype" or default-autowire= "ByName"
by adding this attribute to the root element beans, all of the following beans will
Automatic injection using byname or Bytype, if one of the following beans
you want to inject it in other ways, you can use the Autowire= "" property to
, or if a bean does not want to use any automatic injection, use autowire= "No"

Second: Specify the configuration method in the Bean label

Autowire= "ByName":
the Spring container will find the name of the property in the current class, and then
and then, according to this name, to the spring container.
the object with the same name, if any, put the object as a parameter in the
Setxxxx This method into the inside.
Note: Learn what the property refers to in the class.



Autowire= "Bytype":
the spring container is based on the type of the parameter inside the set method in the current class.
go to the container to find the matching object, if not found even if you find
One will be injected in, if you find more than one, then will be an error.


Autowrite= "Constructor"
Match according to the constructor's parameter type

Now, let's use these two ways, respectively.

The first type:

(1) Configure the loading mode in the beans tag: defadefault-autowire= "Bytype"

By adding this attribute to the root element beans, all of the following beans will
Automatic injection using Bytype, if one of the following beans
you want to inject it in other ways, you can use the Autowire= "" property to
, or if a bean does not want to use any automatic injection, use autowire= "no"

The code is as follows:

Two bean classes--This is a fixed code, followed by a constant use of

Student class:

public class Student {private long id;private string name;private int age;public Student (long ID, String name, Int. age) {T His.id = Id;this.name = Name;this.age = age;} Public Student () {System.out.println ("in Student ()");} Public long GetId () {return ID;} public void SetId (long id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}
Teacher Class:

public class Teacher {private long id;private String name;//Reference Type property private Student student;public Teacher () {//TODO auto- Generated constructor stub}public Teacher (Student Student) {this.student = Student;} Public long GetId () {return ID;} public void SetId (long id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Student getstudent () {return Student;} public void Setstudent (Student Student) {this.student = Student;}}


Configuration file Autowired.xml:

<?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:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-3.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context-3.2.xsd "default-autowire=" Bytype "> <bean name=" Student "class=" com.x.spring.b Ean.     Student "> <property name=" id "> <value>1001</value> </property> </bean> <!--<bean name= "Student1" class= "com.x.spring.bean.student" > <property name= "id" > <value>2 002</value> </property> </bean>-<bean name= "teacher" class= "Com.x.spring.bean.teac Her "> </bean> </beans>




The current class is the teacher class, as can be seen from the test class, the test Class I will give the code below, the current set method inside the object parameter types are:

Teacher Class:


Comment out those lines, student class can be injected into the ~

Autowriredtest Test class:

public class Autowriredtest {public static void main (string[] args) {String path = "Autowired.xml"; ApplicationContext container = new Classpathxmlapplicationcontext (path); Teacher t = (Teacher) container.getbean ("Teacher"); System.out.println (t); System.out.println (T.getstudent (). GetId ());}}





Well, now let's look at the second use of the beans tag ~

(2) in Beans in the label, configure the Mount mode: default-autowire= "ByName"

By adding this attribute to the root element beans, all of the following beans will
Automatic injection using ByName, if one of the following beans
you want to inject it in other ways, you can use the Autowire= "" property to
, or if a bean does not want to use any automatic injection, use autowire= "no"

Just change the configuration file Autowired.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:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-3.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context-3.2.xsd "default-autowire=" byname "> <bean name=" Student "class=" com.x.spring.b Ean.      Student "> <property name=" id "> <value>1001</value> </property> </bean> <bean name= "Student1" class= "com.x.spring.bean.student" > <property name= "id" > <value>2002<     /value> </property> </bean> <bean name= "Teacher" class= "Com.x.spring.bean.teacher" > </bean> </beans>
Let me explain, please.





Run, Effect


Well, the second usage is done.

Let's talk about the third use.

(3) Configure the loading mode in the beans tag: default-autowire= "Bytype"

Write autowire= "ByName" in the bean label

Just change the configuration file, 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:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-3.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context-3.2.xsd "default-autowire=" Bytype "> <bean name=" Student "class=" com.x.spring.b Ean.      Student "> <property name=" id "> <value>1001</value> </property> </bean> <bean name= "Student1" class= "com.x.spring.bean.student" > <property name= "id" > <value>2002< /value> </property> </bean> <bean name= "Teacher" class= "Com.x.spring.bean.teacher" autowir E= "ByName" > </bean> </beans> 

In fact, think about it a little bit to think about it, and this is the same as the second usage ~ because this attribute default-autowire= "Bytype" is added to the root element beans, all the following beans are automatically injected using the Bytype method. If you want to inject in one of the following beans in a different way, you can use the Autowire= "" property to describe it, or if a bean does not want to use any automatic injection autowire= "No"

And there will be priority, first of all, consider the bean tag inside the ~

So, the third use is finished, the second way to start it ~


Second: Specify the configuration method in the Bean label

(1) autowire= "Bytype"

the spring container is based on the type of the parameter inside the set method in the current class.
go to the container to find the matching object, if not found even if you find
One will be injected in, if you find more than one, then will be an error.

Only need to change the configuration file can ~

<?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:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-3.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context-3.2.xsd "> <bean name=" Student "class=" com.x.spring.bean.student "> < Property name= "id" > <value>1001</value> </property> </bean> <!--<bean N     Ame= "Student1" class= "com.x.spring.bean.student" > <property name= "id" > <value>2002</value> </property> </bean> <bean name= "Teacher" class= "Com.x.spring.bean.teacher" autowire= "Byty PE "> </bean> </beans>
Here I do not explain, and the first way of the first usage is the same ~ is the same ~

(2) autowire= "ByName"
The spring container will find the name of the property in the current class, and then
and then, according to this name, to the spring container.
the object with the same name, if any, put the object as a parameter in the
Setxxxx This method into the inside.
Note: Learn what the property refers to in the class.

It's just a matter of changing the configuration file ~

<?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:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-3.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context-3.2.xsd "> <bean name=" Student "class=" com.x.spring.bean.student "> < Property name= "id" > <value>1001</value> </property> </bean> <bean name= "Stu Dent1 "class=" com.x.spring.bean.student "> <property name=" id "> <value>2002</value> </p roperty> </bean> <bean name= "Teacher" class= "Com.x.spring.bean.teacher" autowire= "byname" > &L T;/bean> </beans>
This explanation is the same as the second usage of the first way.

(3) Autowrite= "constructor"

This doesn't work, I don't talk about it.



All right, that's it.

If you have any questions, ask me.




Spring IOC Injection (III) auto-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.