spring四種依賴(lài)注入方式的詳細(xì)介紹
平常的java開(kāi)發(fā)中,程序員在某個(gè)類(lèi)中需要依賴(lài)其它類(lèi)的方法,則通常是new一個(gè)依賴(lài)類(lèi)再調(diào)用類(lèi)實(shí)例的方法,這種開(kāi)發(fā)存在的問(wèn)題是new的類(lèi)實(shí)例不好統(tǒng)一管理,spring提出了依賴(lài)注入的思想,即依賴(lài)類(lèi)不由程序員實(shí)例化,而是通過(guò)spring容器幫我們new指定實(shí)例并且將實(shí)例注入到需要該對(duì)象的類(lèi)中。依賴(lài)注入的另一種說(shuō)法是“控制反轉(zhuǎn)”,通俗的理解是:平常我們new一個(gè)實(shí)例,這個(gè)實(shí)例的控制權(quán)是我們程序員,而控制反轉(zhuǎn)是指new實(shí)例工作不由我們程序員來(lái)做而是交給spring容器來(lái)做。
spring有多種依賴(lài)注入的形式,下面僅介紹spring通過(guò)xml進(jìn)行IOC配置的方式:
1、Set注入
這是最簡(jiǎn)單的注入方式,假設(shè)有一個(gè)SpringAction,類(lèi)中需要實(shí)例化一個(gè)SpringDao對(duì)象,那么就可以定義一個(gè)private的SpringDao成員變量,然后創(chuàng)建SpringDao的set方法(這是ioc的注入入口):
package com.bless.springdemo.action;
public class SpringAction {
//注入對(duì)象springDao
private SpringDao springDao;
//一定要寫(xiě)被注入對(duì)象的set方法
public void setSpringDao(SpringDao springDao) {
this.springDao = springDao;
}
public void ok(){
springDao.ok();
}
}
隨后編寫(xiě)spring的xml文件,<bean>中的name屬性是class屬性的一個(gè)別名,class屬性指類(lèi)的全名,因?yàn)樵赟pringAction中有一個(gè)公共屬性Springdao,所以要在<bean>標(biāo)簽中創(chuàng)建一個(gè)<property>標(biāo)簽指定SpringDao。<property>標(biāo)簽中的name就是SpringAction類(lèi)中的SpringDao屬性名,ref指下面<bean name="springDao"...>,這樣其實(shí)是spring將SpringDaoImpl對(duì)象實(shí)例化并且調(diào)用SpringAction的setSpringDao方法將SpringDao注入:
<!--配置bean,配置后該類(lèi)由spring管理-->
<bean name="springAction" class="com.bless.springdemo.action.SpringAction">
<!--(1)依賴(lài)注入,配置當(dāng)前類(lèi)中相應(yīng)的屬性-->
<property name="springDao" ref="springDao"></property>
</bean>
<bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl">
</bean>
2、構(gòu)造器注入
這種方式的注入是指帶有參數(shù)的構(gòu)造函數(shù)注入,看下面的例子,我創(chuàng)建了兩個(gè)成員變量SpringDao和User,但是并未設(shè)置對(duì)象的set方法,所以就不能支持第一種注入方式,這里的注入方式是在SpringAction的構(gòu)造函數(shù)中注入,也就是說(shuō)在創(chuàng)建SpringAction對(duì)象時(shí)要將SpringDao和User兩個(gè)參數(shù)值傳進(jìn)來(lái):
public class SpringAction {
//注入對(duì)象springDao
private SpringDao springDao;
private User user;
public SpringAction(SpringDao springDao,User user){
this.springDao = springDao;
this.user = user;
System.out.println("構(gòu)造方法調(diào)用springDao和user");
}
public void save(){
user.setName("卡卡");
springDao.save(user);
}
}
在XML文件中同樣不用<property>的形式,而是使用<constructor-arg>標(biāo)簽,ref屬性同樣指向其它<bean>標(biāo)簽的name屬性:
<!--配置bean,配置后該類(lèi)由spring管理-->
<bean name="springAction" class="com.bless.springdemo.action.SpringAction">
<!--(2)創(chuàng)建構(gòu)造器注入,如果主類(lèi)有帶參的構(gòu)造方法則需添加此配置-->
<constructor-arg ref="springDao"></constructor-arg>
<constructor-arg ref="user"></constructor-arg>
</bean>
<bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>
<bean name="user" class="com.bless.springdemo.vo.User"></bean>
解決構(gòu)造方法參數(shù)的不確定性,你可能會(huì)遇到構(gòu)造方法傳入的兩參數(shù)都是同類(lèi)型的,為了分清哪個(gè)該賦對(duì)應(yīng)值,則需要進(jìn)行一些小處理:
下面是設(shè)置index,就是參數(shù)位置:
<bean name="springAction" class="com.bless.springdemo.action.SpringAction">
<constructor-arg index="0" ref="springDao"></constructor-arg>
<constructor-arg index="1" ref="user"></constructor-arg>
</bean>
另一種是設(shè)置參數(shù)類(lèi)型:
<constructor-arg type="java.lang.String" ref=""/>
3、靜態(tài)工廠(chǎng)的方法注入
靜態(tài)工廠(chǎng)顧名思義,就是通過(guò)調(diào)用靜態(tài)工廠(chǎng)的方法來(lái)獲取自己需要的對(duì)象,為了讓spring管理所有對(duì)象,我們不能直接通過(guò)"工程類(lèi).靜態(tài)方法()"來(lái)獲取對(duì)象,而是依然通過(guò)spring注入的形式獲取:
package com.bless.springdemo.factory;
import com.bless.springdemo.dao.FactoryDao;
import com.bless.springdemo.dao.impl.FactoryDaoImpl;
import com.bless.springdemo.dao.impl.StaticFacotryDaoImpl;
public class DaoFactory {
//靜態(tài)工廠(chǎng)
public static final FactoryDao getStaticFactoryDaoImpl(){
return new StaticFacotryDaoImpl();
}
}
同樣看關(guān)鍵類(lèi),這里我需要注入一個(gè)FactoryDao對(duì)象,這里看起來(lái)跟第一種注入一模一樣,但是看隨后的xml會(huì)發(fā)現(xiàn)有很大差別:
public class SpringAction {
//注入對(duì)象
private FactoryDao staticFactoryDao;
public void staticFactoryOk(){
staticFactoryDao.saveFactory();
}
//注入對(duì)象的set方法
public void setStaticFactoryDao(FactoryDao staticFactoryDao) {
this.staticFactoryDao = staticFactoryDao;
}
}
Spring的IOC配置文件,注意看<bean name="staticFactoryDao">指向的class并不是FactoryDao的實(shí)現(xiàn)類(lèi),而是指向靜態(tài)工廠(chǎng)DaoFactory,并且配置 factory-method="getStaticFactoryDaoImpl"指定調(diào)用哪個(gè)工廠(chǎng)方法:
<!--配置bean,配置后該類(lèi)由spring管理-->
<bean name="springAction" class="com.bless.springdemo.action.SpringAction" >
<!--(3)使用靜態(tài)工廠(chǎng)的方法注入對(duì)象,對(duì)應(yīng)下面的配置文件(3)-->
<property name="staticFactoryDao" ref="staticFactoryDao"></property>
</property>
</bean>
<!--(3)此處獲取對(duì)象的方式是從工廠(chǎng)類(lèi)中獲取靜態(tài)方法-->
<bean name="staticFactoryDao" class="com.bless.springdemo.factory.DaoFactory" factory-method="getStaticFactoryDaoImpl"></bean>
4、實(shí)例工廠(chǎng)的方法注入
實(shí)例工廠(chǎng)的意思是獲取對(duì)象實(shí)例的方法不是靜態(tài)的,所以你需要首先new工廠(chǎng)類(lèi),再調(diào)用普通的實(shí)例方法:
public class DaoFactory {
//實(shí)例工廠(chǎng)
public FactoryDao getFactoryDaoImpl(){
return new FactoryDaoImpl();
}
}
那么下面這個(gè)類(lèi)沒(méi)什么說(shuō)的,跟前面也很相似,但是我們需要通過(guò)實(shí)例工廠(chǎng)類(lèi)創(chuàng)建FactoryDao對(duì)象:
public class SpringAction {
//注入對(duì)象
private FactoryDao factoryDao;
public void factoryOk(){
factoryDao.saveFactory();
}
public void setFactoryDao(FactoryDao factoryDao) {
this.factoryDao = factoryDao;
}
}
最后看spring配置文件:
<!--配置bean,配置后該類(lèi)由spring管理-->
<bean name="springAction" class="com.bless.springdemo.action.SpringAction">
<!--(4)使用實(shí)例工廠(chǎng)的方法注入對(duì)象,對(duì)應(yīng)下面的配置文件(4)-->
<property name="factoryDao" ref="factoryDao"></property>
</bean>
<!--(4)此處獲取對(duì)象的方式是從工廠(chǎng)類(lèi)中獲取實(shí)例方法-->
<bean name="daoFactory" class="com.bless.springdemo.factory.DaoFactory"></bean>
<bean name="factoryDao" factory-bean="daoFactory" factory-method="getFactoryDaoImpl"></bean>
總結(jié)
Spring IOC注入方式用得最多的是(1)(2)種,多謝多練就會(huì)非常熟練。
另外注意:通過(guò)Spring創(chuàng)建的對(duì)象默認(rèn)是單例的,如果需要?jiǎng)?chuàng)建多實(shí)例對(duì)象可以在<bean>標(biāo)簽后面添加一個(gè)屬性:
<bean name="..." class="..." scope="prototype">
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用spring-data-redis實(shí)現(xiàn)incr自增的操作
這篇文章主要介紹了利用spring-data-redis實(shí)現(xiàn)incr自增的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
關(guān)于RedisTemplate之opsForValue的使用說(shuō)明
這篇文章主要介紹了關(guān)于RedisTemplate之opsForValue的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Java中的ArrayList.trimToSize()方法詳解
這篇文章主要介紹了Java中的ArrayList.trimToSize()方法詳解,前幾天看了Java?ArrayList,沒(méi)有明白trimToSize()這個(gè)方法是什么意思,所以看了一下源碼并且debug一下自己的一個(gè)例子,明白了其中的含義,需要的朋友可以參考下2023-11-11
java swing 實(shí)現(xiàn)加載自定義的字體
這篇文章主要介紹了java swing 實(shí)現(xiàn)加載自定義的字體,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
鑒權(quán)認(rèn)證+aop+注解+過(guò)濾feign請(qǐng)求的實(shí)例
這篇文章主要介紹了鑒權(quán)認(rèn)證+aop+注解+過(guò)濾feign請(qǐng)求的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
idea顯示springboot多服務(wù)啟動(dòng)界面service操作
這篇文章主要介紹了idea顯示springboot多服務(wù)啟動(dòng)界面service操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
springboot Jpa多數(shù)據(jù)源(不同庫(kù))配置過(guò)程
這篇文章主要介紹了springboot Jpa多數(shù)據(jù)源(不同庫(kù))配置過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05

