其中它主要是依赖一个父项目,主要是管理项目的资源过滤及插件
org.springframework.boot spring-boot-starter-parent 2.2.5.RELEASE
点进去,发现还有一个父依赖
org.springframework.boot spring-boot-dependencies 2.2.5.RELEASE ../../spring-boot-dependencies
这里才是真正管理SpringBoot应用里面所有依赖版本的地方,SpringBoot的版本控制中心;
以后我们导入依赖默认是不需要写版本;但是如果导入的包没有在依赖中管理着就需要手动配置版本了
org.springframework.boot spring-boot-starter-xxx
springboot-boot-starter-xxx:就是spring-boot的场景启动器
比如 : spring-boot-starter-web:帮我们导入了web模块正常运行所依赖的组件;
SpringBoot将所有的功能场景都抽取出来,做成一个个的starter (启动器),只需要在项目中引入这些starter即可,所有相关的依赖都会导入进来 ,要用什么功能就导入什么样的场景启动器即可
//@SpringBootApplication 来标注一个主程序类
//说明这是一个Spring Boot应用
@SpringBootApplication
public class SpringbootApplication {public static void main(String[] args) {//以为是启动了一个方法,没想到启动了一个服务SpringApplication.run(SpringbootApplication.class, args);}
}
进入这个注解
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {@Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class}
), @Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class}
)}
)
……
这个注解在Spring中很重要 ,它对应XML配置中的元素。
作用
自动扫描并加载符合条件的组件或者bean , 将这个bean定义加载到IOC容器中
进入这个注解
// 点进去得到下面的 @Component
@Configuration
public @interface SpringBootConfiguration {}@Component
public @interface Configuration {}
@Configuration
说明这是一个配置类 ,配置类就是对应Spring的xml 配置文件
@Component
说明启动类本身也是Spring中的一个组件而已,负责启动应用
作用
开启自动配置功能
该注解告诉SpringBoot开启自动配置功能,这样自动配置才能生效
以前我们需要自己配置的东西,而现在SpringBoot可以自动帮我们配置
进入该注解
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
进入AutoConfigurationImportSelector
// 获得候选的配置
protected List getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {//这里的getSpringFactoriesLoaderFactoryClass()方法//返回的就是我们最开始看的启动自动导入配置文件的注解类;EnableAutoConfigurationList configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");return configurations;
}
public static List loadFactoryNames(Class> factoryClass, @Nullable ClassLoader classLoader) {String factoryClassName = factoryClass.getName();//这里它又调用了 loadSpringFactories 方法return (List)loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
}
private static Map> loadSpringFactories(@Nullable ClassLoader classLoader) {//获得classLoader , 我们返回可以看到这里得到的就是EnableAutoConfiguration标注的类本身MultiValueMap result = (MultiValueMap)cache.get(classLoader);if (result != null) {return result;} else {try {//去获取一个资源 "META-INF/spring.factories"Enumeration urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");LinkedMultiValueMap result = new LinkedMultiValueMap();//将读取到的资源遍历,封装成为一个Propertieswhile(urls.hasMoreElements()) {URL url = (URL)urls.nextElement();UrlResource resource = new UrlResource(url);Properties properties = PropertiesLoaderUtils.loadProperties(resource);Iterator var6 = properties.entrySet().iterator();while(var6.hasNext()) {Entry, ?> entry = (Entry)var6.next();String factoryClassName = ((String)entry.getKey()).trim();String[] var9 = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());int var10 = var9.length;for(int var11 = 0; var11 < var10; ++var11) {String factoryName = var9[var11];result.add(factoryClassName, factoryName.trim());}}}cache.put(classLoader, result);return result;} catch (IOException var13) {throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var13);}}
}
查看构造器:
public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {// ......this.webApplicationType = WebApplicationType.deduceFromClasspath();this.setInitializers(this.getSpringFactoriesInstances();this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));this.mainApplicationClass = this.deduceMainApplicationClass();
}
上一篇:网络相关的命令-新手必备