天天看點

項目中都用到了哪些注解

1.首先是在Springboot啟動類上,使用@SpringBootApplication注解,

2.編寫測試的時候,用到@RunWith(SpringRunner.class) @SpringBootTest

3.在service層,使用@Service标志是service層并交給spring管理,@Transactional表示使用事務,@Autowired的作用是:

(1)可以對類成員變量,方法及構造函數進行标注,讓spring完成bean的自動裝配,

(2)@Autowired預設是按類型去比對,配合@Qualifer按類的名稱去裝配bean

4.在controller層,

a.使用RestController=Controller+ResponseBody,則Controller中的方法無法傳回jsp頁面,或者html,配置的視圖解析器 InternalResourceViewResolver不起作用,傳回的内容就是Return 裡的内容,

b.使用@ReqeustMapping實作url解析,路由的作用,可以在類上,也可以在方法上使用

c.使用@PostMapping用在方法上,表示處理post的請求,使用@GetMapping來處理get的請求

d.處理前端傳過來的參數時,可以使用@RequestParam,@PathVariable,它們的差別是@RequestParam是處理的是請求參數,比如說這種類型:http://localhost/mdeditor/chen?id=39,

http://localhost/mdeditor/chen?id=39請求參數和請求變量的差別
對于該請求參數的接收我們可以這樣
//将請求參數映射到處理器參數上
@RequestMapping("/mededitor/chen")
public String getId(@RequestParam( value="id",requested=false)String Pid){
return id;
}
//會輸出39
           

而@PathVariable處理的是路徑變量,

//如下,設定chen為路徑變量
http://localhost/mdeditor/chen
//将請求的路徑變量,映射到處理器參數中
@RequestMapping("/mededitor/{page}")
public String getId(@PathVariable( value="page",requested=false)String Page){
return page;
}

//會輸出chen

//同時,在路徑變量名和處理器參數名一緻的時候,我們可以省去PathVariable中的value值,如下
http://localhost/mdeditor/cc
@RequestMapping("/mededitor/{page}")
public String getId(@PathVariable String page){
return page;
}
//輸出cc
           

 5.對于就比如說要讀取配置在yml檔案中的屬性,使用@ConfigrationProperties(prefix="xx"),還是用@Component來将所給的bean注入到spring容器裡面,是作用在類級别,而對于@Bean的話是作用在方法級别,有具體類的建立過程。比如說要使用第三方的一些api的話,就可以用@Bean,再使用@Component将其注入到spring容器裡面。

6.對于對頁面的字段進行驗證的時候,比如說哪些不能為空,@Valid+BingResult配合