SpringBootApplication注解

SpringBoot程序启动入口一个是SpringApplication.run,一个是@SpringBootApplication注解,这个注解是由三部分组成:

1. @ComponentScan注解,主要用于组件扫描和自动装配。

2. @SpringBootConfiguration注解,这个注解主要是继承@Configuration注解,主要用于加载配置文件。

3. @EnableAutoConfiguration注解,这个注释启用了Spring Boot的自动配置功能,可以自动为您配置很多东西。

源码

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

}

实例

@SpringBootApplication
public class HelloDemo implements CommandLineRunner {
 
    private static final Logger log = LoggerFactory.getLogger(HelloDemo.class);
 
    public static void main(String args[]) {
        SpringApplication.run(HelloDemo.class);
    }
 
    @Override
    public void run(String...args) throws Exception {
        log.info("hello 51gjie");
    }
}

@SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiguration,是这三个Spring注释的组合,只需一行代码即可提供所有三个注释的功能。

版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。