感受:
其实我投简历的时候,都不太敢投递阿里。因为在阿里一面前已经过了字节的三次面试,投阿里的简历一直没被捞,所以以为简历就挂了。
特别感谢一面的面试官捞了我,给了我机会,同时也认可我的努力和态度。对比我的面经和其他大佬的面经,自己真的是运气好。别人8成实力,我可能8成运气。所以对我而言,我要继续加倍努力,弥补自己技术上的不足,以及与科班大佬们基础上的差距。希望自己能继续保持学习的热情,继续努力走下去。
也祝愿各位同学,都能找到自己心动的offer。
分享我在这次面试前所做的准备(刷题复习资料以及一些大佬们的学习笔记和学习路线),都已经整理成了电子文档
- 总结
一、自动配置介绍
我们都知道,在没有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;
最后
由于文案过于长,在此就不一一介绍了,这份Java后端架构进阶笔记内容包括:Java集合,JVM、Java并发、微服务、SpringNetty与 RPC 、网络、日志 、Zookeeper 、Kafka 、RabbitMQ 、Hbase 、MongoDB、Cassandra 、Java基础、负载均衡、数据库、一致性算法、Java算法、数据结构、分布式缓存等等知识详解。
本知识体系适合于所有Java程序员学习,关于以上目录中的知识点都有详细的讲解及介绍,掌握该知识点的所有内容对你会有一个质的提升,其中也总结了很多面试过程中遇到的题目以及有对应的视频解析总结。
[外链图片转存中…(img-mSV0XkuF-1715649499584)]
[外链图片转存中…(img-yfzaDqcc-1715649499584)]