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>子節點支持多種類型的過濾表達式:
組件裝配
<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>
使用@Resource或@Inject自動裝配bean
Spring還支持@Resource和@Inject注解,這兩個注解和@Autowired注解的功用類似
@Resource注解要求提供一個bean名稱的屬性,若該屬性為空,則自動采用標注處的變量或方法名作為bean的名稱
@Inject和@Autowired注解一樣也是按類型注入的bean,但是沒有required屬性
建議使用@Autowired注解
Spring中如何配置Hibernate事務 http://www.linuxidc.com/Linux/2013-12/93681.htm
Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm
基於 Spring 設計並實現 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm
Spring-3.2.4 + Quartz-2.2.0集成實例 http://www.linuxidc.com/Linux/2013-10/91524.htm
使用 Spring 進行單元測試 http://www.linuxidc.com/Linux/2013-09/89913.htm
運用Spring注解實現Netty服務器端UDP應用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm
Spring 3.x 企業應用開發實戰 PDF完整高清掃描版+源代碼 http://www.linuxidc.com/Linux/2013-10/91357.htm
Spring 的詳細介紹:請點這裡
Spring 的下載地址:請點這裡