Spring Boot 常用註解彙總

2021-09-28 17:37:17 字數 4110 閱讀 2026

煙雨紅塵**網 wap.zuxs.net

@target(elementtype.type)

@retention(retentionpolicy.runtime)

@documented

@inherited

@springbootconfiguration

@enableautoconfiguration

@componentscan(excludefilters = )

// ... 此處省略原始碼

}

@springbootconfiguration繼承自@configuration,二者功能也一致,標註當前類是配置類, 並會將當前類內宣告的乙個或多個以@bean註解標記的方法的例項納入到spring容器中,並且例項名就是方法名。

@enableautoconfiguration可以幫助springboot應用將所有符合條件的@configuration配置都載入到當前springboot建立並使用的ioc容器。借助於spring框架原有的乙個工具類:springfactoriesloader的支援,@enableautoconfiguration可以智慧型的自動配置功效才得以大功告成

@componentscan的功能其實就是自動掃瞄並載入符合條件的元件或bean定義,最終將這些bean定義載入到容器中。我們可以通過basepackages等屬性指定@componentscan自動掃瞄的範圍,如果不指定,則預設spring框架實現從宣告@componentscan所在類的package進行掃瞄,預設情況下是不指定的,所以springboot的啟動類最好放在root package下。

控制器,處理http請求。

檢視@restcontroller原始碼

@target(elementtype.type)

@retention(retentionpolicy.runtime)

@documented

@controller

@responsebody

public @inte***ce restcontroller

從原始碼我們知道,@restcontroller註解相當於@responsebody+@controller合在一起的作用,restcontroller使用的效果是將方法返回的物件直接在瀏覽器上展示成json格式.

通過httpmessageconverter讀取request body並反序列化為object(泛指)物件

@target(elementtype.method)

@retention(retentionpolicy.runtime)

@documented

//...

}

@target(elementtype.method)

@retention(retentionpolicy.runtime)

@documented

//...

}

@controller

public class helloworldcontroller

}

請求示例:http://localhost:8080/user/getuser/123

public string getuser(@requestparam("uid")integer id, model model)

請求示例:http://localhost:8080/user/getuser?uid=123

dao層註解,dao層中介面繼承jparepository,需要在build.gradle中引入相關jpa的乙個jar自動載入。

repository註解原始碼

@target()

@retention(retentionpolicy.runtime)

@documented

@component

public @inte***ce repository

@target()

@retention(retentionpolicy.runtime)

@documented

@component

public @inte***ce service

@scope作用在類上和方法上,用來配置 spring bean 的作用域,它標識 bean 的作用域

@scope原始碼

@target()

@retention(retentionpolicy.runtime)

@documented

public @inte***ce scope .

* @see #scopename

*/@aliasfor("scopename")

string value() default "";

@aliasfor("value")

string scopename() default "";

scopedproxymode proxymode() default scopedproxymode.default;

}

屬性介紹

value

singleton 表示該bean是單例的。(預設)

prototype 表示該bean是多例的,即每次使用該bean時都會新建乙個物件。

request 在一次http請求中,乙個bean對應乙個例項。

session 在乙個httpsession中,乙個bean對應乙個例項。

proxymode

default 不使用**。(預設)

no 不使用**,等價於default。

inte***ces 使用基於介面的**(jdk dynamic proxy)。

target_class 使用基於類的**(cglib)。

@table(name ="資料庫表名"),這個註解也注釋在實體類上,對應資料庫中相應的表。

@id、@column註解用於標註實體類中的字段,pk欄位標註為@id,其餘@column。

@bean明確地指示了一種方法,產生乙個bean的方法,並且交給spring容器管理。支援別名@bean("xx-name")

把普通pojo例項化到spring容器中,相當於配置檔案中的

雖然有了@autowired,但是我們還是要寫一堆bean的配置檔案,相當麻煩,而@component就是告訴spring,我是pojo類,把我註冊到容器中吧,spring會自動提取相關資訊。那麼我們就不用寫麻煩的xml配置檔案了

引入單個properties檔案:

@propertysource(value = )

引入多個properties檔案:

@propertysource(value = )

可以額外分為兩種模式 相對路徑classpath,絕對路徑(真實路徑)file

注意:單檔案可以不寫value或locations,value和locations都可用

相對路徑(classpath)

絕對路徑(file)

取值:使用@value註解取配置檔案中的值

@value("$")

private string ***;

功能類似xml配置的,用來匯入配置類,可以匯入帶有@configuration註解的配置類或實現了importselector/importbeandefinitionregistrar。

使用示例

@import()

public static void main(string args)

}在spring中,事務有兩種實現方式,分別是程式設計式事務管理和宣告式事務管理兩種方式

@controlleradvice 註解定義全域性異常處理類

@controlleradvice

public class globalexceptionhandler

@controlleradvice

public class globalexceptionhandler

}

Springboot常用註解

1.requestbody可以將請求體中的json字串繫結到相應的bean上 1 之前 ajax success function data public void login requestbody string username,requestbody string pwd 2 使用reques...

springboot常用註解

申明讓spring boot自動給程式進行必要的配置,這個配置等同於 configuration enableautoconfiguration 和 componentscan 三個配置。configuration 相當於傳統的xml配置檔案,如果有些第三方庫需要用到xml檔案,建議仍然通過 con...

springboot常用註解

controller層 controller 用來響應頁面,表示當前的類為控制器。restcontroller 是 responsebody和 controller的結合表明當前類是控制器且返回的是一組資料,不是頁面。autowired 這個註解的作用是將其他的類,介面引入,類似於之前的類的初始化等...