一、註解(annotations)列表
@SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration註解
其中@ComponentScan讓spring Boot掃描到Configuration類並把它加入到程序上下文。
@ComponentScan 組件掃描,可自動發現和裝配一些Bean。
@Configuration 等同於spring的XML配置文件;使用Java代碼可以檢查類型安全。
@EnableAutoConfiguration 自動配置。
@Component可配合CommandLineRunner使用,在程序啓動後執行一些基礎任務。
@RestController註解是@Controller和@ResponseBody的合集
表示這是個控制器bean,並且是將函數的返回值直接填入HTTP響應體中,是REST風格的控制器。
@Autowired自動導入。
@PathVariable獲取參數。
@JsonBackReference解決嵌套外鏈問題。
@RepositoryRestResourcepublic配合spring-boot-starter-data-rest使用。
二、註解(annotations)詳解
@SpringBootApplication:讓springboot自動給程序進行必要的配置
等同於:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三個配置。
package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@ResponseBody:表示該方法的返回結果直接寫入HTTP response body中,一般在異步獲取數據時使用,用於構建RESTful的api。
在使用@RequestMapping後,返回值通常解析爲跳轉路徑,加上@responsebody後返回結果不會被解析爲跳轉路徑,而是直接寫入HTTP response body中。
比如異步獲取json數據,加上@responsebody後,會直接返回json數據。該註解一般會配合@RequestMapping一起使用。示例代碼:
@RequestMapping(“/test”)
@ResponseBody
public String test(){
return”ok”;
}
@Controller:用於定義控制器類,在spring 項目中由控制器負責將用戶發來的URL請求轉發到對應的服務接口(service層),一般這個註解在類中,通常方法需要配合註解@RequestMapping。示例代碼:
@Controller
@RequestMapping(“/demoInfo”)
publicclass DemoController {
@Autowired
private DemoInfoService demoInfoService;
@RequestMapping("/hello")
public String hello(Map<String,Object> map){
System.out.println("DemoController.hello()");
map.put("hello","from TemplateController.helloHtml");
//會使用hello.html或者hello.ftl模板進行渲染顯示.
return"/hello";
}
}
@RestController:用於標註控制層組件(如struts中的action),@ResponseBody和@Controller的合集。
package com.kfit.demo.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(“/demoInfo2”)
publicclass DemoController2 {
@RequestMapping("/test")
public String test(){
return"ok";
}
}
@EnableAutoConfiguration:Spring Boot自動配置(auto-configuration)
@ComponentScan:表示將該類自動發現掃描組件。
@Configuration:相當於傳統的xml配置文件
@ImportResource:加載xml配置文件。
@Import:用來導入其他配置類。
@Autowired:自動導入依賴的bean。當加上(required=false)時,就算找不到bean也不報錯。
@Service:一般用於修飾service層的組件
@Repository:使用@Repository註解可以確保DAO或者repositories提供異常轉譯
@Bean:相當於XML中的,放在方法的上面,而不是類,意思是產生一個bean,並交給spring管理。
@Value:注入Spring boot application.properties配置的屬性的值。
@Value(value = “#{message}”)
private String message;
@Inject:等價於默認的@Autowired,只是沒有required屬性;
@Component:泛指組件,當組件不好歸類的時候,我們可以使用這個註解進行標註。
@Qualifier:有多個同一類型的Bean時,用@Qualifier(“name”)來指定。與@Autowired配合使用。
@Qualifier限定描述符除了能根據名字進行注入,但能進行更細粒度的控制如何選擇候選者,具體使用方式如下:
@Autowired
@Qualifier(value = “demoInfoService”)
private DemoInfoService demoInfoService;
@Resource(name=”name”,type=”type”):沒有括號內內容的話,默認byName。與@Autowired幹類似的事。
三、springMVC相關注解
@RequestMapping:@RequestMapping(“/path”)表示該控制器處理所有“/path”的URL請求
RequestMapping是一個用來處理請求地址映射的註解,可用於類或方法上。
用於類上,表示類中的所有響應請求的方法都是以該地址作爲父路徑。
用於方法上,表示子路徑。
該註解有六個屬性:
params:指定request中必須包含某些參數值是,才讓該方法處理。
headers:指定request中必須包含某些指定的header值,才能讓該方法處理請求。
value:指定請求的實際地址,指定的地址可以是URI Template 模式
method:指定請求的method類型, GET、POST、PUT、DELETE等
consumes:指定處理請求的提交內容類型(Content-Type),如application/json,text/html;
produces:指定返回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才返回
@RequestParam:用在方法的參數前面。
@RequestParam
String a =request.getParameter(“a”)。
@PathVariable:路徑變量。
RequestMapping(“user/get/mac/{macAddress}”)
public String getByMacAddress(@PathVariable String macAddress){
//do something;
}
四、全局異常處理
@ControllerAdvice:包含@Component。可以被掃描到。統一處理異常。
@ExceptionHandler(Exception.class):用在方法上面表示遇到這個異常就執行以下方法。