@SpringBootApplication

115 阅读1分钟

核心注解

大道至简,分而治之!

springboot 核心注解 @SpringbootApplication 是所有功能开始;这个注解主要是包括了 以下三个注解

  • @SpringBootConfiguration (用于加载@bean这种配置类)
  • @EnableAutoConfiguration (自动装配,加载外部jar包中的类到spring容器中)
  • @ComponentScan (包扫描,通过配置包路径从而进行类的加载)
@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 {

图形视图:

Untitled

@SpringBootConfiguration

代码示例:

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

该注解主要是对于@configuration注解的封装,用于替换xml中标签来创建实体类。这样代码中凡是写了@bean注解的类都会被加载到容器中

******

@ComponentScan

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

}

@ComponentScan 该注解用来启动组件扫描,比如@Controller 注解、@Service 注解等,都可以被 @ComponentScan 注解扫描到。

@EnableAutoConfiguration

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

@EnableAutoConfiguration 是 Spring Boot 自动装配的关键,可以开启自动配置的功能