在使用SpringBoot框架进行开发时在启动类上必须标注的就是@SpringBootApplication接口,运行启动类即可启动一个SpringBoot应用。
      如果不标注此接口则会报如下异常:

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
......

      Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.记住这个ServletWebServerFactory,很重要。

类前注释

      进入SpringBootApplication注解源码我们可以发现其被标注了三个注解:SpringBootConfigurationEnableAutoConfigurationComponentScan。突发奇想,在启动类使用者三个注解可以不。去试试吧!您会发现没问题的^_^(文档也说了呢)。

//...其他java注解
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

文档原文及翻译:

Indicates a {@link Configuration configuration} class that declares one or more
 {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
 auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
 annotation that is equivalent to declaring {@code @Configuration},
 {@code @EnableAutoConfiguration} and {@code @ComponentScan}.

标注了一个配置类,此类上声明了一个或多个@Bean方法并且触发自动配置和组件扫描。这个一个方便的注解,等同于标注@Configuration、@EnableAutoConfiguration和@ComponentScan这三个注解。

      关于引申出来的三个注解在下面三篇文章中介绍:

  • 1
  • 2
  • 3

注解属性

/**
 * 排除指定的自动配置类(classname.class),它们将用于不会被使用到
 * @return 排除的类
 */
@AliasFor(annotation = EnableAutoConfiguration.class)
Class<?>[] exclude() default {};


/**
 * 排除指定的自动配置类(packageName.classname),它们将用于不会被使用到
 * @return 排除的类
 * @since 1.3.0
 */
@AliasFor(annotation = EnableAutoConfiguration.class)
String[] excludeName() default {};


/**
 * 基础包扫描注释组件。使用scanBasePackageClasses的类型安全的替代基于字符串的包名。
 * 注:该设置是只是@ComponentScan的别名。它没有影响@Entity扫描或Spring Data Repository扫描。 
 * 对于那些你应该添加@EntityScan和@Enable...Repositories的注解
 * @return 扫描的基础包名
 * @since 1.3.0
 */
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};


/**
 * 替代scanBasePackages用于指定包扫描注释组件。 指定的每个类的包将被扫描。
 * 考虑在每个程序包中创建一个特殊的无操作标记类或接口,除了被该属性引用以外,没有其他用途。
 * 注:该设置是只是@ComponentScan的别名。它没有影响@Entity扫描或Spring Data Repository扫描。 
 * 对于那些你应该添加@EntityScan和@Enable...Repositories的注解
 * @return 扫描基础类名
 * @since 1.3.0
 */
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};


/**
 * 指定是否应强制执行@Bean方法以强制执行bean生命周期行为,例如,即使在用户代码中直接调用 @Bean 方法可以返回共享的singleton bean实例。
 * 此功能需要通过运行时生成的CGLIB子类来实现方法拦截,该子类具有局限性,例如配置类及其方法不允许声明为 final。
 * 默认值为 true ,允许在配置类中使用"inter-bean references",以及对该配置的 @Bean 方法的外部调用,例如从另一个配置类中调用。
 * 如果由于每个特定配置的 @Bean 方法都是自包含的并且被设计为供容器使用的普通工厂方法,则不需要这样做,请将此标志切换为 false ,以避免处理CGLIB子类。
 * 阻止Bean方法拦截可以有效地分别处理 @Bean 方法,就像在非 @Configuration 类上声明时一样,也就是" @Bean Lite Mode"(将@Bean运用在@Component类或普通类中的行为,称为lite mode)。 
 * 因此,从行为上讲,它等效于删除 @Configuration。
 * @since 2.2
 * @return 是否代理 @Bean 方法
 */
@AliasFor(annotation = Configuration.class)
boolean proxyBeanMethods() default true;
Last modification:May 28th, 2020 at 12:20 pm
如果觉得我的文章对你有用,请随意赞赏