SpringBoot 高级特性与源码解析指南
大约 4 分钟
一、核心启动原理
1. SpringBoot 启动流程源码分析
SpringBoot 的启动过程主要包含以下关键步骤:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}核心流程解析:
创建 SpringApplication 对象
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { // 设置资源加载器 this.resourceLoader = resourceLoader; // 判断是否是 Web 环境 this.webApplicationType = WebApplicationType.deduceFromClasspath(); // 设置初始化器 setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class)); // 设置监听器 setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); // 推断主启动类 this.mainApplicationClass = deduceMainApplicationClass(); }运行 SpringApplication
public ConfigurableApplicationContext run(String... args) { // 计时器启动 StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; // 准备环境 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); // 创建 ApplicationContext context = createApplicationContext(); // 刷新上下文 refreshContext(context); stopWatch.stop(); return context; }
2. 自动配置原理
@SpringBootApplication 注解是一个组合注解,包含:
@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan
重点关注 @EnableAutoConfiguration 的工作原理:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
}核心逻辑在 AutoConfigurationImportSelector 类中:
public String[] selectImports(AnnotationMetadata annotationMetadata) {
// 加载自动配置类
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
// 排除重复配置
configurations = removeDuplicates(configurations);
// 排除指定的配置
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
configurations.removeAll(exclusions);
return configurations.toArray(new String[configurations.size()]);
}二、高级特性实战
1. 自定义 Starter 开发
创建一个简单的日志记录 starter:
@Configuration
@ConditionalOnClass(LogService.class)
@EnableConfigurationProperties(LogProperties.class)
public class LogAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public LogService logService(LogProperties properties) {
return new LogService(properties);
}
}配置文件:
# application.properties
custom.log.enabled=true
custom.log.prefix=SYSTEM2. 自定义条件注解
创建一个基于特定条件的配置:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Conditional(OnCustomCondition.class)
public @interface ConditionalOnCustom {
String value() default "";
}
public class OnCustomCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 自定义条件逻辑
return true;
}
}3. 事件监听机制
自定义事件和监听器:
// 自定义事件
public class CustomEvent extends ApplicationEvent {
public CustomEvent(Object source) {
super(source);
}
}
// 事件监听器
@Component
public class CustomEventListener implements ApplicationListener<CustomEvent> {
@Override
public void onApplicationEvent(CustomEvent event) {
// 处理事件
}
}
// 发布事件
@Autowired
private ApplicationEventPublisher publisher;
public void doSomething() {
publisher.publishEvent(new CustomEvent(this));
}三、性能调优实战
1. Spring Boot Actuator 高级运用
@Configuration
public class ActuatorConfig {
@Bean
public HealthIndicator customHealthIndicator() {
return () -> {
// 自定义健康检查逻辑
return Health.up()
.withDetail("customKey", "customValue")
.build();
};
}
}2. 自定义线程池配置
@Configuration
public class ThreadPoolConfig {
@Bean
public ThreadPoolTaskExecutor asyncThreadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(200);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("Async-Service-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}3. 缓存优化
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
ConcurrentMapCache usersCache = new ConcurrentMapCache("users");
cacheManager.setCaches(Arrays.asList(usersCache));
return cacheManager;
}
}
@Service
public class UserService {
@Cacheable(value = "users", key = "#id")
public User getUser(Long id) {
// 数据库查询逻辑
}
}四、源码解析案例
1. Spring Boot 配置加载源码分析
配置优先级:
- 命令行参数
- java:comp/env 中的 JNDI 属性
- Java 系统属性(System.getProperties())
- 操作系统环境变量
- RandomValuePropertySource 配置的 random.* 属性值
- JAR 包外部的 application-{profile}.properties 或 application.yml 文件
- JAR 包内部的 application-{profile}.properties 或 application.yml 文件
- @Configuration 注解类上的 @PropertySource
- 通过 SpringApplication.setDefaultProperties 指定的默认属性
关键源码:
public class StandardEnvironment extends AbstractEnvironment {
/** 系统属性 */
public static final String SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME = "systemProperties";
/** 系统环境 */
public static final String SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME = "systemEnvironment";
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(
new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
propertySources.addLast(
new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}
}2. Spring Boot 异常处理机制
全局异常处理:
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception e) {
ErrorResponse error = new ErrorResponse(
HttpStatus.INTERNAL_SERVER_ERROR.value(),
e.getMessage()
);
return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(CustomException.class)
public ResponseEntity<ErrorResponse> handleCustomException(CustomException e) {
ErrorResponse error = new ErrorResponse(
e.getStatus().value(),
e.getMessage()
);
return new ResponseEntity<>(error, e.getStatus());
}
}五、面试要点
SpringBoot 自动配置原理
- @SpringBootApplication 注解的组成
- @EnableAutoConfiguration 的工作机制
- Spring Factories 加载机制
Spring Boot 启动流程
- SpringApplication 的初始化过程
- run 方法的执行流程
- 事件监听机制
条件注解工作原理
- @ConditionalOnClass
- @ConditionalOnMissingBean
- @ConditionalOnProperty
- 自定义条件注解的实现
自定义 Starter
- 基本组成部分
- 自动配置类的编写
- 配置属性类的定义
- Spring Factories 的配置
性能优化
- 线程池配置
- 缓存使用策略
- JVM 参数调优
- 数据库优化
六、实战案例
1. 分布式限流实现
@Aspect
@Component
public class RateLimiterAspect {
private final RedisTemplate<String, String> redisTemplate;
@Around("@annotation(rateLimiter)")
public Object rateLimit(ProceedingJoinPoint point, RateLimiter rateLimiter) throws Throwable {
String key = rateLimiter.key();
int count = rateLimiter.count();
int time = rateLimiter.time();
String countKey = "rate_limit:" + key;
String lockKey = "lock:" + key;
try {
// 获取计数器
Integer currentCount = redisTemplate.opsForValue().get(countKey) != null ?
Integer.valueOf(redisTemplate.opsForValue().get(countKey)) : 0;
if (currentCount >= count) {
throw new RuntimeException("访问太频繁,请稍后再试");
}
// 计数器加1
redisTemplate.opsForValue().increment(countKey);
// 设置过期时间
redisTemplate.expire(countKey, time, TimeUnit.SECONDS);
return point.proceed();
} finally {
redisTemplate.delete(lockKey);
}
}
}2. 分布式事务实现
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private ProductMapper productMapper;
@Transactional
@GlobalTransactional
public void createOrder(Order order) {
// 创建订单
orderMapper.insert(order);
// 扣减库存
productMapper.decreaseStock(order.getProductId(), order.getQuantity());
// 可能的异常
if (someCondition) {
throw new RuntimeException("创建订单失败");
}
}
}总结
以上内容涵盖了 SpringBoot 的核心原理、高级特性、性能优化、源码分析等方面。建议:
- 先掌握基础原理
- 动手实践案例
- 深入源码学习
- 结合实际项目经验
- 注重性能调优
持续学习和实践是提升的关键。如有任何问题,欢迎随时讨论。