spring 注入bean的两种方式
?
我們都知道,使用spring框架時(shí),不用再使用new來實(shí)例化對(duì)象了,直接可以通過spring容器來注入即可。
而注入bean有兩種方式:
一種是通過XML來配置的,分別有屬性注入、構(gòu)造函數(shù)注入和工廠方法注入;
另一種是通過注解的方式注入,有@Autowired和@Resource
?
我們先來講通過注解的方式:
@Autowired和@Resource都是對(duì)引用對(duì)象的注入,它們有什么區(qū)別呢?
(1)@Autowired是先是匹配引用的類型(就算沒有匹配到),再匹配對(duì)象,@Resource是先匹配對(duì)象(就算沒有匹配到),再匹配引用的類型;
(2)它們兩者的提供者不一樣,@Autowired是spring提供的,而@Resource是JavaEE提供的。
?
下面我們直接通過例子來說明(我就當(dāng)大家都對(duì)spring有點(diǎn)理解了,只寫出部分代碼)
?
@Autowired是怎么注入呢?
?
spring.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="hello" class="com.service.imple.SayHelloImpl"></bean> </beans>JunitTest.java
package com.test;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.service.ISayHello;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring.xml") public class JunitTest {@Autowiredprivate ISayHello sayhello;@Testpublic void test() {sayhello.sayHello();} }JunitTest.java中的@Autowired是怎么把對(duì)象給注入進(jìn)來的呢?
先找到spring.xml的文件,找到<bean>標(biāo)簽,先查找<bean>標(biāo)簽中的class屬性,找到接口為 ISayHello的實(shí)現(xiàn)類,然后把對(duì)應(yīng)的對(duì)象給注入進(jìn)來。
但是有個(gè)問題,要是有兩個(gè)相同的class屬性了,系統(tǒng)就會(huì)報(bào)錯(cuò),比如:
運(yùn)行結(jié)果:
系統(tǒng)找到兩個(gè)對(duì)象,一個(gè)是hello,一個(gè)是hello1,系統(tǒng)不知道要注入哪個(gè),所以就報(bào)錯(cuò)了。這時(shí)候這么辦呢?
很簡(jiǎn)單,有兩種解決方案:
一種是直接在引用的地方,把名稱改為實(shí)例化中bean中某一個(gè)名稱,如下圖:
另一種是在@Autowired前提下,再添加@Qualifier("hello")注解,在該注解指定對(duì)象的名稱,如下圖:
?
@Resource是怎么注入的呢?
?
@Resource是先匹配對(duì)象的名稱(hello),找到后就直接注入進(jìn)來
?
但是,類似于@Autowired,如果有想聽的名稱,但有兩個(gè)實(shí)現(xiàn)類的話,會(huì)出現(xiàn)什么問題?如下圖:
?
運(yùn)行結(jié)果:
?
?解決方案:在引用的地方,直接指定對(duì)象的名稱,如下圖:
?
我們來講一下bean和spring容器的關(guān)系:
?
工作原理:
(1)spring容器會(huì)根據(jù)含有Bean配置的XML文件(spring.xml),把配置文件的Bean配置信息與spring容器中Bean定義注冊(cè)表相對(duì)應(yīng)起來;
(2)spring容器根據(jù)Bean注冊(cè)表實(shí)例化Bean;
(3)通過Bean的實(shí)現(xiàn)類,把實(shí)例化過的Bean放到spring容器中的Bean緩存池中;
(4)某個(gè)模塊要是使用的話,通過注解或者xml配置,讓spring把Bean緩存池中的實(shí)例Bean注入到該模塊當(dāng)中;
?
Bean通過XMl配置注入
?(1)屬性注入
屬性注入即通過setXxx()方法注入Bean的屬性值或依賴對(duì)象,由于屬性注入方式具有可選擇性和靈活性高的優(yōu)點(diǎn),因此屬性注入是實(shí)際應(yīng)用中最常采用的注入方式。
屬性注入要求Bean提供一個(gè)默認(rèn)的構(gòu)造函數(shù),并為需要注入的屬性提供對(duì)應(yīng)的Setter方法。Spring先調(diào)用Bean的默認(rèn)構(gòu)造函數(shù)實(shí)例化Bean對(duì)象,然后通過反射的方式調(diào)用Setter方法注入屬性值。
廢話不多說,直接上代碼:
spring.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="hello" class="com.service.imple.SayHelloImpl"><property name="name" value="小明"></property></bean></beans>其中:屬性注入時(shí),要在<bean>標(biāo)簽里添加<property>標(biāo)簽,該標(biāo)簽里name屬性是對(duì)應(yīng)的屬性的名稱,value屬性是賦值。
SayHelloImpl.java
package com.service.imple;import com.service.ISayHello;public class SayHelloImpl implements ISayHello{private String name;@Overridepublic void sayHello() {System.out.println(name+"你好啊!!!程序員"); }public String getName() {return name;}public void setName(String name) {this.name = name;} }(2)構(gòu)造方法注入
轉(zhuǎn)載于:https://www.cnblogs.com/WQX-work24/p/9929325.html
總結(jié)
以上是生活随笔為你收集整理的spring 注入bean的两种方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序的z-index在苹果ios无
- 下一篇: cesium obj转b3dm转换及加载