什麼是SpringBoot註解?讓我來告訴你

一、背景

1、大量的XML讓開發者厭惡,因此Spring提供了許多註解來完成各種功能。

2、大量的註解讓開發者厭惡 ,因此Spring提供了組合註解來完成各種功能。

二、舉例

1、最典型的SpringMVC中我們使用如下注解 @Controller

事實上我們接觸到Controller時都會如下說明Controller Service Repository事實上和Component 沒有差別 但是Spring後期可能可能條件不同的語義 因此注意使用

/**

*Indicates that an annotated class is a "Controller" (e.g. a webcontroller).

Advertisements

*

*<p>This annotation serves as a specialization of {@link Component@Component},

*allowing for implementation classes to be autodetected through classpathscanning.

* Itis typically used in combination with annotated handler methods based on the

*{@link org.springframework.web.bind.annotation.RequestMapping} annotation.

Advertisements

*

*@author Arjen Poutsma

*@author Juergen Hoeller

*@since 2.5

*@see Component

*@see org.springframework.web.bind.annotation.RequestMapping

*@see org.springframework.context.annotation.ClassPathBeanDefinitionScanner

*/

@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Component

public @interface Controller {

/**

*The value may indicate a suggestion for a logical component name,

*to be turned into a Spring bean in case of an autodetected component.

*@return the suggested component name, if any

*/

String value() default "";

}

Java

2、我們把修飾其他註解的註解稱之為Meta Annotation【元註解】

A meta-annotation is an annotation that isdeclared on another annotation. An annotation is therefore meta-annotated if itis annotated with another annotation. For example, any annotation that isdeclared to be documented is meta-annotated with @Documented fromthejava.lang.annotation package.

3、當我們業務越發複雜的情況下我們會大量的使用註解進行組合比如

@RestController

@RequestMapping("user")

public class TbUserController extendsAbstractRestController<TbUserVo, TbUserSo, BigInteger> {

@Resource

private TbUserService tbUserService;

}

這個註解還比較少,但是對於SpringBoot 來說可能註解的量會直線上升,比如

@AutoConfigurationPackage

@Import(EnableAutoConfigurationImportSelector.class)

@EnableAutoConfiguration

@ComponentScan(excludeFilters = {

@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),

@Filter(type = FilterType.CUSTOM, classes =AutoConfigurationExcludeFilter.class) })

4、那麼由於如上的問題帶來了一個問題可能一個簡單的類中的代碼比註解還要少!

Java

5、我們通過組合註解的方式來解決此問題

我們聲明如下

@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 {

}

三、Spring註解編程模型

對於Spring來說通常定義的註解是要適配原有的功能比如Controller註解可以聲明Bean的名稱,但是一旦定義的組合註解的名稱相同自然就會存在如下的問題【覆蓋】.

Spring提供了AliasFor註解來完成別名的轉換 比如

@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 {

/**

*Exclude specific auto-configuration classes such that they will never beapplied.

*@return the classes to exclude

*/

@AliasFor(annotation = EnableAutoConfiguration.class, attribute ="exclude")

Class<?>[] exclude() default {};

/**

*Exclude specific auto-configuration class names such that they will never be

*applied.

*@return the class names to exclude

*@since 1.3.0

*/

@AliasFor(annotation = EnableAutoConfiguration.class, attribute ="excludeName")

String[] excludeName() default {};

/**

*Base packages to scan for annotated components. Use {@link#scanBasePackageClasses}

*for a type-safe alternative to String-based package names.

*@return base packages to scan

*@since 1.3.0

*/

@AliasFor(annotation = ComponentScan.class, attribute ="basePackages")

String[] scanBasePackages() default {};

/**

*Type-safe alternative to {@link #scanBasePackages} for specifying the packagesto

*scan for annotated components. The package of each class specified will bescanned.

*<p>

*Consider creating a special no-op marker class or interface in each packagethat

*serves no purpose other than being referenced by this attribute.

*@return base packages to scan

* @since 1.3.0

*/

@AliasFor(annotation = ComponentScan.class, attribute ="basePackageClasses")

Class<?>[] scanBasePackageClasses() default {};

}

Java

通過如上方式可以指定到元註解的屬性【比如SpringBootApplication的scanBasePackageClasses ===》ComponentScan的basePackageClasses】

Java學習資料(複製下段鏈接至瀏覽器即可)

data:textml;charset=UTF-8;base64,5oGt5Zac5L2g77yM5p625p6E5biI5a2m5Lmg576k5Y+35pivNjg2NTc5MDE0Cg==

Advertisements

你可能會喜歡