【SpringBoot 2學習筆記】《十四》SpringBoot2 大話註解

SpringBoot2核心註解說明

現在企業的項目開發,Java應用開發領域目前最火的框架非SpringBoot2莫屬,它可以方便地創建生產級的Spring應用程序,還能通過註解配置的方式與微服務框架SpringCloud集成。其最核心在於SpringBoot2極大地簡化了項目的配置,盡其所能地實現了約定大於配置的原則。

然而要想充分利用SpringBoot2就需要對其提供的各類註解有清晰地認識和深刻的理解。所以本章主要歸納整理一些常用註解。

10.1 @SpringBootApplication

SpringBoot2核心註解,該註解使用在SpringBoot2主類上,標識這是一個SpringBoot2應用啓動類入口。該註解是一個複合註解,由 @SpringBootConfiguration@EnableAutoConfiguration@ComponentScan三個註解的組合,即可以用這三個註解來代替@SpringBootApplication註解。

註解源碼:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
    
}

10.2 @EnableAutoConfiguration

表示開啓自動配置功能,開啓該註解之後,SpringBoot2自動配置註解,根據當前類路徑下的包或者類來配置 Spring Bean。通過觀察@EnableAutoConfiguration源碼,發現它也是一個組合註解,包含以下兩個註解:@AutoConfigurationPackage@Import({AutoConfigurationImportSelector.class})

註解源碼:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    
}

Springboot2會根據我們在pom.xml文件中添加的jar包來配置項目的默認配置。例如:通過spring-boot-starter-web來判斷項目中是否需要添加了tomcat,並自動的幫我們配置web項目中所需要的默認配置。

10.3 @Configuration

Spring 3.0以後添加的一個註解,可以使用Java代碼的形式代替XML配置文件(applicationContext.xml)的效果,所有這個配置文件裏面能做到的事情都可以通過這個註解所在類來進行註冊。使用該註解的類內部包含有一個或多個被@Bean註解的方法,這些方法將會被AnnotationConfigApplicationContextAnnotationConfigWebApplicationContext類進行掃描,並用於構建bean定義,初始化Spring容器。

註解源碼:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    
}

10.4 @SpringBootConfiguration

該註解是@Configuration註解的變體,僅僅用來表示是SpringBoot2配置而已。該註解繼承自@Configuration,二者功能一致。用來標註當前類是配置類,並會將當前類內聲明的一個或多個以@Bean註解標記的方法的實例納入到Srping容器中,並且實例名就是方法名。

註解源碼:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
    
}

10.5 @ComponentScan

@ComponentScan註解可以自定義Spring掃描的包,默認會掃描標包路徑下面標註了@Controller@Service@Component以及@Repository註解的類,並將這些組件實例化後加載到SpringIOC容器中,它有個配置屬性:basePackages,也就是指定掃描的包。如果不設置,它會默認掃描配置了該註解的類的包所在的路徑(包括子包)。

註解源碼:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
    
}

10.6 @Conditional

Spring4.0添加的新註解。@Conditional類型的註解,可以用在@Configuration註解的類上,或者@Bean註解方法中使用。可以允許基於Spring Environment屬性包含配置,可以僅允許在存在特定資源時包含配置。

註解源碼:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {
    
}

10.6.1 @ConditionalOnBean & @ConditionalOnMissingBean

@ConditionalOnBean:組合@Conditional註解,當容器中有指定的 Bean 纔開啓配置。

@ConditionalOnMissingBean:組合@Conditional註解,和@ConditionalOnBean註解相反,當容器中沒有指定的Bean纔開啓配置。

註解源碼:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnBean {
    
}

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnMissingBean {
    
}

10.6.2 @ConditionalOnClass & @ConditionalOnMissingClass

@ConditionalOnClass:組合@Conditional註解,當容器中有指定的Class纔開啓配置。

@ConditionalOnMissingClass:組合@Conditional註解,當容器中沒有指定的Class纔開啓配置。

註解源碼:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {

}

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnMissingClass {
    
}

10.6.3 @ConditionalOnWebApplication & @ConditionalOnNotWebApplication

@ConditionalOnWebApplication:組合@Conditional註解,當前項目類型是WEB項目纔開啓配置。

@ConditionalOnNotWebApplication:組合@Conditional註解,當前項目類型不是WEB項目纔開啓配置。

註解源碼:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnWebApplication {
    
}

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnNotWebApplication {

}

10.6.4 @ConditionalOnProperty

@ConditionalOnProperty:組合@Conditional註解,當指定的屬性有指定的值時纔開啓配置。

註解源碼:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {
    
}

10.6.5 @ConditionalOnExpression

@ConditionalOnExpression:組合@Conditional註解,當SpEL表達式爲true時纔開啓配置。

註解源碼:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnExpressionCondition.class)
public @interface ConditionalOnExpression {
    
}

10.6.6 @ConditionalOnJava

@ConditionalOnJava:組合@Conditional註解,當運行的JVM在指定的版本範圍時纔開啓配置。

註解源碼:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnJavaCondition.class)
public @interface ConditionalOnJava {
    
}

10.6.7 @ConditionalOnResource

@ConditionalOnResource:組合@Conditional註解,當類路徑下有指定的資源纔開啓配置。

註解源碼:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnResourceCondition.class)
public @interface ConditionalOnResource {
    
}

10.6.8 @ConditionalOnJndi

@ConditionalOnJndi:組合@Conditional註解,當指定的 JNDI 存在時纔開啓配置。

註解源碼:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnJndiCondition.class)
public @interface ConditionalOnJndi {
    
}

10.6.9 @ConditionalOnCloudPlatform

@ConditionalOnCloudPlatform:組合@Conditional註解,當指定的雲平臺激活時纔開啓配置。

註解源碼:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnCloudPlatformCondition.class)
public @interface ConditionalOnCloudPlatform {
    
}

10.6.10 @ConditionalOnSingleCandidate

@ConditionalOnSingleCandidate:組合@Conditional註解,當指定的class在容器中只有一個Bean,或者同時有多個但爲首選時纔開啓配置。

源碼註解:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnSingleCandidate {
    
}

10.7 @ConfigurationProperties

@ConfigurationProperties:用來方便加載額外的配置文件中的參數值(application.properties)。

註解源碼:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
    
}

注意:在SpringBoot2.2.1中,該註解的掃描支持默認不啓用,需要添加@ConfigurationPropertiesScan註解,顯式地去掃描以完成初始化動作。

10.8 @EnableConfigurationProperties

該註解一般要配合@ConfigurationProperties註解使用,用來開啓對@ConfigurationProperties註解配置Bean的支持。

註解源碼:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EnableConfigurationPropertiesRegistrar.class)
public @interface EnableConfigurationProperties {
    
}

10.9 @AutoConfigureAfter

該註解用在自動配置類上面,表示該自動配置類需要在另外指定的自動配置類配置完之後。

// Mybatis 的自動配置類,需要在數據源自動配置類之後
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisAutoConfiguration {

}

註解源碼:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
@Documented
public @interface AutoConfigureAfter {
    
}

10.10 @AutoConfigureBefore

該註解和@AutoConfigureAfter註解使用相反,表示該自動配置類需要在另外指定的自動配置類配置之前。

註解源碼:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
@Documented
public @interface AutoConfigureBefore {
    
}

10.11 @Import

@Import是Spring3.0添加的新註解,用來導入@Configuration註解修飾的類,通過快速導入的方式實現把實例加入spring的IOC容器中。也可以導入第三方包。

註解源碼:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
    
}

10.12 @ImportResource

這是 Spring 3.0 添加的新註解,用來導入一個或者多個Spring配置文件,這對SpringBoot兼容老項目非常有用,因爲有些配置無法通過 Java Config 的形式來配置就只能用這個註解來導入。

註解源碼:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface ImportResource {
    
}

10.13 @Component

@Component泛指組件,當組件不好歸類的時候,我們可以使用這個註解進行標註,可以作用在代碼的任何層次。

註解源碼:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {
    
}

@Component@Service@Controller@Repository都是Spring註解,註解後可以被Spring框架所掃描並注入到Spring容器來進行管理。用這些註解對應用進行分層之後,就能將請求處理,業務邏輯處理,數據庫操作處理分離出來,爲代碼解耦,也方便了以後項目的維護和開發。

  • @Component是通用註解,其他三個註解是這個註解的拓展,並且具有了特定的功能。
  • @Repository註解在持久層中,具有將數據庫操作拋出的原生異常翻譯轉化爲spring的持久層異常的功能。
  • @Controller層是SpringMvc的註解,具有將請求進行轉發,重定向的功能。
  • @Service層是業務邏輯層註解,這個註解只是標註該類處於業務邏輯層。

10.11.1 @Repository

@Repository主要作用於持久化層,用來標註數據訪問的DAO組件,該註解除了自動將標識的類識別爲Bean,同時也能將所標註的類中拋出的數據訪問異常封裝爲Spring的數據訪問異常類型。被@Repository註解的類可以自動的被@ComponentScan 通過路徑掃描給找到。

註解源碼:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
    
}

10.13.2 @Service

@Service主要用於標註業務層組件,表明該類處於業務邏輯層。目前該註解與@Component相同。

註解源碼:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
    
}

10.13.3 @Controller

@Controller用於標註是控制層組件,需要返回頁面時請用。目前該註解與@Component相同。

註解源碼:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
    
}

10.14 @RestController

@RestController用於標註控制層組件,作用等同於@Controller + @ResponseBody

註解源碼:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    
}

注意使用@RestController這個註解,就不能返回HTML頁面,視圖解析器無法解析。

10.14.1 @ResponseBody

@ResponseBody表示將 Controller 方法返回的對象,通過適當的轉換器轉換爲指定的格式之後直接寫入HTTP Response Body中,一般在異步獲取數據時使用。使用@ResponseBody後返回結果不會被解析爲跳轉路徑,會直接返回json數據。

註解源碼:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ResponseBody {

}

10.14.2 @RequestBody & @RequestParam

@RequestBody主要用來接收前端傳遞給後端的JSON字符串中的數據,即請求體中的數據。

  • GET方式無請求體,所以使用@RequestBody接收數據時,前端不能使用GET方式提交數據,而是用POST方式進行提交。
  • SpringBoot2後端的接收方法裏,@RequestBody@RequestParam可以共存。
  • 同一個請求中,最多隻能有一個@RequestBody,而可以同時有多個@RequestParam參數。

當同時使用@RequestParam@RequestBody時,

  • @RequestParam指定的參數可以是普通元素、數組、集合、對象等等。即原SpringMVC接收參數的機制不變。
  • 如果參數時放在請求體中,傳入後臺的話,那麼後臺要用@RequestBody才能接收到。
  • 如果參數不放在請求體中的話,那麼後臺要用@RequestParam來接收參數。
  • 如果參數前寫了@RequestParam(xxx),那麼前端必須有對應的xxx名字纔行(不管其是否有值,當然可以通
    過設置該註解的required屬性來調節是否必須傳),如果沒有xxx名的話,那麼請求會出錯,報400。
  • 如果參數前不寫@RequestParam(xxx)的話,前端如果有xxx名的參數的話,那麼就會自動匹配;如果沒有的話,請求也能正確發送。

當後端參數是一個以@RequestBody修飾的對象,那麼前端傳遞JSON數據注意事項:

  • @RequestBody註解對應的類在將HTTP的輸入流(含請求體)裝配到目標類,即:會根據請求體中JSON字符串中的key來匹配對應實體類的屬性。

  • JSON字符串中,如果value爲"“的話,後端對應屬性如果是String類型的,那麼接受到的就是”",如果是後端屬性的類型是Integer、Double等類型,那麼接收到的就是null。

  • JSON字符串中,如果value爲null的話,後端對應收到的就是null。

  • 如果某個參數沒有value的話,在傳JSON字符串給後端時,要麼乾脆就不把該字段寫到json字符串中;要麼寫value時, 必須有值,null 或""都行。

註解源碼:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestBody {
    
}


@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
    
}

10.15 @Autowired

@Autowired把配置好的Bean拿來用,完成屬性、方法的組裝,它可以對類成員變量、方法及構造函數進行標註,完成自動裝配的工作,當加上required=false時,就算找不到bean也不報錯。

註解源碼:

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
    
}

10.16 @RequestMapping

@RequestMapping是一個用來處理請求地址映射的註解,可用於類或方法上。用於類上,表示類中的所有響應請求的方法都是以該地址作爲父路徑。其包含的六個屬性如下:

  • params:指定請求中必須包含某些參數值時才讓該方法處理。

  • headers:指定請求中必須包含某些指定的header值,才能讓該方法處理請求。

  • value:指定請求的實際地址,指定的地址可以是URI Template模式 。

  • method:指定請求的類型:GET、POST、PUT、DELETE等。

  • consumes:指定處理請求的提交內容類型(Content-Type):application/jsontext/html

  • produces:指定返回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才返回。

新註解主要有如下幾個,他們都是組合註解

  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping

相當於@RequestMapping(value=”/”,method=RequestMethod.Get\Post\Put\Delete等) 。

註解源碼:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    
}

10.17 @RequestParam

@RequestParam:相當於 request.getParameter(),將請求參數綁定到控制器的方法參數上。

語法:

@RequestParam(value="name", required = true, defaultValue="zhangsan") String name
  • value:參數名

  • required:是否包含該參數,默認爲true,表示該請求路徑中必須包含該參數,如果不包含就報錯。

  • defaultValue:默認參數值,如果設置了該值,required=true將失效;設置爲false,如果沒有傳該參數,就使用默認值。

註解源碼:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
    
}

10.18 @PathVariable

@PathVariable:通過該註解可以將URL中佔位符參數綁定到控制器處理方法的入參中:URL 中的 {xxx} 佔位符可以通過@PathVariable(“xxx“) 綁定到操作方法的入參中。參數與大括號裏的名字相同的話,註解後括號裏的內容可以不填。

註解源碼:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {
    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章