springboot学习之启动类中的@SpringBootApplication注解

来源:互联网 发布:怎么把淘宝小号养到2心 编辑:程序博客网 时间:2024/06/07 07:23

最近公司对原有框架进行微服务升级,使用的是SpringCould框架,springboot是SpringCloud的基础,随着不断对SpringBoot的了解,越发的发现它的强大;在这里简单总结自己所学的;

提到SpringBoot,很吸引人的一点就是他的启动类,通过一个简单的main方法就可以运行,去除了spring中那些烦人的xml配置;它可以直接打包成jar/war包,然后这个jar/war包也是可以直接运行,不需另外配置;

在启动类中有一个非常重要的注解:@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 {

其中@SpringboootConfiguration也是一个符合注解

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Configurationpublic @interface SpringBootConfiguration {}
这些注解好多都是对元信息进行标记:但实际上对SpringBoot来说最重要的只有三个注解,是一个“三体”结构:

@Configuration
@EnableAutoConfiguration
@ComponentScan

因此启动类注解写成如下形式和原来使用@SpringBootApplication是一样的

@ComponentScan@Configuration@EnableAutoConfigurationpublic class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
这里@Configuration对我们来说并不陌生,它就是JavaConfig形式的Spring IOC容器的配置类使用的那个@Confinguration;既然SpringBoot应用骨子里就是一个Spring应用,那么自然也要加载某个Ioc容器的配置;而springBoot社区推荐使用javaConfig形式;所以很明显,这里的启动类标注了@Configuration之后,其实也是一个Ioc容器的配置类;这都是spring当中的东西,没什么特殊;

@EnableAutoConfiguration:也是一个复合注解,springboot引入了自动装配


在spring中有各种以Enable开头的定义:@EnableScheduling,@EnableMBeanExpot等,@EnableAutoConfiguration就是基于这些注解,简单概括下就是借助@Import的支持,手机和注册各种特定场景相关的bean定义:

比如@EnableScheduling是通过@Import将spring调度框架相关的bean加载到Ioc容器中;

@EnableMBeanExport是通过@Import将JMX相关的bean加载Ioc容器中;

在组成@EnableAutoConfiguration的各种复合注解中,最重要的是@Import({EnableAutoConfigurationImportSelector.class})借助EnableAutoConfigurationImportSelector类,@EnableAutoConfiguration可以帮助springboot将所有符合条件的@Conguration配置都加载到当前springboot并创建ioc容器;

@EnableAutoConfiguration注解实现自动装配可以概括为:从class path中搜寻所有META-INF/Spring.factories配置文件,将其中org.springframework.boot.autoconfigure.EnableAutoConfiguration对应配置项通过反射的形式实例化为标注了@Configuration和javaconfig形式的IOC容器配置类,然后汇总为一个并加载到ioc容器中;

# PropertySource Loadersorg.springframework.boot.env.PropertySourceLoader=\org.springframework.boot.env.PropertiesPropertySourceLoader,\org.springframework.boot.env.YamlPropertySourceLoader# Run Listenersorg.springframework.boot.SpringApplicationRunListener=\org.springframework.boot.context.event.EventPublishingRunListener# Application Context Initializersorg.springframework.context.ApplicationContextInitializer=\org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\org.springframework.boot.context.ContextIdApplicationContextInitializer,\org.springframework.boot.context.config.DelegatingApplicationContextInitializer# Application Listenersorg.springframework.context.ApplicationListener=\org.springframework.boot.builder.ParentContextCloserApplicationListener,\org.springframework.boot.cloudfoundry.VcapApplicationListener,\org.springframework.boot.context.FileEncodingApplicationListener,\org.springframework.boot.context.config.AnsiOutputApplicationListener,\org.springframework.boot.context.config.ConfigFileApplicationListener,\org.springframework.boot.context.config.DelegatingApplicationListener,\org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener,\org.springframework.boot.logging.ClasspathLoggingApplicationListener,\org.springframework.boot.logging.LoggingApplicationListener
以上是从SpringBoot的autoconfigura依赖包中的META-INF/spring.factories配置文件中的内容,可以很好地说明问题:根据@EnableAutoConfiguration的完整类名作为key,查找出一组对应的@Configuration类;

最后说下@ConponeentScan这个注解:

这个注解作用和spring中的使用没有任何区别,就是自动扫描并加载符合条件的组件或bean定义,最终将这些bean加载到ioc容器中;因此在springboot项目中,会把代码放在启动类的同级目录或下级目录下;
当然加载bean定义到spring的ioc容器中,我们可以手动单个注册,不一定非要通过批量的自动扫描完成,所以启动类中的这个注解也是可以去掉的。(但一般没人这么做)

原创粉丝点击