一、自动配置介绍
我们都知道,在没有SpringBoot
之前,利用Spring
进行开发的时候,研发需要花费大量精力去定义模板化的各类配置文件。Spring
最初使用Bean Factory
以及动态代理实现各模块之间的解耦,它通过配置文件将bean扫描到Spring容器中。而SpringBoot
将这种xml解析配置的过程,通过注解自动配置的方式来进行替换,它根据定义在classpath下的类,自动生成对应的bean
,同时将其加载到Spring
的context
中。SpringBoot
通过条件化配置来启动某个能力项。
在SpringBoot启动类WebApplication中,可以看到很多个注解。我们知道SpringBoot项目是高度依赖注解的,它可以在main函数中启动整个应用。
@SpringBootApplication(scanBasePackages = {“com.test”})
@MapperScan(“com.test.module.mapper”)
@ImportResource(locations = {“classpath:springMVC-servlet.xml”})
@ServletComponentScan
public class WebApplication extends SpringBootServletInitializer{
static Logger logger = LoggerFactory.getLogger(WebApplication.class);
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
以上代码中,@SpringBootApplication
是SpringBoot
的核心注解,它是一系列注解的集合。它对应的源码如下所示。在这些注解当中@EnableAutoConfiguration
即为当前的项目提供自动配置功能,它也是一系列注解的集合。该注解可以让Spring Boot
根据类路径中的jar包依赖为当前项目进行自动配置。
@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 {};
}
通过注解的方式实现配置的自动化,主要在spring-boot-autoconfigure-1.4.3.RELEASE-sources.jar这个jar包中提供了对于SpringBoot自动化配置的支持。这个jar包中包含了如下包,篇幅有限只列出了部分包。
在这个jar包中的META-INF文件夹中,可以看到spring.factories文件
在spring.factories文件中我们看到了一些初始化的类、监听器以及构建类等等。
我们具体看一下@EnableAutoConfiguration
这个注解里面的内容。Spring中有很多@Enable-*开头的注解,类似@EnableScheduling
以及@EnableCaching
等等,这类注解即为该修饰的类赋予某项能力,在每个该类注解中都会通过@Import注解来导入实现对应功能的类。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = “spring.boot.enableautoconfiguration”;
/**
-
Exclude specific auto-configuration classes such that they will never be applied.
-
@return the classes to exclude
*/
Class<?>[] exclude() default {};
/**
-
Exclude specific auto-configuration class names such that they will never be
-
applied.
-
@return the class names to exclude
-
@since 1.3.0
*/
String[] excludeName() default {};
}
在该注解中引入了EnableAutoConfigurationImportSelector
这个类,按照这个类的字面理解为自动配置导入选择器,它实现了以下几个接口。
public class AutoConfigurationImportSelector
implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware,
BeanFactoryAware, EnvironmentAware, Ordered{
…
}
在这个类中,使用SpringFactoriesLoader.loadFactoryNames方法来扫描具有META-INF/spring.factories文件的jar包,spring-boot-autoconfigure-x.x.x.x.jar里就有一个spring.factories文件,这个文件中声明了有哪些类要自动配置。
protected List getCandidateConfigurations(AnnotationMetadata metadata,
AnnotationAttributes attributes) {
//获取
List configurations = SpringFactoriesLoader.loadFactoryNames(
getSpringFactoriesLoaderFactoryClass(), 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;
}
二、Kafka自动配置源码分析
下面分析下Kafka自动配置类,贴上其源码,相关源码已经加上注释。
//配置注解
@Configuration
//KafkaTemplate类在classpath目录下存在时,才会去解析KafkaAutoConfiguration自动配置类
@ConditionalOnClass(KafkaTemplate.class)
//自动注入属性,如果在application.properties配置文件中定义,则会将配置文件中key对应的value值注入到KafkaProperties中
@EnableConfigurationProperties(KafkaProperties.class)
//导入KafkaAnnotationDrivenConfiguration
@Import(KafkaAnnotationDrivenConfiguration.class)
public class KafkaAutoConfiguration {
private final KafkaProperties properties;
private final RecordMessageConverter messageConverter;
public KafkaAutoConfiguration(KafkaProperties properties,
ObjectProvider messageConverter) {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数同学面临毕业设计项目选题时,很多人都会感到无从下手,尤其是对于计算机专业的学生来说,选择一个合适的题目尤为重要。因为毕业设计不仅是我们在大学四年学习的一个总结,更是展示自己能力的重要机会。
因此收集整理了一份《2024年计算机毕业设计项目大全》,初衷也很简单,就是希望能够帮助提高效率,同时减轻大家的负担。
既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!
由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频
如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
1712535638005)]
[外链图片转存中…(img-MP5EyVmk-1712535638006)]
[外链图片转存中…(img-5Taye2FO-1712535638006)]
既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!
由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频
如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
[外链图片转存中…(img-3XvDN6o1-1712535638006)]