常用spring3註解

2021-07-28 03:20:47 字數 750 閱讀 8981

@configuration把乙個類作為乙個ioc容器,它的某個方法頭上如果註冊了@bean,就會作為這個spring容器中的bean。

@scope註解 作用域

@lazy(true) 表示延遲初始化

@service用於標註業務層元件、 

@controller用於標註控制層元件(如struts中的action)

@repository用於標註資料訪問元件,即dao元件。

@component泛指元件,當元件不好歸類的時候,我們可以使用這個註解進行標註。

@scope用於指定scope作用域的(用在類上)

@postconstruct用於指定初始化方法(用在方法上)

@predestory用於指定銷毀方法(用在方法上)

@dependson:定義bean初始化及銷毀時的順序

@primary:自動裝配時當出現多個bean候選者時,被註解為@primary的bean將作為首選者,否則將丟擲異常

@autowired 預設按型別裝配,如果我們想使用按名稱裝配,可以結合@qualifier註解一起使用。如下:

@autowired @qualifier("persondaobean") 存在多個例項配合使用

@resource預設按名稱裝配,當找不到與名稱匹配的bean才會按型別裝配。

@postconstruct 初始化註解

@predestroy 摧毀註解 預設 單例  啟動就載入

@async非同步方法呼叫,需要新增以下**:

spring3 基於註解的AOP

要點 1.aop的概念真的很多。其實從使用出發無非兩點 1,定義要攔截的方法,2,實現攔截後的操作方法。2.基於註解的 aspect需要配合bean宣告來用,不然不報錯,不執行。官方doc貌似沒提倒。3.注入的bean物件,訪問其屬性需要生成get set方法,如果直接訪問也會出現空指標。autow...

基於spring 3 註解的junit測試

import org.junit.test import org.junit.runner.runwith import org.springframework.beans.factory.annotation.autowired import org.springframework.test.co...

spring 3中新增的 value註解

在spring 3.0中,可以通過使用 value,對一些如 properties檔案 中的檔案,進行鍵值對的注入,例子如下 的命名空間,然後 2 3 建立test.properties abc 123 4 import org.springframework.beans.factory.annot...