Spring註解篇,學完註解深入瞭解SpringBoot更容易

程式設計師xiaozhang發表於2023-02-25

由於Spring Boot專案底層也都是Spring,使用Spring Boot就需要對Spring的註解有一定的瞭解,這次就把Spring的部分註解聊一下。熟悉了Spring的註解使用Spring Boot開發更是得心應手。

@ComponentScan:用於指定掃描包的路徑,只有在它指定的包下 的類,才能歸Spring管理。

Provides support parallel with Spring XML's {@code <context:component-scan>} element。
ComponentScan 這個註解類似於XML配置的context:component-scan的功能

@Bean在不指定Scope的情況下,所有的Bean都是單例項的,而且是餓漢載入,也就是容器啟動的時候例項就建立好了。

指定Scope為prototype的時候表示多例項的,而且是餓漢載入(容器啟動的時候不會建立,只有在第一次使用的時候才建立。這個@Bean註解常常和@Configuration註解一起使用(PS想更詳細瞭解Configuration這個配置類註解請看我的這篇文章)

@Contional 進行條件判斷的註解。(這個註解SpringBoot底層用的多,我們如果寫公共的jar特別是自己寫Starter的時候會常用到,自己功能開發的時候用的不多)

Contional:有條件的Spring中的註釋如下 :Indicates that a component is only eligible for registration when all match
加了Contional。表示元件只有滿足Contional的判斷條件才有資格注入Spring容器Indicates:表示,表明。eligible:有資格的

@Component :Java類加上這個註解這個類就歸Spring容器管理了,代表它是Spring的一個元件。(PS:加註解的類需要在ComponentScan包掃描的範圍內),還有開發中常常用到的@Controller,@Service,@Configuration也都屬於Component,如下所示:

@Component
public @interface Configuration 
@Component
public @interface Controller 
@Component
public @interface Service {
上面3個類上面都加了Component註解,他們標註的註解都是Spring的元件
當然還有其他的註解也都加了Component,就不一一列舉了。

component: 組成部分;成分;部件Spring 中的描述 Indicates that an annotated class is a "component".
加了註解的類代表是一個元件

@Autowired 預設是按照型別匹配的方式。

容器中查詢匹配的bean,當且僅有一個匹配的bean時。Spring將其注入到@Autowired所標註的變數中。如果容器中有一個以上匹配的bean時,則可以透過@Qualifier註解限定bean的名稱。(PS一般開發的時候一個介面只有一個實現類,當寫的有2個的時候可以用這種方式完成注入不報錯)。

Autowired:自動裝配In case of a {@link java.util.Collection} or {@link java.util.Map} dependency type, the container autowires all beans matching the declared value type. For such purposes, the map keys must be declared as type String which will be resolved to the correspondingbean names. 

大概解釋就是當容器中存在多個同一種型別的要注入的時候,需要指定名稱。PS:Qualifier這個剛好可以指定名稱。

@EnableConfigurationProperties 有2個作用如下:

1:對類的配置繫結的功能。

2:把這個類自動注入的容器中。

這個註解的作用就是使 @ConfigurationProperties生效,如果一個配置類只配置@ConfigurationProperties註解,而沒有配置@Component,那麼在IOC容器中是獲取不到properties配置轉換的bean的 。

@EnableConfigurationProperties 註解相當於把使用 @ConfigurationProperties 註解的類進行了一次注入。(PS有的開發者就有疑問說:想把類注入到Spring容器的把類加上@Component不就可以了。但有的時候我們引入的是其他的jar包我們自己無法直接加@Component註解的,所以需要用到EnableConfigurationProperties這個註解),Spring官方也是非常支援開發者這樣做的。

enable:使能夠。properties:特性property的複數

/**
 * Enable support for {@link ConfigurationProperties}
 * annotated beans.{@link ConfigurationProperties} beans 
 * can be registered in the standard way (for example 
 * using {@link Bean @Bean} methods) or, for convenience, 
 * can be specified directly on this annotation.
 * 這個註解Spring 也是推薦結合 ConfigurationProperties註解一起使用
 */
@Import(EnableConfigurationPropertiesImportSelector.class)
public @interface EnableConfigurationProperties {

@Import :上一篇文章講了它的2個作用,這個註解還有第三個作用是匯入

ImportBeanDefinitionRegistrar然後我們可以實現手動注入bean到Spring容器(用法寫一個類實現ImportBeanDefinitionRegistrar,然後重寫實現類的方法就可以幫助我們注入相關的bean)。如下

 

public @interface Import {
  /**
   * {@link Configuration}, 
   * {@link ImportSelector}, 
   * {@link ImportBeanDefinitionRegistrar},第三個作用匯入
   * 的實現類 ImportBeanDefinitionRegistrar
   * or regular component classes to import.
   */
  Class<?>[] value();

這篇文章只聊註解的作用,沒說實際使用,希望都能看得懂,也希望寫的這些對你能有部分作用。

 

相關文章