SpringBoot中常用的註解
SpringBoot用於簡化Spring應用的搭建,開發及部署;該框架採用註解的方式進行配置可以很方便的構建Spring應用。
1. @SpringBootApplication
@SpringBootApplication 註解等價於以預設屬性使用 @Configuration,@EnableAutoConfiguration 和 @ComponentScan,通常作為主類的註解;
2. @Configuration
指出該類是bean配置的資訊源,相當於xml中的<beans></beans>標籤,通常用於主類上。
3. @EnableAutoConfiguration
讓 Spring Boot 根據應用所宣告的依賴來對 Spring 框架進行自動配置,一般加在主類上。
4. @ComponentScan
我們經常使用 @ComponentScan 註解搜尋beans,並結合 @Autowired 構造器注入,你的所有應用程式元件( @Component , @Service , @Repository , @Controller 等)將被自動註冊為Spring Beans。
5. @Repository
被這個註解所修飾的DAO或repositories類會被ComponentScan發現並配置。
6. @Service
用於修飾Service層元件。
7. @Controller
用於定義控制器類,由控制器將使用者發來的URL請求傳送至對應的service進行處理。
8. @ResponseBody
作用於方法上,表示該方法的返回結果直接寫入HTTP response body中,一般在非同步獲取資料時使用,在使用@RequestMapping後,返回值通常解析為跳轉路徑,加上@responsebody後返回結果不會被解析為跳轉路徑,而是直接寫入HTTP response body中。比如非同步獲取json資料,加上@responsebody後,會直接返回json資料。
9. @RestController
@Controller與@ResponseBody的合集。
10. @RequestMapping
@RequestMapping是一個用來處理請求地址對映的註解,用於類或方法上,用於類上,表示類中所有響應請求的方法都是以該路徑為父路徑;
自帶屬性:
path:請求的地址;
method:請求的方法;
headers:指定request中必須包含某些指定的header值,才能讓該方法處理請求;
params:指定request中必須包含某些引數值是,才讓該方法處理;
consumes:指定處理請求的提交內容型別(Content-Type),如application/json,text/html;
produces:指定返回的內容型別,僅當request請求頭中的(Accept)型別中包含該指定型別才返回;
11. @Autowired
自動裝配,自動匯入依賴的beans;
12. @RequestParam
用在方法的引數前面;@RequestParam String a =request.getParameter("a")。
13. @PathVariable
作為路徑中的變數,
@RequestMapping(path = {"/profile/{groupId}/{userId}"})
@ResponseBody
public String profile(@PathVariable("userId") int userId,
@PathVariable("groupId") String groupId,
@RequestParam(value = "type", defaultValue = "1") int type,
@RequestParam(value = "key", required = false) String key)
輸入不同的路徑值,方法接收的引數也不同;參考博文:http://blog.csdn.net/sbuiwqvb123/article/details/52946039;http://www.tuicool.com/articles/bQnMra;