@SpringBootApplication
SpringBoot项目入类上的标签,一般此类中会有main方法。
是多个注解的组合
@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 {
@AliasFor(
annotation = EnableAutoConfiguration.class
)
Class<?>[] exclude() default {};
@AliasFor(
annotation = EnableAutoConfiguration.class
)
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}
@SpringBootConfiguration
其实这个注解就是@Configuration的封装,如源码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
@EnableAutoConfiguration
该注解表示“开启自动配置”
@ComponentScan
开启自动扫描,当不配置路径时,默认扫描路径为@SpringBootApplication所在类的同级目录以及子目录下的相关注解。
@Target({ElementType.TYPE})
表示注解的作用目标,ElementType指作用目标的类型:(源码英文注释很清晰)
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}
@Retention
代表注解的保留位置,其中的RetentionPolicy代表保留规则:
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
@Documented
说明该注解将被包含在javadoc中
@Inherited
说明子类可以继承父类中的该注解
@SpringBootApplication是Spring Boot核心注解,它整合了@Configuration、@EnableAutoConfiguration和@ComponentScan。该注解用于标记在主类上,启动自动配置并进行组件扫描。@SpringBootConfiguration封装了@Configuration,@EnableAutoConfiguration开启自动配置,@ComponentScan则负责组件扫描,当未指定路径时,会扫描同级及子目录下相关注解。

2669

被折叠的 条评论
为什么被折叠?



