Spring is a framework that relies on injection (inversion of control), so where does dependency injection (subscript control inversion) appear?
That is, a property (other object) in a class no longer needs to be manually new or created through a factory method, but rather the spring container is injected when the property is used.
There are 2 ways of injecting:
1. Attribute injection: +setter method injection via parameterless constructor
2. Construction injection: Injected by a constructor function with a parameter.
Advantages and Disadvantages
1. Attribute injection is straightforward, the disadvantage is that when it comes to attributes, many constructors appear to be bloated.
2. Construction injection is a high-cohesion embodiment, especially for some attributes that need to be assigned when the object is created, and subsequent modifications are not allowed (no setter method is provided).
The project uses MAVEN for architecture, and its basic project structure is:
Among the contents of Pom.xml are:
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.mc.base.learn</groupId> <artifactId> spring</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> &L t;name>spring</name> <url>http://maven.apache.org</url> <properties> <project.b uild.sourceencoding>utf-8</project.build.sourceencoding> </properties> <dependencies> &L T;dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> & lt;! --Spring core dependent-<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.6.RELEASE</version> </dependency> <!--provides a spring context object---& Lt;dependency> <groupId>org.springframework</groupId> <artifactid>spring-contex t</artifactid> <version>4.2.6.RELEASE</version> </dependency> </de Pendencies></project>
Project BASIC Structure complete
First, create an object from a constructor function. 2.1 Injecting values using the +setter method of the parameterless constructor
The most basic method of object creation, only need to have a parameterless constructor (the class does not write any constructors, the default is that there is a constructor, if you write any one constructor, the default parameterless constructor will not be created automatically Oh!! ) and the setter method for the field.
Person class:
Package Com.mc.base.learn.spring.bean;public class Person { private String name; Private Integer ID; Public String GetName () { return name; } public void SetName (String name) { this.name = name; } Public Integer getId () { return ID; } public void SetId (Integer id) { this.id = ID; } @Override public String toString () { return ' person [name= + name + ', id= ' + ID + '] '; }}
XML configuration:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://www.springframework.org/schema/ Beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean class=" Com.mc.base.learn.spring.bean.Person "id=" person "> <property name=" name "value=" Liuchunfu "></ property> <property name= "id" value= "></property>" </bean> </beans>
Its essence is:
Springcontext uses the parameterless constructor to create an object and then assigns a value using the Setter method. So if the parameterless constructor does not exist, the spring context creates an error when the object is created.
Nested exception is java.lang.nosuchmethodexception:com.mc.base.learn.spring.bean.person.<init> () at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean ( abstractautowirecapablebeanfactory.java:1105) .....
2.2 Direct injection using a parametric constructor
Person class:
Package Com.mc.base.learn.spring.bean;public class Person { private String name; Private Integer ID; Public person (String name, Integer ID) { super (); this.name = name; This.id = ID; } Public String GetName () { return name; } public void SetName (String name) { this.name = name; } Public Integer getId () { return ID; } public void SetId (Integer id) { this.id = ID; } @Override public String toString () { return ' person [name= + name + ', id= ' + ID + '] '; }}
XML configuration:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://www.springframework.org/schema/ Beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean class=" Com.mc.base.learn.spring.bean.Person "id=" person "> <constructor-arg name=" id "value=" 123 "></ constructor-arg> <constructor-arg name= "name" value= "Liuchunfu" ></constructor-arg> </ Bean> </beans>
Spring injection is worth 2 ways: attribute injection and construction injection