天天看點

Spring--通過注解來配置bean【轉】

Spring通過注解配置bean

  基于注解配置bean

  基于注解來配置bean的屬性

在classpath中掃描元件

  元件掃描(component scanning):Spring能夠從classpath下自動掃描,偵測和執行個體化具有特定注解的元件。

  特定的元件包括:

    -@Component:基本注解,辨別了一個受Spring管理的元件

    -@Responsitory:辨別持久層元件

    -@Service:辨別服務層(業務層)元件

    -@Controller:辨別表現層元件

  對于掃描到的元件,Spring有預設的命名政策:使用非限定類名,第一個字母小寫。也可以在注解中通過value屬性值辨別元件的名稱。

  當在元件類上使用了特定的注解之後,還需要在Spring的配置檔案中聲明<context:component-scan>:

    base-package屬性指定一個需要掃描的基類包,Spring容器将會掃描這個基類包裡及其子包中的所有類

    當需要掃描多個包時,可以使用逗号分隔

    如果僅希望掃描特定的類而非基包下的所有類,可使用resource-pattern屬性過濾特定的類,示例:

    <context:component-sacn base-package="com.yl.spring.beans" resource-pattern="autowire/*.class"/>

    <context:include-filter>子節點表示要包含的目标類

    <context:exclude-filter>子節點表示要排除在外的目标類

    <context:component-sacn>下可以擁有若幹個<context:include-filter>和<context:exclude-filter>子節點

<context:include-filter>和<context:exclude-filter>子節點支援多種類型的過濾表達式:

類别 示例 說明
annotation  com.yl.XxxAnnotation 所有标注了XxxAnnotation的類,該類型采用目标類是否标注了某個注解進行過濾
assinable  com.yl.XxxService 所有繼承或擴充XxxService的類,該類型采用了目标類是否繼承或擴充某個特定類進行過濾
aspectj  com.yl.*Service 所有類名義Service結束的類及繼承或擴充它們的類,該類型采用AspectJ表達式進行過濾
regex  com.yl.anno.* 所有com.yl.anno包下的類。該類型采用正規表達式,根據類的類名進行過濾
custom  com.yl.XxxTypeFilter  采用XxxTypeFilter通過代碼的方式定義過濾原則。該類必須實作org.springframework.core.type.TypeFilter接口

元件裝配

<context:component-scan>元素還會自動注冊AutowiredAnnotationBeanPostProcessor執行個體,該執行個體可以自動裝配具有@Autowired和@Resource、和@Inject注解的屬性

使用@Autowired自動裝配bean

  @Autowired注解自動裝配具有相容類型的單個bean屬性

  -構造器,普通字段(即使是非public),一切隻有參數的方法都可以應用@Autowired

  -預設情況下,所有使用@Autowired注解的屬性都需要被設定,當Spring找不到比對的bean裝配屬性時,會抛出異常。若某一屬性允許不被設定,可以設定@Autowired注解的required屬性為false

  -預設情況下,當IOC容器裡存在多個類型相容的bean時,通過類型的自動裝配将無法工作。此時可以在@Qualifiter注解裡提供bean的名稱,Spring允許對方法的入參标注 @Qualifiter已指定注入bean的名稱

  -@Autowired注解也可以應用在數組類型的屬性上,此時Spring将會把所有比對的bean進行自動比對

  -@Autowired注解也可以應用在集合屬性上,此時Spring讀取該集合的類型資訊,然後自動裝配所有與之相容的bean

  -@Autowired注解用在java.util.Map上時,若該Map的鍵值作為String,那麼Spring将自動裝配與之Map值類型相容的bean,此時bean的名稱作為鍵值

TestObject.java

1 package com.yl.annotation;
2 
3 import org.springframework.stereotype.Component;
4 
5 @Component
6 public class TestObject {
7     
8 }      

UserRepository.java接口

1 package com.yl.annotation.repository;
2 
3 public interface UserRepository {
4     public void save();
5     
6 }      

UserRepositoryImpl.java

1 package com.yl.annotation.repository;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Repository;
 5 
 6 import com.yl.annotation.TestObject;
 7 
 8 @Repository
 9 //@Repository("userRepository")
10 public class UserRepositoryImpl implements UserRepository {
11     
12     @Autowired(required=false)
13     private TestObject testObject;
14     
15     @Override
16     public void save() {
17         System.out.println("UserRepository save...");
18         System.out.println(testObject);
19     }
20 
21 }      

UserJdbcRepository.java

1 package com.yl.annotation.repository;
 2 
 3 import org.springframework.stereotype.Repository;
 4 
 5 @Repository
 6 public class UserJdbcRepository implements UserRepository {
 7 
 8     @Override
 9     public void save() {
10         System.out.println("UserJdbcRepository save...");
11     }
12 
13 }      

UserService.java

1 package com.yl.annotation.service;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.beans.factory.annotation.Qualifier;
 5 import org.springframework.stereotype.Service;
 6 
 7 import com.yl.annotation.repository.UserRepository;
 8 
 9 @Service
10 public class UserService {
11     @Autowired
12     @Qualifier("userJdbcRepository")
13     private UserRepository userRepository;
14     
15     /*@Autowired
16     @Qualifier("userJdbcRepository")
17     public void setUserRepository(UserRepository userRepository) {
18         this.userRepository = userRepository;
19     }*/
20     
21     public void add() {
22         System.out.println("UserService add...");
23         userRepository.save();
24     }
25 }      

UserController.java

1 package com.yl.annotation.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 
 6 import com.yl.annotation.service.UserService;
 7 
 8 @Controller
 9 public class UserController {
10     @Autowired
11     private UserService userService;
12     
13     public void execute() {
14         System.out.println("UserController execute...");
15         userService.add();
16     }
17 }      

beans-annotation.xml

1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
 7 
 8     <!-- 指定Spring IOC容器掃描的包 -->
 9     <!-- 可以通過resource-pattern指定掃描的資源 -->
10     <!-- <context:component-scan 
11         base-package="com.yl.annotation"
12         resource-pattern="repository/*.class"></context:component-scan> -->
13     
14     <!-- context:exclude-filter 子節點指定排除哪些指定表達式的元件 -->    
15     <!-- context:include-filter 子節點指定包含哪些指定表達式的元件, 該子節點需要use-default-filters配合使用 -->    
16     <context:component-scan 
17         base-package="com.yl.annotation" >
18         <!-- use-default-filters="false"> -->
19         <!-- <context:exclude-filter type="annotation" 
20             expression="org.springframework.stereotype.Repository"/> -->
21         
22         <!-- <context:include-filter type="annotation" 
23             expression="org.springframework.stereotype.Repository"/> -->
24             
25         <!-- <context:exclude-filter type="assignable" 
26             expression="com.yl.annotation.repository.UserRepository"/> -->
27         
28         <!-- <context:include-filter type="annotation" 
29             expression="com.yl.annotation.repository.UserRepository"/> -->
30     </context:component-scan>
31 </beans>      
Spring--通過注解來配置bean【轉】