采用注解的優(yōu)勢:
- 注解可以充分利用 Java 的反射機(jī)制獲取類結(jié)構(gòu)信息,這些信息可以有效減少配置的工作敲霍。如使用 JPA 注釋配置 ORM 映射時耿战,我們就不需要指定 PO 的屬性名巴帮、類型等信息晶密,如果關(guān)系表字段和 PO 屬性名、類型都一致秀鞭,您甚至無需編寫任務(wù)屬性映射信息——因為這些信息都可以通過 Java 反射機(jī)制獲取趋观。
- 注解和 Java 代碼位于一個文件中牙言,而 XML 配置采用獨(dú)立的配置文件搓谆,大多數(shù)配置信息在程序開發(fā)完成后都不會調(diào)整,如果配置信息和 Java 代碼放在一起归苍,有助于增強(qiáng)程序的內(nèi)聚性宠默。而采用獨(dú)立的 XML 配置文件麸恍,程序員在編寫一個功能時,往往需要在程序文件和配置文件中不停切換,這種思維上的不連貫會降低開發(fā)效率抹沪。
以前配置bean的方法及在bean之間建立依賴關(guān)系的做法
以用戶購買商品為例主要有四個實體類(items,orderdateil,user)
商品信息 Items.java
public class Items {
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
//省略 get/setter
@Override
public String toString() {
return "Items{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", pic='" + pic + '\'' +
", createtime=" + createtime +
", detail='" + detail + '\'' +
'}';
}
}
訂單明細(xì)(包含用戶信息與商品信息) Orderdetail.java
public class Orderdetail {
private int id;
private Items items;
private User user;
//省略get/setter方法
@Override
public String toString() {
return "Orderdetail{" +
"id=" + id +
", items=" + items +
", user=" + user +
'}';
}
}
用戶 User.jav
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
//省略get/setter方法
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
", iId=" + iId +
'}';
}
}
在spring容器中我們將User和Order兩個類聲明為bean,并注入到Orderdetail這個bean中刻肄,因此創(chuàng)建一個bean.xml,進(jìn)行配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc">
<bean id="items" class="pojo.Items">
<property name="name" value="蘋果手機(jī)"></property>
<property name="price" value="10000"></property>
</bean>
<bean id="user" class="pojo.User">
<property name="username" value="張三"></property>
<property name="address" value="陝西西安"></property>
</bean>
<bean id="orderdetail" class="pojo.Orderdetail">
<property name="items" ref="items"></property>
<property name="user" ref="user"></property>
</bean>
</beans>
測試 Test.java (輸出訂單明細(xì)時,成功時就會打印用戶與商品的相關(guān)信息)
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Orderdetail;
/**
* Created by admin on 2017/6/30.
*/
public class Test {
public static void main(String[] args) {
String path = "bean.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
Orderdetail orderdetail = (Orderdetail) applicationContext.getBean("orderdetail");
System.out.println(orderdetail);
}
}
使用 @Autowired 注釋
- @Autowired可以對成員變量融欧、方法和構(gòu)造函數(shù)進(jìn)行標(biāo)注敏弃,來完成自動裝配的工作,這里必須明確:@Autowired是根據(jù)類型進(jìn)行自動裝配的噪馏,如果需要按名稱進(jìn)行裝配麦到,則需要配合@Qualifier[1]使用;
- @Autowired標(biāo)注可以放在成員變量上欠肾,也可以放在成員變量的set方法上瓶颠。前者,Spring會直接將UserDao類型的唯一一個bean賦值給userDao這個成員變量刺桃;后者粹淋,Spring會調(diào)用setUserDao方法來將UserDao類型的唯一一個bean裝配到userDao這個屬性。
- Spring 2.5 引入了 @Autowired 注釋瑟慈,它可以對類成員變量桃移、方法及構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動裝配的工作葛碧。 通過 @Autowired的使用來消除 set 借杰,get方法进泼。
使用@Autowired注釋Orderdetail
- Spring 通過一個 BeanPostProcessor 對 @Autowired 進(jìn)行解析缘琅,所以要讓 @Autowired 起作用必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。
- 在bean.xml中 移除 boss Orderdetail 的屬性注入配置的信息刷袍,并聲明 AutowiredAnnotationBeanPostProcessor Bean樊展。
<?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="items" class="pojo.Items" scope="singleton">
<property name="name" value="三星手機(jī)"></property>
<property name="price" value="10000"></property>
</bean>
<bean id="user" class="pojo.User">
<property name="username" value="李四"></property>
<property name="address" value="陝西西安"></property>
</bean>
<!-- 移除 boss Orderdetail 的屬性注入配置的信息 -->
<bean id="orderdetail" class="pojo.Orderdetail">
</bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>
</beans>
- 修改在原來注入spring容器中的bean的方法。在域變量上加上標(biāo)簽@Autowired,并且去掉 相應(yīng)的get 和set方法
Orderdetail.java
package pojo;
import org.springframework.beans.factory.annotation.Autowired;
public class Orderdetail {
private int id;
@Autowired
private Items items;
@Autowired
private User user;
@Override
public String toString() {
return "Orderdetail{" +
"id=" + id +
", items=" + items +
", user=" + user +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
用測試類測試:
-
在默認(rèn)情況下使用 @Autowired 注釋進(jìn)行自動注入時,Spring 容器中匹配的候選 Bean 數(shù)目必須有且僅有一個哥力。當(dāng)找不到一個匹配的 Bean 時吩跋,Spring 容器將拋出BeanCreationException 異常桥温,并指出必須至少擁有一個匹配的 Bean侵浸。
-
當(dāng)不能確定 Spring 容器中一定擁有某個類的 Bean 時掏觉,可以在需要自動注入該類 Bean 的地方可以使用 @Autowired(required = false)履腋,這等于告訴 Spring:在找不到匹配 Bean 時也不報錯。
在 beans.xml 中配置兩個 User類型的 Bean時
<bean id="user" class="pojo.User">
<property name="username" value="李四"></property>
<property name="address" value="陝西西安"></property>
</bean>
<bean id="user1" class="pojo.User">
<property name="username" value="張三"></property>
<property name="address" value="陝西西安"></property>
</bean>
這樣配置時延旧,就會發(fā)生異常,因為Spring 容器將無法確定到底要用哪一個 Bean集畅,Spring 允許我們通過 @Qualifier 注釋指定注入 Bean 的名稱,這樣歧義就消除了赦颇。
@Autowired
@Qualifier("user")
private User user;
- @Qualifier("office") 中的 office 是 Bean 的名稱,所以 @Autowired 和@Qualifier 結(jié)合使用時扇苞,自動注入的策略就從 byType 轉(zhuǎn)變成 byName 了杨拐。@Autowired 可以對成員變量帆阳、方法以及構(gòu)造函數(shù)進(jìn)行注釋蜒谤,而@Qualifier 的標(biāo)注對象是成員變量、方法入?yún)⒔准馈?gòu)造函數(shù)入?yún)ⅰU怯捎谧⑨寣ο蟮牟煌逄辏?Spring 不將 @Autowired 和@Qualifier 統(tǒng)一成一個注釋類。
使用 @Resource 注釋
@Resource 的作用相當(dāng)于 @Autowired挥唠,只不過 @Autowired 按 byType 自動注入猛遍,面@Resource 默認(rèn)按 byName 自動注入罷了梯醒。@Resource 有兩個屬性是比較重要的畜隶,分別是 name 和 type籽慢,Spring 將@Resource 注釋的 name 屬性解析為 Bean 的名字跛锌,而 type 屬性則解析為 Bean 的類型。所以如果使用 name 屬性郑藏,則使用 byName 的自動注入策略必盖,而使用 type 屬性時則使用 byType 自動注入策略。如果既不指定 name 也不指定 type 屬性阁吝,這時將通過反射機(jī)制使用 byName 自動注入策略。
// 自動注入類型為 Items 的 Bean
@Resource
private Items items;
// 自動注入 bean 名稱為 user 的 Bean
@Resource(name = "user")
private User user;
使用 <context:annotation-config/> 簡化配置
Spring 2.1 添加了一個新的 context 的 Schema 命名空間,該命名空間對注釋驅(qū)動定躏、屬性文件引入、加載期織入等功能提供了便捷的配置碧聪。我們知道注釋本身是不會做任何事情的辞嗡,它僅提供元數(shù)據(jù)信息。要使元數(shù)據(jù)信息真正起作用挺狰,必須讓負(fù)責(zé)處理這些元數(shù)據(jù)的處理器工作起來。
而我們前面所介紹的 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor 就是處理這些注釋元數(shù)據(jù)的處理器趁耗。但是直接在 Spring 配置文件中定義這些 Bean 顯得比較笨拙。Spring 為我們提供了一種方便的注冊這些BeanPostProcessor 的方式罢屈,這就是 <context:annotation-config/>。
在bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="items" class="pojo.Items" scope="singleton">
<property name="name" value="三星手機(jī)"></property>
<property name="price" value="10000"></property>
</bean>
<bean id="user" class="pojo.User">
<property name="username" value="李四"></property>
<property name="address" value="陝西西安"></property>
</bean>
<bean id="user1" class="pojo.User">
<property name="username" value="張三"></property>
<property name="address" value="陝西西安"></property>
</bean>
<!-- 移除 boss Orderdetail 的屬性注入配置的信息 -->
<bean id="orderdetail" class="pojo.Orderdetail">
</bean>
<!-- <!–@Autowired–>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>
<!–@Resource–>
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>-->
</beans>
<context:annotationconfig/> 將隱式地向 Spring 容器注冊 AutowiredAnnotationBeanPostProcessor哑芹、CommonAnnotationBeanPostProcessor聪姿、PersistenceAnnotationBeanPostProcessor 以及equiredAnnotationBeanPostProcessor 這 4 個 BeanPostProcessor。
在配置文件中使用 context 命名空間之前,必須在 <beans> 元素中聲明 context 命名空間曹货。
使用 @Component
雖然我們可以通過 @Autowired 或 @Resource 在 Bean 類中使用自動注入功能,但是 Bean 還是在 XML 文件中通過 <bean> 進(jìn)行定義 —— 也就是說礼饱,在 XML 配置文件中定義 Bean,通過@Autowired 或 @Resource 為 Bean 的成員變量蝴韭、方法入?yún)⒒驑?gòu)造函數(shù)入?yún)⑻峁┳詣幼⑷氲墓δ堋D芊褚餐ㄟ^注釋定義 Bean,從 XML 配置文件中完全移除 Bean 定義的配置呢驶忌?答案是肯定的,我們通過 Spring 2.5 提供的@Component 注釋就可以達(dá)到這個目標(biāo)了抒抬。
為什么 @Repository 只能標(biāo)注在 DAO 類上呢?這是因為該注解的作用不只是將類識別為 Bean,同時它還能將所標(biāo)注的類中拋出的數(shù)據(jù)訪問異常封裝為 Spring 的數(shù)據(jù)訪問異常類型。 Spring 本身提供了一個豐富的并且是與具體的數(shù)據(jù)訪問技術(shù)無關(guān)的數(shù)據(jù)訪問異常結(jié)構(gòu)售担,用于封裝不同的持久層框架拋出的異常岩四,使得異常獨(dú)立于底層的框架。
Spring 2.5 在 @Repository 的基礎(chǔ)上增加了功能類似的額外三個注解:@Component耕姊、@Service、@Constroller,它們分別用于軟件系統(tǒng)的不同層次:
@Component 是一個泛化的概念燃辖,僅僅表示一個組件 (Bean) ,可以作用在任何層次。
@Service 通常作用在業(yè)務(wù)層蛋欣,但是目前該功能與 @Component 相同。
@Constroller 通常作用在控制層尚猿,但是目前該功能與 @Component 相同。
通過在類上使用 @Repository踪少、@Component、@Service 和 @Constroller 注解,Spring 會自動創(chuàng)建相應(yīng)的 BeanDefinition 對象,并注冊到 ApplicationContext 中。這些類就成了 Spring 受管組件侨舆。這三個注解除了作用于不同軟件層次的類,其使用方式與 @Repository 是完全相同的。
接下來完全使用注釋定義 Bean 并完成 Bean 之間裝配:
Items .java
package pojo;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component //使用 @Component 注釋就可以將一個類定義為 Spring 容器中的 Bean
public class Items {
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
@Override
public String toString() {
return "Items{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", pic='" + pic + '\'' +
", createtime=" + createtime +
", detail='" + detail + '\'' +
'}';
}
//省略get/set方法
}
User.java
@Component
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
", iId=" + iId +
'}';
}
Orderdetail.java
package pojo;
import javax.annotation.Resource;
public class Orderdetail {
private int id;
// 自動注入類型為 Items 的 Bean
@Resource
private Items items;
// 自動注入 bean 名稱為 user 的 Bean
@Resource(name = "user")
private User user;
@Override
public String toString() {
return "Orderdetail{" +
"id=" + id +
", items=" + items +
", user=" + user +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
升級之后的配置文件beanUp.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="pojo"/>
</beans>
這里茵乱,所有通過 <bean> 元素定義 Bean 的配置內(nèi)容已經(jīng)被移除,僅需要添加一行 <context:component-scan/> 配置就解決所有問題了——Spring XML 配置文件得到了極致的簡化(當(dāng)然配置元數(shù)據(jù)還是需要的,只不過以注釋形式存在罷了)。<context:component-scan/> 的 base-package 屬性指定了需要掃描的類包块饺,類包及其遞歸子包中所有的類都會被處理授艰。