spring Boot深入分析
1.pom文件
父项目
//这个为继承的父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
//它的父项目是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
//这个父项目是真正管理springboot应用的依赖版本 也就是springboot的版本仲裁中心
这也就是说以后导入以来不需要版本号(没有在dependencies里面管理的依赖那需要版本号的)
导入的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web;
spring-boot-starter 是springboot的场景启动器,帮我们导入了web的模块正常运行所需的依赖组件
springboot将所有的功能场景抽取出来,做成一个个的starters(启动器),只需要在项目中引入这些starter相关场景的所有依赖都导入进来,需要什么功能就导入什么场景启动器
2.主程序类.主入口类
@SpringBootApplication
public class HelloWordMainApplication {
public static void main(String[] args) {
//spring应用启动
SpringApplication.run(HelloWordMainApplication.class);
}
}
@SpringBootApplication springboot应用标注在某一个类上,说明这个类是springboot的主配置类.spring boot 就运行这个类的main方法来启动springboot应用;
下面为@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 {
1.@SpringBootConfiguration
是spring boot的配置类,标注在某个类上,表示这是spring boot的配置类;
下面是@SpringBootConfiguration
的源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
@SpringBootConfiguration
配置类上来标注这个注解
配置类-------配置文件;
配置类也是容器中的一个组件@Component
2.@EnableAutoConfiguration
开启自动配置功能(为什么呢?);
以前我们需要配置的东西,spring boot帮我们自动配置,此注解告诉spring boot开启自动配置功能;这样自动配置才生效
下面是@EnableAutoConfiguration
的源码
@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage
自动配置包
下面是@AutoConfigurationPackage
的源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {
发现用的是spring的底层注解,给容器中加入一个组件,导入的组件AutoConfigurationPackages.Registrar.class
将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到spring容器中(为什么呢);
@Import(EnableAutoConfigurationImportSelector.class)
给容器导入组件(原因1)
组件是EnableAutoConfigurationImportSelector.class
导入哪些组件的选择器?
将所有需要导入的组件以全类名的方式返回;这些组件就会添加到容器中,
此组件会给容器中导入很多的自动配置类(最重要的作用)
将这个场景需要的所有组件导入容器中,并配置好这些组件
自动配置的类 有了这些自动配置类,免去了我们需要手动编写配置,注入功能组件等操作
下面为AutoConfigurationPackages.Registrar.class的getCandidateConfigurations()方法
SpringFactoriesLoader.loadFactoryNames(
getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
上面的方法实参为
SpringFactoriesLoader.loadFactoryNames(
EnableAutoConfiguration.class, classLoader);
简单来说就是 spring boot在启动是会从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效 |
ssm我们需要手动配置的文件,现在自动配置类帮我们配置好了
J2EE的整体解决方案和自动配置都在
spring-boot-autoconfigure-1.5.9.RELEASE.jar这个路径下
学习不同场景的源码点击对应的场景即可
附图
下面是组件AutoConfigurationPackages.Registrar.class类的源码的registerBeanDefinitions()方法(原因2)
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata,
BeanDefinitionRegistry registry) {
register(registry, new PackageImport(metadata).getPackageName());
}
metadata是注解的源信息
new PackageImport(metadata).getPackageName()
此代码会获取包名 结果是这样:com.ymh