(1)@SpringBootApplication
申明讓spring boot自動給程序進行必要的配置,這個配置等同於:
@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三個配置。
這個具體可以查看博客:
(32)Spring Boot使用@SpringBootApplication注解,從零開始學Spring Boot
示例代碼:
package com.kfit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
* @author
* @version v.0.1
* @date
*/
@SpringBootApplication
publicclass App {
publicstaticvoid main(String[] args) {
SpringApplication.run(ApiCoreApp.class, args);
}
}
(2)@ResponseBody
該注解修飾的函數,會將結果直接填充到HTTP的響應體中,一般用於構建RESTful的api,該注解一般會配合@RequestMapping一起使用。
示例代碼:
@RequestMapping("/test")
@ResponseBody
public String test(){
return"ok";
}
(3)@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";
}
}
(4)@RestController
@ResponseBody和@Controller的合集
示例代碼:
package com.kfit.demo.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @author Angel(QQ交流群:193341332,QQ:412887952)
* @version v.0.1
* @date 2016年7月29日下午7:26:04
*/
@RestController
@RequestMapping("/demoInfo2")
publicclass DemoController2 {
@RequestMapping("/test")
public String test(){
return"ok";
}
}
(5)@RequestMapping
提供路由信息,負責URL到Controller中的具體函數的映射。
(6)@EnableAutoConfiguration
Spring Boot自動配置(auto-configuration):嘗試根據你添加的jar依賴自動配置你的Spring應用。例如,如果你的classpath下存在HSQLDB,並且你沒有手動配置任何數據庫連接beans,那么我們將自動配置一個內存型(in-memory)數據庫”。你可以將@EnableAutoConfiguration或者@SpringBootApplication注解添加到一個@Configuration類上來選擇自動配置。如果發現應用了你不想要的特定自動配置類,你可以使用@EnableAutoConfiguration注解的排除屬性來禁用它們。
(7)@ComponentScan
表示將該類自動發現(掃描)並注冊為Bean,可以自動收集所有的Spring組件,包括@Configuration類。我們經常使用@ComponentScan注解搜索beans,並結合@Autowired注解導入。如果沒有配置的話,Spring Boot會掃描啟動類所在包下以及子包下的使用了@Service,@Repository等注解的類。
(8)@Configuration
相當於傳統的xml配置文件,如果有些第三方庫需要用到xml文件,建議仍然通過@Configuration類作為項目的配置主類——可以使用@ImportResource注解加載xml配置文件。
(9)@Import
用來導入其他配置類。
(10)@ImportResource
用來加載xml配置文件。
(11)@Autowired
自動導入依賴的bean
(12)@Service
一般用於修飾service層的組件
(13)@Repository
使用@Repository注解可以確保DAO或者repositories提供異常轉譯,這個注解修飾的DAO或者repositories類會被ComponetScan發現並配置,同時也不需要為它們提供XML配置項。
(14)@Bean
用@Bean標注方法等價於XML中配置的bean。
(15)@Value
注入Spring boot application.properties配置的屬性的值。
示例代碼: @Value(value = "#{message}")
private String message;
(16)@Qualifier
@Qualifier限定描述符除了能根據名字進行注入,但能進行更細粒度的控制如何選擇候選者,具體使用方式如下:
@Autowired
@Qualifier(value = "demoInfoService")
private DemoInfoService demoInfoService;
(17)@Inject
等價於默認的@Autowired,只是沒有required屬性;
