Spring--通過註解來配置bean
Spring通過註解配置bean
基於註解配置bean
基於註解來配置bean的屬性
在classpath中掃描元件
元件掃描(component scanning):Spring能夠從classpath下自動掃描,偵測和例項化具有特定註解的元件。
特定的元件包括:
[email protected]:基本註解,標識了一個受Spring管理的元件
[email protected]:標識持久層元件
[email protected]:標識服務層(業務層)元件
[email protected]:標識表現層元件
對於掃描到的元件,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的名稱
[email protected]註解也可以應用在集合屬性上,此時Spring讀取該集合的型別資訊,然後自動裝配所有與之相容的bean
[email protected]註解用在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--通過註解來配置bean
Spring通過註解配置bean 基於註解配置bean 基於註解來配置bean的屬性 在classpath中掃描元件 元件掃描(component scanning):Spring能夠從classpath下自動掃描,偵測和例項化具有特定註解的元件。 特定的元件包括: [email
Spring中通過註解來配置bean以及自動注入
今天看到一篇好文章,寫的很是詳細。再加上自己的理解和補充,成了這一篇文章。文後會獻上原文連結。 使用Spring經常性的需要: 通過註解配置bean 基於註解配置bean 基於註解來配置bean的屬性 ----------------------
Spring 通過FactoryBean來配置bean
Spring 通過FactoryBean來配置bean 標籤(空格分隔): Spring 除了之前全類名和工廠方法這兩種形式來配置bean之外,還可以使用FactoryBean的形式來配置bean。下面是一個例子: 首先先定義一個類,實現了actoryBean這個介面: p
Spring MVC通過註解(annotation)配置Bean
Spring能夠在classpath下自動掃描,偵測和例項化具有特定註解的元件,這在Spring中稱為元件掃描(Component scanning).特定元件的註解包括: @Component:基本註解,標識了一個受spring管理的元件. @Reposit
Spring中如何使用註解來配置Bean?有哪些相關的註解?
首先需要在Spring配置檔案中增加如下配置:<context:component-scan base-package="org.example"/>然後可以用@Component、@Con
spring之通過註解方式配置Bean(一)
(1)元件掃描:spring能夠從classpath下自動掃描、偵測和例項化具有特定註解的元件。 (2)特定元件包括: @Component:基本註解,標識一個受spring管理的元件; @Respority:標識持久層元件; @Service:標識服務層(業務層)元件; @Controller:標識表現層
Spring通過工廠方法配置Bean
前言: Spring配置Bean的方法有很多,這裡只介紹通過工廠方法配置Bean。 所謂工廠即含有批量的Bean,可根據傳入的引數條件返回對應的Bean例項。 工廠又分兩種: 靜態工廠通過靜態方
Spring 通過兩種工廠方法來配置bean
在Spring框架中建立Bean例項的時候中, 我們通常會利用配置檔案,也就是XML檔案形式 或者 annotation註解方式來配置bean. 在第一種利用配置檔案方式中, 還包括如下三小類 1、反射模式 2、工廠方法模式(本文重點) 3、Factory Be
spring通過工廠方法和FactoryBean來配置Bean
spring通過工廠方法配置Bean 1.通過呼叫靜態工廠方法建立bean(直接呼叫某一個類的靜態方法就可以返回bean的例項) 呼叫靜態工廠方法建立Bean是將物件建立的過程封裝到靜態方法中,當客戶端需要物件時,只需要簡單的呼叫靜態方法, 而不關心建立物件的細節。 要宣告
Spring通過註解配置bean
簡介及測試 Spring支援通過註解的方式來配置bean例項。Spring能夠從classpath下自動掃描,偵測和例項化具有特定註解的元件。特定元件包括: @Component:基本註解,標識了一個受Spring管理的元件。 @Reposit
spring----通過實現FactoryBean介面來配置Bean----筆記
Bean配置檔案 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="htt
Spring通過註解配置bean的xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/b
Spring(十三):使用工廠方法來配置Bean的兩種方式(靜態工廠方法&實例工廠方法)
color 示例 簡單的 rgs icc tostring pac ng- clas 通過調用靜態工廠方法創建Bean 1)調用靜態工廠方法創建Bean是將對象創建的過程封裝到靜態方法中。當客戶端需要對象時,只需要簡單地調用靜態方法,而不需要關心創建對象的具體細節。 2
spring-boot 之Lombok的使用,通過註解來省略一些常用程式碼,set get 日誌等
如果使用IDEA要先安裝lombok外掛 三、注意:如果註解@Slf4j注入後找不到變數log,那就給IDE安裝lombok外掛,、 下面以idea為例 1、File → settings → Plugins, 然後點選“Browse repositori
spring-boot 之Lombok的使用,通過註解來省略一些常用程式碼,set get 日誌等
如果使用IDEA要先安裝lombok外掛 三、注意:如果註解@Slf4j注入後找不到變數log,那就給IDE安裝lombok外掛,、 下面以idea為例 1、File → settings → Plugins, 然後點選“Browse repositories” 如
Spring通過註解annotation方式注入Bean時,採用動態代理,那麼JDK代理和CGLIB代理區別?
切面程式設計是Spring中非常重要的一個模組,切面程式設計的實現原理是動態代理,那麼動態代理又有兩種實現方式:一種方法是直接實現JDK中的InvocationHandler介面,另一種方法是繼承CGLIB。 首先如果不是很清楚兩者的區別的話,記住一般情況下Invocati
spring通過名稱獲取配置的bean例項
/** * 服務工廠介面. * * * */ public interface ServiceFactory extends Serializable { /** * 獲取服務,通過class. * * @param c
spring學習筆記(12)——使用註解方式配置bean
常用的註解 常用的有四個註解 Controller: 用於控制器的註解 Service : 用於service的註解 Component: 用於基本元件的註解 Repository:用於Dao層的註解 其實,對於sprin
Spring程式碼例項系列-05:通過註解@Configuration、@Bean和@Import定義bean
Spring中有三種定義Bean的方式: 1. *.xml/<bean>/<import> 2. @Configuration/@Bean/@Import 3. @Component/@Controller/@Service/@
Spring基於Java類配置Bean(四):通過@Configuration配置類引用xml配置資訊
總結:在@Configuration配置類中,通過@ImportResource就可以引入xml配置檔案,在配置類中可直接通過@AutoWired引用xml檔案中定義的bean。(1)XML配置檔案<?xml version="1.0" encoding="UTF-8"