<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Before you say no </span><span style= "font-family: ' Times New Roman '; Background-color:rgb (255, 255, 255); " >spring</span><span style= "font-family: Song body; Background-color:rgb (255, 255, 255); " >, the object of direct cooperation is if the work, such as </span><span style= "font-family: ' Times New Roman '; Background-color:rgb (255, 255, 255); " >a</span><span style= "font-family: Song body; Background-color:rgb (255, 255, 255); " > class to call </span><span style= "font-family: ' Times New Roman '; Background-color:rgb (255, 255, 255); " >b</span><span style= "font-family: Song body; Background-color:rgb (255, 255, 255); " The > method, first in </span><span style= "font-family: ' Times New Roman '; Background-color:rgb (255, 255, 255); " >a</span><span style= "font-family: Song body; Background-color:rgb (255, 255, 255); " The > class will definitely need to create a </span><span style= "font-family: ' Times New Roman '; Background-coLor:rgb (255, 255, 255); " >b</span><span style= "font-family: Song body; Background-color:rgb (255, 255, 255); " > object, then through </span><span style= "font-family: ' Times New Roman '; Background-color:rgb (255, 255, 255); " >b</span><span style= "font-family: Song body; Background-color:rgb (255, 255, 255); " > instance of object to call </span><span style= "font-family: ' Times New Roman '; Background-color:rgb (255, 255, 255); " >a</span><span style= "font-family: Song body; Background-color:rgb (255, 255, 255); " An example method of the > method, such as a static method, can be called directly from the class name. The disadvantage of doing so through </span><span style= "font-family: ' Times New Roman '; Background-color:rgb (255, 255, 255); " >new</span><span style= "font-family: Song body; Background-color:rgb (255, 255, 255); " > object of the way to create objects, the first mutual dependence is too large, the same object created multiple times, after the end of the need to destroy, inefficient, such a way overall is that the coupling between the object is high, low efficiency </span><span style= " Font-family: ' Times New Roman '; Background-color:rgb (255, 255, 255); " > (</span><span style= "font-family: Song body; Background-color:rgb (255, 255, 255); " > Every time you need to create and destroy objects </span><span style= "font-family: ' Times New Roman '; Background-color:rgb (255, 255, 255); " >) </span>
Springto solve these problems, there have been similar frameworks before, that isEJB (Enterprise JavaBean)To define different types ofBean, but it is cumbersome and difficult to understand and learn,Springbasically completely replaced the appearance of theEJB,SpringNotice there are two chunks, one isIOCand theDI (Invert control Dependency Injection), and there isAOPplane-oriented programming), here is a key explanationSpringof theIOCand theDI
Spring IOCis the inversion control, bySpringto generate objects, no longer have objects to be created and destroyed when needed, bySpringto control the life cycle of the object.Springto manage, this is inversion control. So what's going on with dependency injection, through research that actuallySpringis to reverse the entire process of creating an object, the process of creating the object must contain the attributes of his object, such as creatingAobject,Aobject needs to be calledBAn instance method of an object, it must be created with theBobject is injected toAobject, soAThe object is a target we want, actually.Bobject is injected toAobject is the process of dependency injection, in the final analysis to create a completeAobject, and you need to giveAan attribute of an object(Ban instance of an object)assignment, then by and by, all objects areSpringto create, all haveSpringto manage, there areSpringto inject, this isSpringof theIOCthe most basic thought,
Instance parsingSpringof dependency Injection. For example, now a request is sent toController,Controllerneed to callServiceobject, and the method of theServiceyou also need to callDAOthe method, then actually create theControllerobject, it should be.Serviceobject into aAction, butServicewhen creating objects, you need toDAOobject is injected toService, so when the request arrivedControllerwe are directly able to get data and manipulate the database data, without the bottom is how to achieve, the underlyingDAOthe operational database is throughJDBCor isHibernateor isMyBatisdo not care, including some objects that manipulate database objects such asConnectionWhen an object is created when it is destroyed, we do not need to care, and this is completely without violatingOOPthought of.
Injection of the realization, that sinceAto need to getBan instance of the object, which isAthe object is sure to need aBtype as a property of theBobject to the instance of theBobject is injected toAobject of thisBThe attributes of the object's vectors, which are injected into the configuration file injection and annotation injection, arespring2.xand earlier versions can only be injected through the configuration file before supporting annotation injection, first of all to explain the configuration file method of injection, which also more reflect the principle of dependency injection, such as I now define aUserServiceand aUserdao, now to putDAOinjected toService, it must be earlyUserServicethe first one to acceptUserdaobecause of the configuration injection, the property must be provided externallySetmethod is then injected into the configuration file through this methodDAOobject, note that the property name must beServicename of the property, and also requiresServiceThe properties in and the correspondingSetmethods must be named according to the standard properties. The property value must beSpringobjects that already exist, cannot be given toSpringthe managed class.
Extension point, in fact, not only can inject object properties, general simple properties can also inject the amount, such as injecting an Integer,List can also inject , simple attribute injection through value injection, complex attributes ( Object Properties ) are injected by ref .
Code implementation:
XML Configuration <!--Userdao--><bean id= "Userdao" class= "Cn.dome.dao.UserDao"/><!--userservice--><bean Id= "UserService" class= "Cn.dome.service.UserService" ><!--object injection--><property name= "Userdao" ref= "Userdao ></property><!--string attribute injection--><property name= "UserName" value= "Zhang San" ></property><!-- Simple attribute injection--><property name= "age" value= "></property><p</bean>"
Userservicepackage Cn.dome.service;import Cn.dome.service.userdao;public class UserService { //userdao object Private Userdao Userdao; Name (attribute) private String userName; Age (attribute) private Integer; public void Setuserdao (Userdao userdao) { This.userdao = Userdao; } public void Setusername (String userName) { this.username = userName; } public void Setage (Integer age) { this.age = age; } public void Delete (Integer id) { userdao.delete (id);} }
</pre><pre name= "code" class= "HTML" >userdao code package Cn.dome.dao;public class Userdao {public void Delete (Integer ID) { }}
annotation injection, annotation injection is spring2.0 After the injection method, the advantage is not to write cumbersome configuration files, public component annotations, you can give a common class to Spring management, and Spring generate and assign, and then inject annotations to inject a specified object into one of the properties of the object, annotation injection by name injection, by name injection, must require the corresponding name on the component annotations to be injected by name, the second is through the type injection, Only type matching can be injected, but if the implementation class through the interface application, if the interface has more than one implementation class, through the type injection will be error, the third is through the type and name of the mixed injection, so that through the interface to apply the implementation class, but the implementation class must have a name , Annotations can reduce the complexity of writing XML configuration, only need to turn on the annotation driver
XML Configuration <!--opening annotations sweep face--><context:component-scan base-package= "Cn.dome"/>
</pre><p></p><span style= "font-family: Song Body" ></span><pre name= "code" class= "HTML" >userdao Code Package Cn.dome.dao;import org.springframework.stereotype.Component, @Componentpublic class Userdao { Public void Delete (Integer id) { }}
</pre><pre name= "code" class= "HTML" >userservice code package Cn.dome.<span style= "font-family: Arial;" >service</span><span style= "font-family: the song Body;" >;</span>import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import Cn.dome.service.UserDao; @Servicepublic class UserService { @ autowired private Userdao Userdao; public void Delete (Integer id) { userdao.delete (id);} }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Deep analysis of Spring's IOC and DI