一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 基于Spring@Autowired注解與自動(dòng)裝配詳談

基于Spring@Autowired注解與自動(dòng)裝配詳談

2021-01-16 10:32和大黃 Java教程

下面小編就為大家?guī)硪黄赟pring@Autowired注解與自動(dòng)裝配詳談。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

1 配置文件的方法

我們編寫spring 框架的代碼時(shí)候。一直遵循是這樣一個(gè)規(guī)則:所有在spring中注入的bean 都建議定義成私有的域變量。并且要配套寫上 get 和 set方法。

Boss 擁有 Office 和 Car 類型的兩個(gè)屬性:

清單 3. Boss.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.baobaotao; 
 
public class Boss { 
 private Car car; 
 private Office office; 
 
 // 省略 get/setter 
 
 @Override
 public String toString() { 
  return "car:" + car + "/n" + "office:" + office; 
 
}

System.out.println必須實(shí)現(xiàn)toString方法

我們?cè)?Spring 容器中將 Office 和 Car 聲明為 Bean,并注入到 Boss Bean 中:下面是使用傳統(tǒng) XML 完成這個(gè)工作的配置文件 beans.xml:

清單 4. beans.xml 將以上三個(gè)類配置成 Bean

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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-2.5.xsd"> 
 <bean id="boss" class="com.baobaotao.Boss"
  <property name="car" ref="car"/> 
  <property name="office" ref="office" /> 
 </bean
 <bean id="office" class="com.baobaotao.Office"
  <property name="officeNo" value="002"/> 
 </bean
 <bean id="car" class="com.baobaotao.Car" scope="singleton"
  <property name="brand" value=" 紅旗 CA72"/> 
  <property name="price" value="2000"/> 
 </bean
</beans>

當(dāng)我們運(yùn)行以下代碼時(shí),控制臺(tái)將正確打出 boss 的信息:

清單 5. 測(cè)試類:AnnoIoCTest.java

?
1
2
3
4
5
6
7
8
9
10
11
12
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
public class AnnoIoCTest { 
 
 public static void main(String[] args) { 
  String[] locations = {"beans.xml"}; 
  ApplicationContext ctx = 
   new ClassPathXmlApplicationContext(locations); 
  Boss boss = (Boss) ctx.getBean("boss"); 
  System.out.println(boss); 
 
}

這說明 Spring 容器已經(jīng)正確完成了 Bean 創(chuàng)建和裝配的工作。

2 @Autowired

Spring 2.5 引入了 @Autowired 注釋,它可以對(duì)類成員變量、方法及構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動(dòng)裝配的工作。 通過 @Autowired的使用來消除 set ,get方法。

要實(shí)現(xiàn)我們要精簡(jiǎn)程序的目的。需要這樣來處理:

* 在applicationContext.xml中加入:

?
1
2
<!-- 該 BeanPostProcessor 將自動(dòng)對(duì)標(biāo)注 @Autowired 的 Bean 進(jìn)行注入 -->
 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

Spring 通過一個(gè) BeanPostProcessor 對(duì) @Autowired 進(jìn)行解析,所以要讓 @Autowired 起作用必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。

* 修改在原來注入spirng容器中的bean的方法。

在域變量上加上標(biāo)簽@Autowired,并且去掉 相應(yīng)的get 和set方法

清單 6. 使用 @Autowired 注釋的 Boss.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.baobaotao; 
import org.springframework.beans.factory.annotation.Autowired; 
 
public class Boss { 
 
 @Autowired
 private Car car; 
 
 @Autowired
 private Office office; 
 
 … 
}

* 在applicatonContext.xml中 把原來 引用的<porpery >標(biāo)簽也去掉。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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-2.5.xsd"> 
 
 <!-- 該 BeanPostProcessor 將自動(dòng)起作用,對(duì)標(biāo)注 @Autowired 的 Bean 進(jìn)行自動(dòng)注入 -->
 <bean class="org.springframework.beans.factory.annotation. 
  AutowiredAnnotationBeanPostProcessor"/> 
 
 <!-- 移除 boss Bean 的屬性注入配置的信息 -->
 <bean id="boss" class="com.baobaotao.Boss"/> 
  
 <bean id="office" class="com.baobaotao.Office"
  <property name="officeNo" value="001"/> 
 </bean
 <bean id="car" class="com.baobaotao.Car" scope="singleton"
  <property name="brand" value=" 紅旗 CA72"/> 
  <property name="price" value="2000"/> 
 </bean
</beans>

這樣,當(dāng) Spring 容器啟動(dòng)時(shí),AutowiredAnnotationBeanPostProcessor 將掃描 Spring 容器中所有 Bean,當(dāng)發(fā)現(xiàn) Bean 中擁有 @Autowired 注釋時(shí)就找到和其匹配(默認(rèn)按類型匹配)的 Bean,并注入到對(duì)應(yīng)的地方中去。

按照上面的配置,Spring 將直接采用 Java 反射機(jī)制對(duì) Boss 中的 car 和 office 這兩個(gè)私有成員變量進(jìn)行自動(dòng)注入。所以對(duì)成員變量使用 @Autowired 后,您大可將它們的 setter 方法(setCar() 和 setOffice())從 Boss 中刪除。

當(dāng)然,您也可以通過 @Autowired 對(duì)方法或構(gòu)造函數(shù)進(jìn)行標(biāo)注,如果構(gòu)造函數(shù)有兩個(gè)入?yún)ⅲ謩e是 bean1 和 bean2,@Autowired 將分別尋找和它們類型匹配的 Bean,將它們作為 CountryService (Bean1 bean1 ,Bean2 bean2) 的入?yún)韯?chuàng)建 CountryService Bean。來看下面的代碼: 對(duì)方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.baobaotao; 
 
public class Boss { 
 private Car car; 
 private Office office; 
 
  @Autowired
 public void setCar(Car car) { 
  this.car = car; 
 
  
 @Autowired
 public void setOffice(Office office) { 
  this.office = office; 
 
 … 
}

這時(shí),@Autowired 將查找被標(biāo)注的方法的入?yún)㈩愋偷?Bean,并調(diào)用方法自動(dòng)注入這些 Bean。而下面的使用方法則對(duì)構(gòu)造函數(shù)進(jìn)行標(biāo)注:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.baobaotao; 
 
public class Boss { 
 private Car car; 
 private Office office; 
 
  @Autowired
 public void setCar(Car car) { 
  this.car = car; 
 
  
 @Autowired
 public void setOffice(Office office) { 
  this.office = office; 
 
 

由于 Boss() 構(gòu)造函數(shù)有兩個(gè)入?yún)ⅲ謩e是 car 和 office,@Autowired 將分別尋找和它們類型匹配的 Bean,將它們作為 Boss(Car car ,Office office) 的入?yún)韯?chuàng)建 Boss Bean。

以上這篇基于Spring@Autowired注解與自動(dòng)裝配詳談就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/heyutao007/article/details/5981555

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 丰满大屁股美女一级毛片 | 日韩亚洲一区中文字幕在线 | 高h辣文小说网 烧书阁 | 99热精品久久 | 日本美女视频韩国视频网站免费 | 91sao在线看片水片 | 久热在线这里只有精品7 | 脱女学小内内摸出水网站免费 | 羞羞污视频 | 日韩亚洲欧美一区二区三区 | 国产人妖ts在线视频网 | 久久中文字幕亚洲精品最新 | 女人把扒开给男人爽的 | 91天堂国产在线 在线播放 | 爸爸干女儿小说 | 1024免费观看完整版在线播放 | 777奇米影视一区二区三区 | 99在线视频观看 | 美女扒开腿让男生捅 | 亚洲成在人网站天堂一区二区 | 四虎影视4hutv最新地址在线 | 免费黄色片在线观看 | 国产精品成人免费观看 | 日本阿v在线播放 | 亚洲精品黄色 | 精品免费久久久久久成人影院 | voyeur 中国女厕 亚洲女厕 | 色多多在线观看视频 | 国产一成人精品福利网站 | 亚洲精品国产自在现线最新 | 久久成人免费大片 | 咪咪爱小说 | 亚洲国产综合久久久无码色伦 | 俄罗斯13一14处出血视频在线 | 国产一区二区视频在线观看 | 黑人干亚洲人 | 亚洲免费在线观看视频 | 亚洲高清毛片一区二区 | 97综合 | 热99re久久精品精品免费 | 五月婷婷丁香色 |