SpringBoot启动类
@SpringBootApplication public class MySpringbootApplication { public static void main(String[] args) { SpringApplication.run(MySpringbootApplication.class, args); } }
SpringApplication.run方法源码
/** * Static helper that can be used to run a {@link SpringApplication} from the * specified source using default settings. * @param primarySource the primary source to load * @param args the application arguments (usually passed from a Java main method) * @return the running {@link ApplicationContext} */ public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) { return run(new Class<?>[] { primarySource }, args); }
理解:这是一个静态的助手类,可以用于跑一个SpringApplication实例,使用@SpringBootApplication传入一些默认的配置,SpringApplication通过primarySource传入,args需要传入main方法中的一个数组,该方法运行后返回一个运行中的上下文对象ApplicationContext
@SpringBootApplication源码
@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 {}; }
@ComponentScan:用于扫描,会扫描Application启动类当前目录及其子目录下的所有包类
@SpringBootConfiguration:具有@Configuration注解,是一个用于标识配置类的容器
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { }
@EnableAutoConfiguration:用于开启自动装配功能
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import({AutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; Class<?>[] exclude() default {}; String[] excludeName() default {}; }