Spring註解解釋

2021-07-25 01:54:34 字數 4400 閱讀 9288

@controller

public

class

softcreatecontroller

extends

******basecontroller {}

@controller("softcreatecontroller")

@controller負責註冊乙個bean到spring上下文中,bean的id預設為類名稱開頭字母小寫

@service

public

class

softcreateserviceimpl

implements

isoftcreateservice {}

@service("softcreateserviceimpl")

@service負責註冊乙個bean到spring上下文中,bean的id預設為類名稱開頭字母小寫

@autowired

private isoftpmservice softpmservice;

@autowired(required=false)

private isoftpmservice softpmservice = new softpmserviceimpl();

@autowired根據bean型別從spring上線文中進行查詢,註冊型別必須唯一,否則報異常。與@resource的區別在於,@resource允許通過bean名稱或bean型別兩種方式進行查詢。

@autowired(required=false)表示,如果spring上下文中沒有找到該型別的bean時,才會使用new softpmserviceimpl();

@autowired標註作用於map型別時,如果map的key為string型別,則spring會將容器中所有型別符合map的value對應的型別的bean增加進來,用bean的id或name作為map的key。

@controller

public

class

bbtforumcontroller

}

如果我們使用以下的url請求:

http://localhost/bbtforum.do?method=listboardtopic&topicid=1&userid=10&username=tom
topicid url引數將繫結到topicid入參上,而userid和 username url引數將繫結到user物件的userid和 username屬性中。和url請求中不允許沒有topicid引數不同,雖然user的userid屬性的型別是基本資料型別,但如果 url中不存在userid引數,spring也不會報錯,此時 user.userid值為0。如果user物件擁有乙個dept.deptid的級聯屬性,那麼它將和dept.deptid url引數繫結。

@requestparam(required=false):引數不是必須的,預設為true

@requestparam(value=」id」,required=false)

作用域:request

public string handleinit(@modelattribute("querybean") manageduser suser,model model,){}

@modelattribute("coopmap")//將coopmap返回到頁面

public mapcoopmapitems(){}

@modelattribute宣告在屬性上,表示該屬性的value**於model裡」querybean」,並被儲存到model裡@modelattribute宣告在方法上,表示該方法的返回值被儲存到model裡

@cacheable:宣告乙個方法的返回值應該被快取

@cacheable(modelid = "testcaching")
@cacheflush :宣告乙個方法是清空快取的觸發器

@cacheflush(modelid = "testcaching")
說明:要配合快取處理器使用,

參考:

@resource

private datasource datasource; // inject the bean named 'datasource'

@resource(name="datasource")

@resource(type=datasource.class)

@postconstruct

在方法上加上註解@postconstruct,這個方法就會在bean 初始化之後被spring容器執行(注:bean初始化包括,例項化bean,並裝配bean的屬性(依賴注入)。

@predestroy

在方法上加上註解@predestroy,這個方法就會在bean被銷毀前被spring容器執行。

與@controller、@service類似,都是向spring上下文中註冊bean

@component是所有受spring管理元件的通用形式,spring還提供了更加細化的註解形式: @repository 、@service、@controller,它們分別對應儲存層bean,業務層bean,和展示層bean。

spring2.5版本中,這些註解與@component的語義是一樣的,完全通用,在spring 以後的版本中可能會給它們追加更多的語義。所以,我們推薦使用@repository 、@service 、@controller來替代@component。

@scope("session")

@repository()

public

class

usersessionbean

implements

serializable {}

在使用xml定義bean時,可以通過bean的scope屬性來定義乙個bea 的作用範圍,同樣可以通過@scope註解來完成。

@scope中可以指定如下值:

1. singleton:定義bean的範圍為每個spring容器乙個例項(預設值)

2. prototype:定義bean可以被多次例項化(使用一次就建立一次)

3. request:定義bean的範圍是http請求(springmvc中有效)

4. session:定義bean的範圍是http會話(springmvc中有效)

5. global-session:定義bean的範圍是全域性http會話(portlet中有效)

spring允許我們有選擇地指定modelmap中的哪些屬性需要轉存到session中,以便下乙個請求屬對應的modelmap的屬性列表中還能訪問到這些屬性。

這一功能是通過類定義處標註@sessionattributes註解來實現的。

@sessionattributes只能宣告在類上,而不能宣告在方法上。

@sessionattributes("curruser")//將modelmap中屬性名為curruser的屬性

@sessionattributes()

@sessionattributes(types = user.class)

@sessionattributes(types = )

@sessionattributes(types = ,value=)

如果希望某個屬性編輯器僅作用於特定的controller,可以在controller中定義乙個標註@initbinder註解的方法,可以在該方法中向controller了註冊若干個屬性編輯器

@initbinder

public

void

initbinder(webdatabinder binder)

@required

public

setname(string name){}

@required負責檢查乙個bean在初始化時其宣告的set方法是否被執行,當某個被標註了@required的setter方法沒有被呼叫,則spring在解析的時候會丟擲異常,以提醒開發者對相應屬性進行設定。 @required註解只能標註在 setter方法之上。因為依賴注入的本質是檢查setter方法是否被呼叫了,而不是真的去檢查屬性是否賦值了以及賦了什麼樣的值。如果將該註解標註在非set***x()型別的方法則被忽略。

@autowired

@qualifier("softservice")

private isoftpmservice softpmservice;

使用@autowired時,如果找到多個同一型別的bean,則會拋異常,此時可以使用@qualifier(「beanname」),明確指定bean的名稱進行注入,此時與@resource指定name屬性作用相同。

Spring的幾個常用註解解釋

resource 獲取spring容器中象 依賴注入 configuration 申明這是乙個配置類 componentscan basepackages 定義掃瞄的路徑從中找出標識了需要裝配的類自動裝配到spring的bean容器中 spring框架跟junit整合 runwith springj...

spring中 註解的相關解釋

component controller service repository 在annotaion配置註解中用 component來表示乙個通用注釋用於說明乙個類是乙個spring容器管理的類。即就是該類已經拉入到spring的管理中了。而 controller,service,repositor...

Spring註解 Import註解

常用的匯入註解分類 註冊自己寫的類service dao controller可用包掃瞄 元件標註註解 controller service repository component bean 匯入的第三方包裡面的元件 import 快速給容器中匯入乙個元件 1 import 要匯入到容器中的元件 ...