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

首頁 > 學院 > 開發設計 > 正文

關于Spring注解 @Service @Component @Controller @Repository 用法

2019-11-15 00:42:23
字體:
來源:轉載
供稿:網友
關于SPRing注解 @Service @Component @Controller @Repository 用法

@Component 相當于實例化類的對象,其他三個注解可以理解為@Component的子注解或細化。

在annotaion配置注解中用@Component來表示一個通用注釋用于說明一個類是一個spring容器管理的類,此類將有spring掃描并加入容器參與ioc。即就是該類已經拉入到spring的管理中了。

通過在classpath中通過自動掃描方式把組建納入spring容器管理。

要使用自動掃描機制我們需要打開一下配置信息:

Bean.xml代碼

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="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
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  9. <!--<context:annotation-config />-->
  10. <context:component-scanbase-package="com.zchen"/>
  11. <!-- 包含annotation-config。spring可以自動去掃描base-pack下面或者子包下面的java文件,如果掃描到有@Component @Controller@Service等這些注解的類,則把這些類注冊為bean-->
  12. </beans>

注:前面講要使用注解需要配置:<context:annotation-config />但如果使用了@Component就不需要加它了,因為:<context:component-scan base-package="com.zchen">里面默認了<context:annotation-config />。

而@Controller, @Service, @Repository是@Component的細化,這三個注解比@Component帶有更多的語義,它們分別對應了控制層、服務層、持久層的類。

@Component泛指組件,當組件不好歸類的時候我們可以使用這個注解進行標注,(現在可以都用此注解,可以只使用單一組件)

Java代碼

@Repository標簽是用來給持久層的類定義一個名字,讓Spring根據這個名字關聯到這個類。

例如:

@Repository(value="userDao")-----也可以直接寫@Repository("userDao"),如果沒有命名,default name 是class name: UserDaoImplpublic class UserDaoImpl implements UserDao{ ........................................}

保證這個類在bean.xml中<context:component-scan base-pakage="com.czhen">com.czhen pakage dir下。

注解Repository后可以直接使用@autowire 直接注入 managerImpl中,如:

@Autowire

private UserDaoImpl userDaoImpl;

一般來說@Controller(控制層) 是action入口,調用@Service(業務層) ,Service再調用@Repository (持久層)完成數據持久化。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 合阳县| 武穴市| 仙居县| 巢湖市| 天长市| 定陶县| 桃源县| 洪雅县| 旌德县| 雷山县| 辉南县| 岳池县| 汾阳市| 额济纳旗| 明溪县| 上栗县| 米脂县| 焉耆| 武安市| 大同市| 怀宁县| 丹东市| 永善县| 德化县| 巩义市| 和平县| 墨竹工卡县| 丹凤县| 当雄县| 滦南县| 冕宁县| 碌曲县| 凤冈县| 全州县| 滨州市| 公主岭市| 时尚| 南宫市| 怀仁县| 白河县| 石柱|