1. 程式人生 > >spring boot 常用註解解析

spring boot 常用註解解析

(1)@SpringBootApplication

       申明讓spring boot自動給程式進行必要的配置,這個配置等同於:

@Configuration @EnableAutoConfiguration  @ComponentScan 三個配置。

示例程式碼:

package com.kfit;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

publicclass App {

    publicstaticvoid main(String[] args) {

       SpringApplication.run(ApiCoreApp.classargs);

    }

}

(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;

@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配置檔案。

例如:@ImportResource({"classpath:application1.xml", "classpath:application2.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屬性

(18)@ConfigurationProperties

通過@ConfigurationProperties註解可以將priperties檔案中的屬性值與Bean中的屬性自動關聯,實現了屬性值自動注入的功能,如在src/main/resources 下新建application.properties檔案,內容為

user.username=feinik
user.userpass=123456

測試類

@Component
//通過prefix屬性指定properties檔案內容字首
//也可以通過locations屬性指定properties檔案路徑
@ConfigurationProperties(prefix = "user")
public classUserConfig{
   //屬性名需與properties中的鍵值對應
   private String username;
   private String userpass;

   public String getUsername(){
      return username;
   }

   publicvoidsetUsername(String username){
      this.username = username;
   }

   public String getUserpass(){
      return userpass;
   }

   publicvoidsetUserpass(String userpass){
      this.userpass = userpass;
   }
}

(19) @EnableConfigurationProperties

@EnableConfigurationProperties 註解是對 @ConfigurationProperties 的內嵌支援,預設會將指定的class註冊為一個Bean,如:

@ConfigurationProperties註解的類

@ConfigurationProperties(prefix = "hello")
public classMyAutoProperties{
   private static final String MSG = "word";
   private String msg = MSG;

   public String getMsg(){
      return msg;
   }

   publicvoidsetMsg(String msg){
      this.msg = msg;
   }
}

在另外一個類中可以通過 @EnableConfigurationProperties(MyAutoProperties.class) 來將 MyAutoProperties註冊為一個Bean

(20)條件註解(條件滿足的情況下才會去解析條件註解註解的類)

@ConditionalOnBean 表示容器裡存在指定的Bean的條件時

@ConditionalOnClass 表示當類路徑下存在指定的類時

@ConditionalOnJava 基於JVM版本作為判斷條件

@ConditionalOnMissingBean 當容器中不存在指定的Bean時

@ConditionalOnMissingClass 當類路徑下不存在指定的Class時

@ConditionalOnNotWebApplication 當前專案不是web專案的情況下

@ConditionalOnProperty 指定的屬性是否有指定的值

@ConditionalOnWebApplication 當前專案是web專案時