Spring 註解的作用

2021-08-11 18:31:04 字數 1667 閱讀 7663

1、@controller 控制器(注入服務)

2、@service 服務(注入dao)

3、@repository dao(實現dao訪問)

4、@component (把普通pojo例項化到spring容器中,相當於配置檔案中的)

@component,@service,@controller,@repository註解的類,並把這些類納入進spring容器中管理。 

下面寫這個是引入component的掃瞄元件 

其中base-package為需要掃瞄的包(含所有子包) 

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

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

3、@repository用於標註資料訪問元件,即dao元件. 

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

@service public class userserviceimpl implements userservice  

@repository public class userdaoimpl implements userdao getbean的預設名稱是類名(頭字母小寫),如果想自定義,可以@service(「***」)               這樣來指定,這種bean預設是單例的,如果想改變,可以使用@service(「beanname」) 

@scope(「prototype」)來改變。可以使用以下方式指定初始化方法和銷毀方法(方法名任意): @postconstruct public void init()

1、@controller 控制器(注入服務)

2、@service 服務(注入dao)

3、@repository dao(實現dao訪問)

4、@component (把普通pojo例項化到spring容器中,相當於配置檔案中的)

@component,@service,@controller,@repository註解的類,並把這些類納入進spring容器中管理。 

下面寫這個是引入component的掃瞄元件 

其中base-package為需要掃瞄的包(含所有子包) 

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

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

3、@repository用於標註資料訪問元件,即dao元件. 

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

@service public class userserviceimpl implements userservice  

@repository public class userdaoimpl implements userdao getbean的預設名稱是類名(頭字母小寫),如果想自定義,可以@service(「***」)               這樣來指定,這種bean預設是單例的,如果想改變,可以使用@service(「beanname」) 

@scope(「prototype」)來改變。可以使用以下方式指定初始化方法和銷毀方法(方法名任意): @postconstruct public void init()

Spring中常用的幾個註解及其作用

剛開始學spring,覺得一些用到過的註解需要統一的記錄一下,不斷更新哦 元件註解 component 新增元件 service 新增元件 controller 新增元件 repository 新增元件 componentscan 包掃瞄 bean 新增一些未加註解的類的元件 import 要匯入到...

Spring註解 Import註解

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

Spring一些常用註解及其作用

controller 處理http請求的控制器 例子 controller public class hellocontroller spring4之後新加入的註解,原來返回json需要 responsebody和 controller配合。即 restcontroller是 responsebod...