Spring Boot 註解大全,一鍵收藏!回城路上覆習!


來源 | www.cnblogs.com/tanwei81/p/6814022.html

1、註解 (annotations) 列表

@SpringBootApplication
包含了 @ComponentScan、@Configuration 和 @EnableAutoConfiguration 註解。
其中 @ComponentScan 讓 spring Boot 掃描到 Configuration 類並把它加入到程序上下文。
@Configuration 等同於 spring 的 XML 配置文件;使用 Java 代碼能夠檢查類型安全。
**@EnableAutoConfiguration ** 自動配置。
**@ComponentScan ** 組件掃描,可自動發現和裝配一些 Bean。
@Component 可配合 CommandLineRunner 使用,在程序啓動後執行一些基礎任務。
@RestController 註解是 @Controller 和 @ResponseBody 的合集, 表示這是個控制器 bean, 而且是將函數的返回值直 接填入 HTTP 響應體中, 是 REST 風格的控制器。
@Autowired 自動導入。
@PathVariable 獲取參數。
@JsonBackReference 解決嵌套外鏈問題。
@RepositoryRestResourcepublic 配合 spring-boot-starter-data-rest 使用。

2、註解 (annotations) 詳解

@SpringBootApplication:申明讓 spring boot 自動給程序進行必要的配置,這個配置等同於:@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 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";
    }
}
@RequestMapping:提供路由信息,負責 URL 到 Controller 中的具體函數的映射。
@EnableAutoConfiguration:Spring Boot 自動配置(auto-configuration):嘗試根據你添加的 jar 依賴自動配置你的 Spring 應用。
例如,若是你的 classpath 下存在 HSQLDB,而且你沒有手動配置任何數據庫鏈接 beans,那麼咱們將自動配置一個內存型(in-memory)數據庫」。
你能夠將 @EnableAutoConfiguration 或者 @SpringBootApplication 註解添加到一個 @Configuration 類上來選擇自動配置。
若是發現應用了你不想要的特定自動配置類,你可使用 @EnableAutoConfiguration 註解的排除屬性來禁用它們。
@ComponentScan:表示將該類自動發現掃描組件。
我的理解至關於,若是掃描到有 @Component、@Controller、@Service 等這些註解的類,並註冊爲 Bean,能夠自動收集全部的 Spring 組件,包括 @Configuration 類。
咱們常用 @ComponentScan 註解搜索 beans,並結合 @Autowired 註解導入。能夠自動收集全部的 Spring 組件,包括 @Configuration 類。
若是沒有配置的話,Spring Boot 會掃描啓動類所在包下以及子包下的使用了 @Service,@Repository 等註解的類。
@Configuration:至關於傳統的 xml 配置文件,若是有些第三方庫須要用到 xml 文件,建議仍然經過 @Configuration 類做爲項目的配置主類——可使用 @ImportResource 註解加載 xml 配置文件。
@Import:用來導入其餘配置類。
@ImportResource:用來加載 xml 配置文件。
@Autowired:自動導入依賴的 bean
@Service:通常用於修飾 service 層的組件
@Repository:使用 @Repository 註解能夠確保 DAO 或者 repositories 提供異常轉譯,這個註解修飾的 DAO 或者 repositories 類會被 ComponetScan 發現並配置,同時也不須要爲它們提供 XML 配置項。
@Bean:用 @Bean 標註方法等價於 XML 中配置的 bean。
@Value:注入 Spring boot application.properties 配置的屬性的值。示例代碼:
  
  
   
   
   
   
@Value(value = 「#{message}」)
private String message;
@Inject:等價於默認的 @Autowired,只是沒有 required 屬性;
@Component:泛指組件,當組件很差歸類的時候,咱們可使用這個註解進行標註。
@Bean:至關於 XML 中的, 放在方法的上面,而不是類,意思是產生一個 bean, 並交給 spring 管理。
@AutoWired:自動導入依賴的 bean。byType 方式。把配置好的 Bean 拿來用,完成屬性、方法的組裝,它能夠對類成員變量、方法及構造函數進行標註,完成自動裝配的工做。當加上(required=false)時,就算找不到 bean 也不報錯。
@Qualifier:當有多個同一類型的 Bean 時,能夠用 @Qualifier(「name」) 來指定。與 @Autowired 配合使用。@Qualifier 限定描述符除了能根據名字進行注入,但能進行更細粒度的控制如何選擇候選者,具體使用方式以下:
  
  
   
   
   
   
@Autowired
@Qualifier(value = 「demoInfoService」)
private DemoInfoService demoInfoService;
@Resource(name=」name」,type=」type」):沒有括號內內容的話,默認 byName。與 @Autowired 幹相似的事。

3、JPA 註解

@Entity:@Table(name=」「):代表這是一個實體類。通常用於 jpa 這兩個註解通常一塊使用,可是若是表名和實體類名相同的話,@Table 能夠省略
@MappedSuperClass: 用在肯定是父類的 entity 上。父類的屬性子類能夠繼承。
@NoRepositoryBean: 通常用做父類的 repository,有這個註解,spring 不會去實例化該 repository。
@Column:若是字段名與列名相同,則能夠省略。
@Id:表示該屬性爲主鍵。
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator= 「repair_seq」):表示主鍵生成策略是 sequence(能夠爲 Auto、IDENTITY、native 等,Auto 表示可在多個數據庫間切換),指定 sequence 的名字是 repair_seq。
@SequenceGeneretor(name = 「repair_seq」, sequenceName = 「seq_repair」, allocationSize = 1):name 爲 sequence 的名稱,以便使用,sequenceName 爲數據庫的 sequence 名稱,兩個名稱能夠一致。
@Transient:表示該屬性並不是一個到數據庫表的字段的映射, ORM 框架將忽略該屬性。
若是一個屬性並不是數據庫表的字段映射, 就務必將其標示爲 @Transient, 不然, ORM 框架默認其註解爲 @Basic。@Basic(fetch=FetchType.LAZY):標記能夠指定實體屬性的加載方式
@JsonIgnore:做用是 json 序列化時將 Java bean 中的一些屬性忽略掉, 序列化和反序列化都受影響。
@JoinColumn(name=」loginId」): 一對一:本表中指向另外一個表的外鍵。一對多:另外一個表指向本表的外鍵。
@OneToOne、@OneToMany、@ManyToOne:對應 hibernate 配置文件中的一對一,一對多,多對一。

4、springMVC 相關注解

@RequestMapping:@RequestMapping(「/path」)表示該控制器處理全部 「/path」 的 UR L 請求。
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;
}
參數與大括號裏的名字同樣要相同。

5、全局異常處理

@ControllerAdvice:包含 @Component。能夠被掃描到。統一處理異常。
@ExceptionHandler(Exception.class):用在方法上面表示遇到這個異常就執行如下方法。



本文分享自微信公衆號 - 架構師智庫(beijing-tmt)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。html

相關文章
相關標籤/搜索