springboot学习之启动类中的@SpringBootApplication注解
2017-12-10 11:44
691 查看
最近公司对原有框架进行微服务升级,使用的是SpringCould框架,springboot是SpringCloud的基础,随着不断对SpringBoot的了解,越发的发现它的强大;在这里简单总结自己所学的;
提到SpringBoot,很吸引人的一点就是他的启动类,通过一个简单的main方法就可以运行,去除了spring中那些烦人的xml配置;它可以直接打包成jar/war包,然后这个jar/war包也是可以直接运行,不需另外配置;
在启动类中有一个非常重要的注解:@SpringBootApplication;实际上它是一个符合注解,翻看源码即可看到:
其中@SpringboootConfiguration也是一个符合注解
@Configuration
@EnableAutoConfiguration
@ComponentScan
因此启动类注解写成如下形式和原来使用@SpringBootApplication是一样的
@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 Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader
# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener
# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
org.springframework.boot.context.config.DelegatingApplicationContextInitializer
# Application Listeners
org.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容器中,我们可以手动单个注册,不一定非要通过批量的自动扫描完成,所以启动类中的这个注解也是可以去掉的。(但一般没人这么做)
提到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 @Configuration public @interface SpringBootConfiguration { }这些注解好多都是对元信息进行标记:但实际上对SpringBoot来说最重要的只有三个注解,是一个“三体”结构:
@Configuration
@EnableAutoConfiguration
@ComponentScan
因此启动类注解写成如下形式和原来使用@SpringBootApplication是一样的
@ComponentScan @Configuration @EnableAutoConfiguration public 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 Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader
# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener
# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
org.springframework.boot.context.config.DelegatingApplicationContextInitializer
# Application Listeners
org.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容器中,我们可以手动单个注册,不一定非要通过批量的自动扫描完成,所以启动类中的这个注解也是可以去掉的。(但一般没人这么做)
相关文章推荐
- SpringBoot学习之spring-boot:run启动时,指定spring.profiles.active
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- Springboot系列:@SpringBootApplication注解
- SpringBoot系列三:SpringBoot基本概念(统一父 pom 管理、SpringBoot 代码测试、启动注解分析、配置访问路径、使用内置对象、项目打包发布)
- SpringBoot启动报错 ApplicationStartingEvent,刚入门使用springboot
- springboot入门---入口类注解@SpringBootApplication解析
- SpringBoot的注解:@SpringBootApplication注解 vs @EnableAutoConfiguration+@ComponentScan+@Configuration
- Spring boot源码分析-SpringBootApplication注解(8)
- Spring 学习-------WebApplication (web 容器)启动
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- springboot 学习笔记【3】Spring Boot中使用Spring-data-jpa
- Springboot-启动 Spring Boot服务的方式
- Spring boot 学习笔记(05)——各种常用注解
- springboot使用Application启动失败,bootrun启动成功
- 【系统学习SpringBoot】SpringBoot初遇Spring-Data-JPA
- springboot jenkins 部署 通过shell执行,最后springboot启动失败
- Springboot启动报错:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default
- Spring Boot学习——Spring Boot配置文件application
- spring-boot启动报错【This application has no explicit mapping for /error.....】
- SpringBoot学习5之自定义注解