采用注解的優勢:
- 注解可以充分利用 Java 的反射機制獲取類結構信息,這些信息可以有效減少配置的工作。如使用 JPA 注釋配置 ORM 映射時,我們就不需要指定 PO 的屬性名、類型等信息,如果關系表字段和 PO 屬性名、類型都一致,您甚至無需編寫任務屬性映射信息——因為這些信息都可以通過 Java 反射機制獲取。
- 注解和 Java 代碼位于一個文件中,而 XML 配置采用獨立的配置文件,大多數配置信息在程序開發完成后都不會調整,如果配置信息和 Java 代碼放在一起,有助于增強程序的內聚性。而采用獨立的 XML 配置文件,程序員在編寫一個功能時,往往需要在程序文件和配置文件中不停切換,這種思維上的不連貫會降低開發效率。
以前配置bean的方法及在bean之間建立依賴關系的做法
以用戶購買商品為例主要有四個實體類(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 + '\'' +
'}';
}
}
訂單明細(包含用戶信息與商品信息) 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中,因此創建一個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-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="蘋果手機"></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 (輸出訂單明細時,成功時就會打印用戶與商品的相關信息)
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可以對成員變量、方法和構造函數進行標注,來完成自動裝配的工作,這里必須明確:@Autowired是根據類型進行自動裝配的,如果需要按名稱進行裝配,則需要配合@Qualifier[1]使用;
- @Autowired標注可以放在成員變量上,也可以放在成員變量的set方法上。前者,Spring會直接將UserDao類型的唯一一個bean賦值給userDao這個成員變量;后者,Spring會調用setUserDao方法來將UserDao類型的唯一一個bean裝配到userDao這個屬性。
- Spring 2.5 引入了 @Autowired 注釋,它可以對類成員變量、方法及構造函數進行標注,完成自動裝配的工作。 通過 @Autowired的使用來消除 set ,get方法。
使用@Autowired注釋Orderdetail
- Spring 通過一個 BeanPostProcessor 對 @Autowired 進行解析,所以要讓 @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="三星手機"></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的方法。在域變量上加上標簽@Autowired,并且去掉 相應的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;
}
}
用測試類測試:
-
在默認情況下使用 @Autowired 注釋進行自動注入時,Spring 容器中匹配的候選 Bean 數目必須有且僅有一個。當找不到一個匹配的 Bean 時,Spring 容器將拋出BeanCreationException 異常,并指出必須至少擁有一個匹配的 Bean。
-
當不能確定 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>
這樣配置時,就會發生異常,因為Spring 容器將無法確定到底要用哪一個 Bean,Spring 允許我們通過 @Qualifier 注釋指定注入 Bean 的名稱,這樣歧義就消除了。
@Autowired
@Qualifier("user")
private User user;
- @Qualifier("office") 中的 office 是 Bean 的名稱,所以 @Autowired 和@Qualifier 結合使用時,自動注入的策略就從 byType 轉變成 byName 了。@Autowired 可以對成員變量、方法以及構造函數進行注釋,而@Qualifier 的標注對象是成員變量、方法入參、構造函數入參。正是由于注釋對象的不同,所以 Spring 不將 @Autowired 和@Qualifier 統一成一個注釋類。
使用 @Resource 注釋
@Resource 的作用相當于 @Autowired,只不過 @Autowired 按 byType 自動注入,面@Resource 默認按 byName 自動注入罷了。@Resource 有兩個屬性是比較重要的,分別是 name 和 type,Spring 將@Resource 注釋的 name 屬性解析為 Bean 的名字,而 type 屬性則解析為 Bean 的類型。所以如果使用 name 屬性,則使用 byName 的自動注入策略,而使用 type 屬性時則使用 byType 自動注入策略。如果既不指定 name 也不指定 type 屬性,這時將通過反射機制使用 byName 自動注入策略。
// 自動注入類型為 Items 的 Bean
@Resource
private Items items;
// 自動注入 bean 名稱為 user 的 Bean
@Resource(name = "user")
private User user;
使用 <context:annotation-config/> 簡化配置
Spring 2.1 添加了一個新的 context 的 Schema 命名空間,該命名空間對注釋驅動、屬性文件引入、加載期織入等功能提供了便捷的配置。我們知道注釋本身是不會做任何事情的,它僅提供元數據信息。要使元數據信息真正起作用,必須讓負責處理這些元數據的處理器工作起來。
而我們前面所介紹的 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor 就是處理這些注釋元數據的處理器。但是直接在 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="三星手機"></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> 進行定義 —— 也就是說,在 XML 配置文件中定義 Bean,通過@Autowired 或 @Resource 為 Bean 的成員變量、方法入參或構造函數入參提供自動注入的功能。能否也通過注釋定義 Bean,從 XML 配置文件中完全移除 Bean 定義的配置呢?答案是肯定的,我們通過 Spring 2.5 提供的@Component 注釋就可以達到這個目標了。
為什么 @Repository 只能標注在 DAO 類上呢?這是因為該注解的作用不只是將類識別為 Bean,同時它還能將所標注的類中拋出的數據訪問異常封裝為 Spring 的數據訪問異常類型。 Spring 本身提供了一個豐富的并且是與具體的數據訪問技術無關的數據訪問異常結構,用于封裝不同的持久層框架拋出的異常,使得異常獨立于底層的框架。
Spring 2.5 在 @Repository 的基礎上增加了功能類似的額外三個注解:@Component、@Service、@Constroller,它們分別用于軟件系統的不同層次:
@Component 是一個泛化的概念,僅僅表示一個組件 (Bean) ,可以作用在任何層次。
@Service 通常作用在業務層,但是目前該功能與 @Component 相同。
@Constroller 通常作用在控制層,但是目前該功能與 @Component 相同。
通過在類上使用 @Repository、@Component、@Service 和 @Constroller 注解,Spring 會自動創建相應的 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 的配置內容已經被移除,僅需要添加一行 <context:component-scan/> 配置就解決所有問題了——Spring XML 配置文件得到了極致的簡化(當然配置元數據還是需要的,只不過以注釋形式存在罷了)。<context:component-scan/> 的 base-package 屬性指定了需要掃描的類包,類包及其遞歸子包中所有的類都會被處理。