国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Spring注解@Component、@Repository、@Service、@Controller區(qū)別

2019-11-14 20:49:13
字體:
供稿:網(wǎng)友
SPRing注解@Component、@Repository、@Service、@Controller區(qū)別

Spring注解@Component、@Repository、@Service、@Controller區(qū)別

Spring 2.5 中除了提供 @Component 注釋外,還定義了幾個擁有特殊語義的注釋,它們分別是:@Repository、@Service 和 @Controller。在目前的 Spring 版本中,這 3 個注釋和 @Component 是等效的,但是從注釋類的命名上,很容易看出這 3 個注釋分別和持久層、業(yè)務(wù)層和控制層(Web 層)相對應(yīng)。雖然目前這 3 個注釋和 @Component 相比沒有什么新意,但 Spring 將在以后的版本中為它們添加特殊的功能。所以,如果 Web 應(yīng)用程序采用了經(jīng)典的三層分層結(jié)構(gòu)的話,最好在持久層、業(yè)務(wù)層和控制層分別采用 @Repository、@Service 和 @Controller 對分層中的類進行注釋,而用 @Component 對那些比較中立的類進行注釋。在一個稍大的項目中,通常會有上百個組件,如果這些組件采用xml的bean定義來配置,顯然會增加配置文件的體積,查找以及維護起來也不太方便。 Spring2.5為我們引入了組件自動掃描機制,他可以在類路徑底下尋找標注了@Component,@Service,@Controller,@Repository注解的類,并把這些類納入進spring容器中管理。它的作用和在xml文件中使用bean節(jié)點配置組件時一樣的。要使用自動掃描機制,我們需要打開以下配置信息:java代碼1. <?xml version="1.0" encoding="UTF-8" ?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd" 2. > 3. 4. <context:component-scan base-package=”com.eric.spring”>5. </beans> 6. 其中base-package為需要掃描的包(含所有子包) @Service用于標注業(yè)務(wù)層組件,@Controller用于標注控制層組件(如struts中的action),@Repository用于標注數(shù)據(jù)訪問組件,即DAO組件,而@Component泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注。7. @Service public class VentorServiceImpl implements iVentorService{ 8. } @Repository public class VentorDaoImpl implements iVentorDao { 9. } getBean的默認名稱是類名(頭字母小寫),如果想自定義,可以@Service(“aaaaa”)這樣來指定,這種bean默認是單例的,如果想改變,可以使用@Service(“beanName”)@Scope(“prototype”)來改變。可以使用以下方式指定初始化方法和銷毀方法(方法名任意): @PostConstructpublic void init() { 10. } 11. @PreDestroy public void destory() { 12. }注入方式:把DAO實現(xiàn)類注入到service實現(xiàn)類中,把service的接口(注意不要是service的實現(xiàn)類)注入到action中,注入時不要new 這個注入的類,因為spring會自動注入,如果手動再new的話會出現(xiàn)錯誤,然后屬性加上 @Autowired后不需要getter()和setter()方法,Spring也會自動注入。至于更具體的內(nèi)容,等對注入的方式更加熟練后會做個完整的例子上來。注解:在 spring的配置文件里面只需要加上<context:annotation-config/>和<context:component-scanbase-package="需要實現(xiàn)注入的類所在包"/>,可以使用base-package="*"表示全部的類。< context:component-scan base-package=”com.eric.spring”>其中base-package為需要掃描的包(含所有子包)在接口前面標上@Autowired和@Qualifier注釋使得接口可以被容器注入,當接口存在兩個實現(xiàn)類的時候必須指定其中一個來注入,使用實現(xiàn)類首字母小寫的字符串來注入,如: @Autowired @Qualifier("chinese") private Man man;否則可以省略,只寫@Autowired。 @Service服務(wù)層組件,用于標注業(yè)務(wù)層組件,表示定義一個bean,自動根據(jù)bean的類名實例化一個首寫字母為小寫的bean,例如Chinese實例化為chinese,如果需要自己改名字則:@Service("你自己改的bean名")。 @Controller用于標注控制層組件(如struts中的action) @Repository持久層組件,用于標注數(shù)據(jù)訪問組件,即DAO組件 @Component泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注。 @Service public class VentorServiceImpl implements iVentorService { } @Repository public class VentorDaoImpl implements iVentorDao { } getBean 的默認名稱是類名(頭字母小寫),如果想自定義,可以@Service(“aaaaa”) 這樣來指定,這種bean默認是單例的,如果想改變,可以使用@Service(“beanName”)@Scope(“prototype”)來改變。可以使用以下方式指定初始化方法和銷毀方法(方法名任意): @PostConstruct public void init() { } @PreDestroy public void destory() { }

Spring中@Autowired注解、@Resource注解的區(qū)別

BYETHANON2011年 06 月 02 日INJAVA

Spring不但支持自己定義的@Autowired注解,還支持幾個由JSR-250規(guī)范定義的注解,它們分別是@Resource、@PostConstruct以及@PreDestroy。@Resource的作用相當于@Autowired,只不過@Autowired按byType自動注入,而@Resource默認按 byName自動注入罷了。@Resource有兩個屬性是比較重要的,分是name和type,Spring將@Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。所以如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不指定name也不指定type屬性,這時將通過反射機制使用byName自動注入策略。@Resource裝配順序1. 如果同時指定了name和type,則從Spring上下文中找到唯一匹配的bean進行裝配,找不到則拋出異常2. 如果指定了name,則從上下文中查找名稱(id)匹配的bean進行裝配,找不到則拋出異常3. 如果指定了type,則從上下文中找到類型匹配的唯一bean進行裝配,找不到或者找到多個,都會拋出異常4. 如果既沒有指定name,又沒有指定type,則自動按照byName方式進行裝配;如果沒有匹配,則回退為一個原始類型進行匹配,如果匹配則自動裝配;

@Autowired 與@Resource的區(qū)別:

1、@Autowired與@Resource都可以用來裝配bean.都可以寫在字段上,或?qū)懺趕etter方法上。

2、@Autowired默認按類型裝配(這個注解是屬業(yè)spring的),默認情況下必須要求依賴對象必須存在,如果要允許null值,可以設(shè)置它的required屬性為false,如:@Autowired(required=false),如果我們想使用名稱裝配可以結(jié)合@Qualifier注解進行使用,如下:

1

@Autowired()@Qualifier("baseDao")

2

privateBaseDao baseDao;

3、@Resource(這個注解屬于J2EE的),默認安裝名稱進行裝配,名稱可以通過name屬性進行指定,如果沒有指定name屬性,當注解寫在字段上時,默認取字段名進行安裝名稱查找,如果注解寫在setter方法上默認取屬性名進行裝配。當找不到與名稱匹配的bean時才按照類型進行裝配。但是需要注意的是,如果name屬性一旦指定,就只會按照名稱進行裝配。

1

@Resource(name="baseDao")

2

privateBaseDao baseDao;

推薦使用:@Resource注解在字段上,這樣就不用寫setter方法了,并且這個注解是屬于J2EE的,減少了與spring的耦合。這樣代碼看起就比較優(yōu)雅。

1. 使用Spring2.5的Autowired實現(xiàn)注釋型的IOC

2.

3. 本文地址:http://qzone.QQ.com/blog/55357655-1232078233

4.

5. 使用Spring2.5的新特性——Autowired可以實現(xiàn)快速的自動注入,而無需在xml文檔里面添加bean的聲明,大大減少了xml文檔的維護。(偶喜歡這個功能,因為偶對xml不感冒)。以下是一個例子:

6. 先編寫接口Man:

7. publicinterfaceMan{

8. publicStringsayHello();

9. }

10. 然后寫Man的實現(xiàn)類Chinese和American:

11. @Service

12. publicclassChineseimplementsMan{

13. publicStringsayHello(){

14. return"IamChinese!";

15. }

16. }

17.

18. @Service

19. publicclassAmericanimplementsMan{

20. publicStringsayHello(){

21. return"IamAmerican!";

22. }

23. }

24.

25. @Service注釋表示定義一個bean,自動根據(jù)bean的類名實例化一個首寫字母為小寫的bean,例如Chinese實例化為chinese,American實例化為american,如果需要自己改名字則:@Service("你自己改的bean名")。

26.

27. beans.xml

28. <?xmlversion="1.0"encoding="UTF-8"?>

29. <beansxmlns="http://www.springframework.org/schema/beans"

30. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

31. xmlns:context="http://www.springframework.org/schema/context"

32. xsi:schemaLocation="http://www.springframework.org/schema/beans

33. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

34. http://www.springframework.org/schema/context

35. http://www.springframework.org/schema/context/spring-context-2.5.xsd">

36. <context:annotation-config/>

37. <context:component-scanbase-package="testspring.main"/>

38. </beans>

39. 在spring的配置文件里面只需要加上<context:annotation-config/>和<context:component-scanbase-package="需要實現(xiàn)注入的類所在包"/>,可以使用base-package="*"表示全部的類。

40.

41. 編寫主類測試:

42. @Service

43. publicclassMain{

44. @Autowired

45. @Qualifier("chinese")

46. privateManman;

47.

48. publicstaticvoidmain(String[]args){

49. //TODOcodeapplicationlogichere

50. ApplicationContextctx=newClassPathXmlApplicationContext("beans.xml");

51. Mainmain=(Main)ctx.getBean("main");

52. System.out.println(main.getMan().sayHello());

53. }

54.

55. publicMangetMan(){

56. returnman;

57. }

58. }

59.

60. 在Man接口前面標上@Autowired和@Qualifier注釋使得Man接口可以被容器注入,當Man接口存在兩個實現(xiàn)類的時候必須指定其中一個來注入,使用實現(xiàn)類首字母小寫的字符串來注入。否則可以省略,只寫@Autowired

61.

62. **********************

63. 使用Spring2.5注釋驅(qū)動的IoC功能

64. 發(fā)表于08-03-0420:38|閱讀1285|評分(暫無)

65. 概述

66.

67. 注釋配置相對于XML配置具有很多的優(yōu)勢:

68.

69. 它可以充分利用Java的反射機制獲取類結(jié)構(gòu)信息,這些信息可以有效減少配置的工作。如使用JPA注釋配置ORM映射時,我們就不需要指定PO的屬性名、類型等信息,如果關(guān)系表字段和PO屬性名、類型都一致,您甚至無需編寫任務(wù)屬性映射信息——因為這些信息都可以通過Java反射機制獲取。

70. 注釋和Java代碼位于一個文件中,而XML配置采用獨立的配置文件,大多數(shù)配置信息在程序開發(fā)完成后都不會調(diào)整,如果配置信息和Java代碼放在一起,有助于增強程序的內(nèi)聚性。而采用獨立的XML配置文件,程序員在編寫一個功能時,往往需要在程序文件和配置文件中不停切換,這種思維上的不連貫會降低開發(fā)效率。

71. 因此在很多情況下,注釋配置比XML配置更受歡迎,注釋配置有進一步流行的趨勢。Spring2.5的一大增強就是引入了很多注釋類,現(xiàn)在您已經(jīng)可以使用注釋配置完成大部分XML配置的功能。在這篇文章里,我們將向您講述使用注釋進行Bean定義和依賴注入的內(nèi)容。

72.

73.

74. 原來我們是怎么做的

75.

76. 在使用注釋配置之前,先來回顧一下傳統(tǒng)上是如何配置Bean并完成Bean之間依賴關(guān)系的建立。下面是3個類,它們分別是Office、Car和Boss,這3個類需要在Spring容器中配置為Bean:

77.

78. Office僅有一個屬性:

79.

80.

81. 清單1.Office.java

82.

83. packagecom.baobaotao;

84. publicclassOffice{

85. privateStringofficeNo=”001”;

86.

87. //省略get/setter

88.

89. @Override

90. publicStringtoString(){

91. return"officeNo:"+officeNo;

92. }

93. }

94.

95.

96.

97. Car擁有兩個屬性:

98.

99.

100. 清單2.Car.java

101.

102. packagecom.baobaotao;

103.

104. publicclassCar{

105. privateStringbrand;

106. privatedoubleprice;

107.

108. //省略get/setter

109.

110. @Override

111. publicStringtoString(){

112. return"brand:"+brand+","+"price:"+price;

113. }

114. }

115.

116.

117.

118. Boss擁有Office和Car類型的兩個屬性:

119.

120.

121. 清單3.Boss.java

122.

123. packagecom.baobaotao;

124.

125. publicclassBoss{

126. privateCarcar;

127. privateOfficeoffice;

128.

129. //省略get/setter

130.

131. @Override

132. publicStringtoString(){

133. return"car:"+car+"/n"+"office:"+office;

134. }

135. }

136.

137.

138.

139. 我們在Spring容器中將Office和Car聲明為Bean,并注入到BossBean中:下面是使用傳統(tǒng)XML完成這個工作的配置文件beans.xml:

140.

141.

142. 清單4.beans.xml將以上三個類配置成Bean

143.

144. <?xmlversion="1.0"encoding="UTF-8"?>

145. <beansxmlns="http://www.springframework.org/schema/beans"

146. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

147. xsi:schemaLocation="http://www.springframework.org/schema/beans

148. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

149. <beanid="boss"class="com.baobaotao.Boss">

150. <propertyname="car"ref="car"/>

151. <propertyname="office"ref="office"/>

152. </bean>

153. <beanid="office"class="com.baobaotao.Office">

154. <propertyname="officeNo"value="002"/>

155. </bean>

156. <beanid="car"class="com.baobaotao.Car"scope="singleton">

157. <propertyname="brand"value="紅旗CA72"/>

158. <propertyname="price"value="2000"/>

159. </bean>

160. </beans>

161.

162.

163.

164. 當我們運行以下代碼時,控制臺將正確打出boss的信息:

165.

166.

167. 清單5.測試類:AnnoIoCTest.java

168.

169. importorg.springframework.context.ApplicationContext;

170. importorg.springframework.context.support.ClassPathXmlApplicationContext;

171. publicclassAnnoIoCTest{

172.

173. publicstaticvoidmain(String[]args){

174. String[]locations={"beans.xml"};

175. ApplicationContextctx=

176. newClassPathXmlApplicationContext(locations);

177. Bossboss=(Boss)ctx.getBean("boss");

178. System.out.println(boss);

179. }

180. }

181.

182.

183.

184. 這說明Spring容器已經(jīng)正確完成了Bean創(chuàng)建和裝配的工作。

185.

186.

187. 使用@Autowired注釋

188.

189. Spring2.5引入了@Autowired注釋,它可以對類成員變量、方法及構(gòu)造函數(shù)進行標注,完成自動裝配的工作。來看一下使用@Autowired進行成員變量自動注入的代碼:

190.

191.

192. 清單6.使用@Autowired注釋的Boss.java

193.

194. packagecom.baobaotao;

195. importorg.springframework.beans.factory.annotation.Autowired;

196.

197. publicclassBoss{

198.

199. @Autowired

200. privateCarcar;

201.

202. @Autowired

203. privateOfficeoffice;

204.

205. &hellip;

206. }

207.

208.

209.

210. Spring通過一個BeanPostProcessor對@Autowired進行解析,所以要讓@Autowired起作用必須事先在Spring容器中聲明AutowiredAnnotationBeanPostProcessorBean。

211.

212.

213. 清單7.讓@Autowired注釋工作起來

214.

215. <?xmlversion="1.0"encoding="UTF-8"?>

216. <beansxmlns="http://www.springframework.org/schema/beans"

217. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

218. xsi:schemaLocation="http://www.springframework.org/schema/beans

219. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

220.

221. <!--該BeanPostProcessor將自動起作用,對標注@Autowired的Bean進行自動注入-->

222. <beanclass="org.springframework.beans.factory.annotation.

223. AutowiredAnnotationBeanPostProcessor"/>

224.

225. <!--移除bossBean的屬性注入配置的信息-->

226. <beanid="boss"class="com.baobaotao.Boss"/>

227.

228. <beanid="office"class="com.baobaotao.Office">

229. <propertyname="officeNo"value="001"/>

230. </bean>

231. <beanid="car"class="com.baobaotao.Car"scope="singleton">

232. <propertyname="brand"value="紅旗CA72"/>

233. <propertyname="price"value="2000"/>

234. </bean>

235. </beans>

236.

237.

238.

239. 這樣,當Spring容器啟動時,AutowiredAnnotationBeanPostProcessor將掃描Spring容器中所有Bean,當發(fā)現(xiàn)Bean中擁有@Autowired注釋時就找到和其匹配(默認按類型匹配)的Bean,并注入到對應(yīng)的地方中去。

240.

241. 按照上面的配置,Spring將直接采用Java反射機制對Boss中的car和office這兩個私有成員變量進行自動注入。所以對成員變量使用@Autowired后,您大可將它們的setter方法(setCar()和setOffice())從Boss中刪除。

242.

243. 當然,您也可以通過@Autowired對方法或構(gòu)造函數(shù)進行標注,來看下面的代碼:

244.

245.

246. 清單8.將@Autowired注釋標注在Setter方法上

247.

248. packagecom.baobaotao;

249.

250. publicclassBoss{

251. privateCarcar;

252. privateOfficeoffice;

253.

254. @Autowired

255. publicvoidsetCar(Carcar){

256. this.car=car;

257. }

258.

259. @Autowired

260. publicvoidsetOffice(Officeoffice){

261. this.office=office;

262. }

263. …

264. }

265.

266.

267.

268. 這時,@Autowired將查找被標注的方法的入?yún)㈩愋偷腂ean,并調(diào)用方法自動注入這些Bean。而下面的使用方法則對構(gòu)造函數(shù)進行標注:

269.

270.

271. 清單9.將@Autowired注釋標注在構(gòu)造函數(shù)上

272.

273. packagecom.baobaotao;

274.

275. publicclassBoss{

276. privateCarcar;

277. privateOfficeoffice;

278.

279. @Autowired

280. publicBoss(Carcar,Officeoffice){

281. this.car=car;

282. this.office=office;

283. }

284.

285. …

286. }

287.

288.

289.

290. 由于Boss()構(gòu)造函數(shù)有兩個入?yún)ⅲ謩e是car和office,@Autowired將分別尋找和它們類型匹配的Bean,將它們作為Boss(Carcar,Officeoffice)的入?yún)韯?chuàng)建BossBean。

291.

292.

293. 當候選Bean數(shù)目不為1時的應(yīng)對方法

294.

295. 在默認情況下使用@Autowired注釋進行自動注入時,Spring容器中匹配的候選Bean數(shù)目必須有且僅有一個。當找不到一個匹配的Bean時,Spring容器將拋出BeanCreationException異常,并指出必須至少擁有一個匹配的Bean。我們可以來做一個實驗:

296.

297.

298. 清單10.候選Bean數(shù)目為0時

299.

300. <?xmlversion="1.0"encoding="UTF-8"?>

301. <beansxmlns="http://www.springframework.org/schema/beans"

302. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

303. xsi:schemaLocation="http://www.springframework.org/schema/beans

304. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

305.

306. <beanclass="org.springframework.beans.factory.annotation.

307. AutowiredAnnotationBeanPostProcessor"/>

308.

309. <beanid="boss"class="com.baobaotao.Boss"/>

310.

311. <!--將officeBean注釋掉-->

312. <!--<beanid="office"class="com.baobaotao.Office">

313. <propertyname="officeNo"value="001"/>

314. </bean>-->

315.

316. <beanid="car"class="com.baobaotao.Car"scope="singleton">

317. <propertyname="brand"value="紅旗CA72"/>

318. <propertyname="price"value="2000"/>

319. </bean>

320. </beans>

321.

322.

323.

324. 由于officeBean被注釋掉了,所以Spring容器中將沒有類型為Office的Bean了,而Boss的office屬性標注了@Autowired,當啟動Spring容器時,異常就產(chǎn)生了。

325.

326. 當不能確定Spring容器中一定擁有某個類的Bean時,可以在需要自動注入該類Bean的地方可以使用@Autowired(required=false),這等于告訴Spring:在找不到匹配Bean時也不報錯。來看一下具體的例子:

327.

328.

329. 清單11.使用@Autowired(required=false)

330.

331. packagecom.baobaotao;

332.

333. importorg.springframework.beans.factory.annotation.Autowired;

334. importorg.springframework.beans.factory.annotation.Required;

335.

336. publicclassBoss{

337.

338.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 佛教| 湖南省| 翁源县| 鲁甸县| 林口县| 鲁山县| 惠水县| 乌海市| 武宣县| 石楼县| 滦南县| 建平县| 盈江县| 若尔盖县| 祁阳县| 辉县市| 马边| 高安市| 麟游县| 章丘市| 盐亭县| 彭阳县| 平湖市| 繁昌县| 凯里市| 琼结县| 渝中区| 闻喜县| 山西省| 广灵县| 儋州市| 罗山县| 邢台市| 玉树县| 那坡县| 高碑店市| 申扎县| 台中市| 宣化县| 长汀县| 泽州县|