一、簡介
基于 SpringBoot 平臺開發(fā)的項(xiàng)目數(shù)不勝數(shù),與常規(guī)的基于Spring
開發(fā)的項(xiàng)目最大的不同之處,SpringBoot 里面提供了大量的注解用于快速開發(fā),而且非常簡單,基本可以做到開箱即用!
那 SpringBoot 為開發(fā)者提供了多少注解呢?我們該如何使用?
針對此問題,小編特意對其進(jìn)行了一番整理,內(nèi)容如下,個(gè)人感覺還是比較清晰的,今天我們就一起來整一整每個(gè)注解的含義和用法,以免踩坑!
二、注解總結(jié)
2.1、SpringMVC 相關(guān)注解
@Controller
通常用于修飾controller
層的組件,由控制器負(fù)責(zé)將用戶發(fā)來的URL
請求轉(zhuǎn)發(fā)到對應(yīng)的服務(wù)接口,通常還需要配合注解@RequestMapping
使用。
@RequestMapping
提供路由信息,負(fù)責(zé)URL
到Controller
中具體函數(shù)的映射,當(dāng)用于方法上時(shí),可以指定請求協(xié)議,比如GET
、POST
、PUT
、DELETE
等等。
@RequestBody
表示請求體的Content-Type
必須為application/json
格式的數(shù)據(jù),接收到數(shù)據(jù)之后會自動將數(shù)據(jù)綁定到Java
對象上去
@ResponseBody
表示該方法的返回結(jié)果直接寫入HTTP response body
中,返回?cái)?shù)據(jù)的格式為application/json
比如,請求參數(shù)為json
格式,返回參數(shù)也為json
格式,示例代碼如下:
/**
* 登錄服務(wù)
*/
@Controller
@RequestMapping("api")
public class LoginController {
/**
* 登錄請求,post請求協(xié)議,請求參數(shù)數(shù)據(jù)格式為json
* @param request
*/
@RequestMapping(value = "login", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity login(@RequestBody UserLoginDTO request){
//...業(yè)務(wù)處理
return new ResponseEntity(HttpStatus.OK);
}
}
@RestController
和@Controller
一樣,用于標(biāo)注控制層組件,不同的地方在于:它是@ResponseBody
和@Controller
的合集,也就是說,在當(dāng)@RestController
用在類上時(shí),表示當(dāng)前類里面所有對外暴露的接口方法,返回?cái)?shù)據(jù)的格式都為application/json
,示范代碼如下:
@RestController
@RequestMapping("api")
public class LoginController {
/**
* 登錄請求,post請求協(xié)議,請求參數(shù)數(shù)據(jù)格式為json
* @param request
*/
@RequestMapping(value = "login", method = RequestMethod.POST)
public ResponseEntity login(@RequestBody UserLoginDTO request){
//...業(yè)務(wù)處理
return new ResponseEntity(HttpStatus.OK);
}
}
@RequestParam
用于接收請求參數(shù)為表單類型的數(shù)據(jù),通常用在方法的參數(shù)前面,示范代碼如下:
/**
* 登錄請求,post請求協(xié)議,請求參數(shù)數(shù)據(jù)格式為表單
*/
@RequestMapping(value = "login", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity login(@RequestParam(value = "userName",required = true) String userName,
@RequestParam(value = "userPwd",required = true) String userPwd){
//...業(yè)務(wù)處理
return new ResponseEntity(HttpStatus.OK);
}
@PathVariable
用于獲取請求路徑中的參數(shù),通常用于restful
風(fēng)格的api
上,示范代碼如下:
/**
* restful風(fēng)格的參數(shù)請求
* @param id
*/
@RequestMapping(value = "queryProduct/{id}", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity queryProduct(@PathVariable("id") String id){
//...業(yè)務(wù)處理
return new ResponseEntity(HttpStatus.OK);
}
@GetMapping
除了@RequestMapping
可以指定請求方式之外,還有一些其他的注解,可以用于標(biāo)注接口路徑請求,比如GetMapping
用在方法上時(shí),表示只支持get
請求方法,等價(jià)于@RequestMapping(value="/get",method=RequestMethod.GET)
@GetMapping("get")
public ResponseEntity get(){
return new ResponseEntity(HttpStatus.OK);
}
@PostMapping
用在方法上,表示只支持post
方式的請求。
@PostMapping("post")
public ResponseEntity post(){
return new ResponseEntity(HttpStatus.OK);
}
@PutMapping
用在方法上,表示只支持put
方式的請求,通常表示更新某些資源的意思
@PutMapping("put")
public ResponseEntity put(){
return new ResponseEntity(HttpStatus.OK);
}
@DeleteMapping
用在方法上,表示只支持delete
方式的請求,通常表示刪除某些資源的意思
@DeleteMapping("delete")
public ResponseEntity delete(){
return new ResponseEntity(HttpStatus.OK);
}
2.2、bean 相關(guān)注解
@Service
通常用于修飾service
層的組件,聲明一個(gè)對象,會將類對象實(shí)例化并注入到bean
容器里面
@Service
public class DeptService {
//具體的方法
}
@Component
泛指組件,當(dāng)組件不好歸類的時(shí)候,可以使用這個(gè)注解進(jìn)行標(biāo)注,功能類似于于@Service
@Component
public class DeptService {
//具體的方法
}
@Repository
通常用于修飾dao
層的組件,
@Repository
注解屬于Spring
里面最先引入的一批注解,它用于將數(shù)據(jù)訪問層 (DAO
層 ) 的類標(biāo)識為Spring Bean
,具體只需將該注解標(biāo)注在 DAO類上即可,示例代碼如下:
@Repository
public interface RoleRepository extends JpaRepository<Role,Long> {
//具體的方法
}
為什么現(xiàn)在使用的很少呢?
主要是因?yàn)楫?dāng)我們配置服務(wù)啟動自動掃描dao
層包時(shí),Spring
會自動幫我們創(chuàng)建一個(gè)實(shí)現(xiàn)類,然后注入到bean
容器里面。當(dāng)某些類無法被掃描到時(shí),我們可以顯式的在數(shù)據(jù)持久類上標(biāo)注@Repository
注解,Spring
會自動幫我們聲明對象。
@Bean
相當(dāng)于 xml 中配置 Bean,意思是產(chǎn)生一個(gè) bean 對象,并交給spring管理,示例代碼如下:
@Configuration
public class AppConfig {
//相當(dāng)于 xml 中配置 Bean
@Bean
public Uploader initFileUploader() {
return new FileUploader();
}
}
@Autowired
自動導(dǎo)入依賴的bean
對象,默認(rèn)時(shí)按照byType
方式導(dǎo)入對象,而且導(dǎo)入的對象必須存在,當(dāng)需要導(dǎo)入的對象并不存在時(shí),我們可以通過配置required = false
來關(guān)閉強(qiáng)制驗(yàn)證。
@Autowired
private DeptService deptService;
@Resource
也是自動導(dǎo)入依賴的bean
對象, 由JDK
提供 ,默認(rèn)是按照byName
方式導(dǎo)入依賴的對象;而@Autowired
默認(rèn)時(shí)按照byType
方式導(dǎo)入對象,當(dāng)然@Resource
還可以配置成通過byType
方式導(dǎo)入對象。
/**
* 通過名稱導(dǎo)入(默認(rèn)通過名稱導(dǎo)入依賴對象)
*/
@Resource(name = "deptService")
private DeptService deptService;
/**
* 通過類型導(dǎo)入
*/
@Resource(type = RoleRepository.class)
private DeptService deptService;
@Qualifier
當(dāng)有多個(gè)同一類型的bean
時(shí),使用@Autowired
導(dǎo)入會報(bào)錯(cuò),提示當(dāng)前對象并不是唯一,Spring
不知道導(dǎo)入哪個(gè)依賴,這個(gè)時(shí)候,我們可以使用@Qualifier
進(jìn)行更細(xì)粒度的控制,選擇其中一個(gè)候選者,一般于@Autowired
搭配使用,示例如下:
@Autowired
@Qualifier("deptService")
private DeptService deptService;
@Scope
用于生命一個(gè)spring bean
的作用域,作用的范圍一共有以下幾種:
- singleton:唯一 bean 實(shí)例,Spring 中的 bean 默認(rèn)都是單例的。
- prototype:每次請求都會創(chuàng)建一個(gè)新的 bean 實(shí)例,對象多例。
- request:每一次 HTTP 請求都會產(chǎn)生一個(gè)新的 bean,該 bean 僅在當(dāng)前 HTTP request 內(nèi)有效。
- session:每一次 HTTP 請求都會產(chǎn)生一個(gè)新的 bean,該 bean 僅在當(dāng)前 HTTP session 內(nèi)有效。
/**
* 單例對象
*/
@RestController
@Scope("singleton")
public class HelloController {
}
-
spring
+關(guān)注
關(guān)注
0文章
340瀏覽量
14787 -
MVC
+關(guān)注
關(guān)注
0文章
73瀏覽量
14019 -
開發(fā)者
+關(guān)注
關(guān)注
1文章
611瀏覽量
17311 -
SpringBoot
+關(guān)注
關(guān)注
0文章
175瀏覽量
272
發(fā)布評論請先 登錄
相關(guān)推薦
SpringBoot應(yīng)用啟動運(yùn)行run方法
HarmonyOS注解的使用方法分享
常用手機(jī)焊接工具使用方法
Spring Boot常用注解與使用方式
Java注解及其底層原理解析 1

SpringBoot常用注解及使用方法2
Springboot常用注解合集

SpringBoot常用注解及原理
SpringBoot的核心注解1

SpringBoot的核心注解2

評論