一,使用注解:
在spring的配置文件applicationcontext.xml中,加入注解掃描。配置項就配置了對指定的包進行掃描,以實現依賴注入。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?xml version= "1.0" encoding= "utf-8" ?> <span style= "font-size:18px;" ><beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns:context= "http://www.springframework.org/schema/context" xmlns:aop= "http://www.springframework.org/schema/aop" xsi:schemalocation=" http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http: //www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <aop:aspectj-autoproxy/> <context:annotation-config/> <context:component-scan base- package = "com.test" /> //去哪掃描 </beans> |
二,常見注解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@controller @service @autowired @requestmapping @requestparam @modelattribute @cacheable @cacheflush @resource @postconstruct @predestroy @repository @component (不推薦使用) @scope @sessionattributes @initbinder @required @qualifier |
三,常用spring注解:
@controller(運用于表現層)
使用@controller注解標識useraction之后,就表示要把useraction交給spring容器管理,在spring容器中會存在一個名字為"useraction"的action,這個名字是根據useraction類名來取的。注意:如果@controller不指定其value【@controller】,則默認的bean名字為這個類的類名首字母小寫,如果指定value【@controller(value="useraction")】或者【@controller("useraction")】,則使用value作為bean的名字。
這里的useraction還使用了@scope注解,@scope("prototype")表示將action的范圍聲明為原型,可以利用容器的scope="prototype"來保證每一個請求有一個單獨的action來處理,避免struts中action的線程安全問題。spring 默認scope是單例模式(scope="singleton"),這樣只會創建一個action對象,每次訪問都是同一action對象,數據不安全,struts2 是要求每次次訪問都對應不同的action,scope="prototype" 可以保證當有請求的時候都創建一個action對象
@controller
1
2
3
|
@scope ( "prototype" ) public class useraction extends baseaction<user>{ } |
@ service(運用于業務邏輯層)
注意,@service注解是在服務接口的實現類下,而不是接口中使用。
這里很好的體現了spring中的控制反轉,我們不在讓對象自己去實例化對象,去主動依賴對象,而是 專門用一個容器來創建對象,由ioc進行管理。實例:
action要使用userserviceimpl時,就不用主動去創建userserviceimpl的實例了,創建userserviceimpl實例已經交給spring來做了,spring把創建好的userserviceimpl實例給action,action拿到就可以直接用了。action由原來的主動創建userserviceimpl實例后就可以馬上使用,變成了被動等待由spring創建好userserviceimpl實例之后再注入給action,action才能夠使用。這說明action對“userserviceimpl”類的“控制權”已經被“反轉”了,原來主動權在自己手上,自己要使用“userserviceimpl”類的實例,自己主動去new一個出來馬上就可以使用了,但現在自己不能主動去new“userserviceimpl”類的實例,new“userserviceimpl”類的實例的權力已經被spring拿走了,只有spring才能夠new“userserviceimpl”類的實例,而action只能等spring創建好“userserviceimpl”類的實例后,再“懇求”spring把創建好的“userserviceimpl”類的實例給他,這樣他才能夠使用“userserviceimpl”,這就是spring核心思想“控制反轉”,也叫“依賴注入”,“依賴注入”也很好理解,action需要使用userserviceimpl干活,那么就是對userserviceimpl產生了依賴,spring把acion需要依賴的userserviceimpl注入(也就是“給”)給action,這就是所謂的“依賴注入”。對action而言,action依賴什么東西,就請求spring注入給他,對spring而言,action需要什么,spring就主動注入給他。
1
2
3
|
@service ( "userservice" ) public class userserviceimpl implements userservice { } |
@ repository(運用于數據管理層)
筆者是使用工具反向生成的實體層數據model跟mapper,所以也沒用到這個注解,不過這個就是簡單的向spring容器中注入一個bean
1
2
3
|
@repository (value= "userdao" ) public class userdaoimpl extends basedaoimpl<user> { } |
四,常用springmvc注解:
@autowired(按類型注入)
對類成員變量、方法及構造函數進行標注,完成自動裝配的工作。簡單來說就是調用bean,告訴spring這個存在與被管理的容器中。
@autowired 根據bean 類型從spring 上線文中進行查找,注冊類型必須唯一,否則報異常
@autowired 標注作用于 map 類型時,如果 map 的 key 為 string 類型,則 spring 會將容器中所有類型符合 map 的 value 對應的類型的 bean 增加進來,用 bean 的 id 或 name 作為 map 的 key。
@autowired 還有一個作用就是,如果將其標注在 beanfactory 類型、applicationcontext 類型、resourceloader 類型、applicationeventpublisher 類型、messagesource 類型上,那么 spring 會自動注入這些實現類的實例,不需要額外的操作。
@autowired
private ireportservice reportservice ;
@resource(按名稱注入)
跟@autowired類似,@resource 默認按bean的name進行查找,如果沒有找到會按type進行查找
@resource
private datasource datasource; // inject the bean named 'datasource'
@resource(name="datasource")
@resource(type=datasource.class)
延伸問題:什么是按類型進行裝配,什么是按名稱進行裝配?
所謂按類型,就是當spring容器中存在一個與指定屬性類型相同的bean,那么將該屬性進行自動裝配;如果存在多個該類型的bean,那么跑出異常,并指出不能使用按類型進行自動裝配;如果沒有找到匹配的bean,則什么事都不發生。
所謂按名稱,即根據屬性名進行自動裝配,此項會檢查spring容器中與該屬性名完全一致的的bean,進行自動裝配。
@requestmapping(映射請求地址)
用來處理請求地址映射的注解,可用于類或方法上。用于類上,表示類中的所有響應請求的方法都是以該地址作為父路徑。
其中有六個屬性,分別為:
1、 value, method;
value: 指定請求的實際地址,指定的地址可以是uri template 模式(后面將會說明);
method: 指定請求的method類型, get、post、put、delete等;
2、consumes,produces
consumes: 指定處理請求的提交內容類型(content-type),例如application/json, text/html;
produces: 指定返回的內容類型,僅當request請求頭中的(accept)類型中包含該指定類型才返回;
3、params,headers
params: 指定request中必須包含某些參數值是,才讓該方法處理。
headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請求。
1
2
3
4
5
6
7
8
9
|
@controller @requestmapping ( "/bbtforum.do" ) public class bbtforumcontroller { @requestmapping (params = "method=listboardtopic" ) public string listboardtopic( int topicid,user user) {} } @requestmapping ( "/softpg/downsoftpg.do" ) @requestmapping (value= "/softpg/ajaxloadsoftid.do" , method=requestmethod.post) @requestmapping (value= "/osu/product/detail.do" , params={ "modify=false" }, method=requestmethod.post) |
@requestparam(獲取請求參數的值)
比如我們在瀏覽器的訪問地址是:localhost:8080/hello?id=1000,則拿到id的值,例如:
1
2
3
4
5
6
7
8
|
@restcontroller public class hellocontroller { @requestmapping (value= "/hello" ,method= requestmethod.get) public string sayhello( @requestparam ( "id" ) integer id){ return "id:" +id; } } |
@pathvaribale (獲取url中的數據)
1
2
3
4
5
6
7
8
|
@restcontroller public class hellocontroller { @requestmapping (value= "/hello/{id}" ,method= requestmethod.get) public string sayhello( @pathvariable ( "id" ) integer id){ return "id:" +id; } } |
@responsebody(返回類型為json)
作用: 該注解用于將controller的方法返回的對象,通過適當的httpmessageconverter轉換為指定格式后,寫入到response對象的body數據區。
使用時機:返回的數據不是html標簽的頁面,而是其他某種格式的數據時(如json、xml等)使用;
總結
以上所述是小編給大家介紹的spring springmvc中常用注解解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/lwh-note/p/9098797.html