Spring 扩展点详解
Spring 框架提供了丰富的扩展点,允许开发者在不修改框架源码的情况下定制其行为。以下是 Spring 的主要扩展点分类及说明:
1. Bean 生命周期相关扩展点
-
BeanFactoryPostProcessor
在 Bean 实例化之前,允许修改 Bean 定义(如修改属性值、注册新 Bean)。public interface BeanFactoryPostProcessor { void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; }
-
BeanPostProcessor
在 Bean 实例化后、初始化前后进行拦截(如 AOP 代理、属性注入后的处理)。public interface BeanPostProcessor { Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; }
-
InitializingBean
/@PostConstruct
Bean 初始化完成后执行自定义逻辑(InitializingBean
是接口,@PostConstruct
是注解)。 -
DisposableBean
/@PreDestroy
Bean 销毁前执行清理逻辑(类似初始化的逆过程)。
2. Spring 容器扩展点
-
ApplicationContextAware
让 Bean 获取ApplicationContext
实例(用于访问容器资源)。public interface ApplicationContextAware { void setApplicationContext(ApplicationContext applicationContext) throws BeansException; }
-
BeanFactoryAware
让 Bean 获取BeanFactory
实例(底层容器接口)。 -
EnvironmentAware
让 Bean 获取Environment
对象(用于读取配置属性)。 -
MessageSourceAware
让 Bean 获取国际化消息源(MessageSource
)。 -
ApplicationEventPublisherAware
让 Bean 发布自定义事件(需结合ApplicationEvent
和ApplicationListener
)。
3. 事件监听扩展点
-
ApplicationListener<E extends ApplicationEvent>
监听 Spring 容器发布的事件(如ContextRefreshedEvent
、ContextClosedEvent
)。public interface ApplicationListener<E extends ApplicationEvent> { void onApplicationEvent(E event); }
-
自定义事件
继承ApplicationEvent
发布自定义事件:public class MyEvent extends ApplicationEvent { public MyEvent(Object source) { super(source); } }
4. AOP 扩展点
-
Advisor
/Pointcut
/Advice
Spring AOP 的核心接口,用于定义切面逻辑(如@Aspect
注解的底层实现)。 -
IntroductionInterceptor
支持为 Bean 动态添加接口实现(较少使用)。
5. 数据访问扩展点
-
PlatformTransactionManager
自定义事务管理器(如集成非 JDBC 数据源)。 -
DataSource
自定义数据源(如连接池配置)。 -
JdbcTemplate
/NamedParameterJdbcTemplate
扩展数据库操作模板(需继承或包装)。
6. Web 扩展点(Spring MVC / WebFlux)
-
HandlerInterceptor
拦截 HTTP 请求(类似 Servlet 的 Filter,但更灵活)。public interface HandlerInterceptor { boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler); void postHandle(...); void afterCompletion(...); }
-
ControllerAdvice
全局处理异常、绑定参数、响应结果(结合@ExceptionHandler
等注解)。 -
WebMvcConfigurer
自定义 Spring MVC 配置(如拦截器、视图解析器、静态资源处理)。public interface WebMvcConfigurer { void addInterceptors(InterceptorRegistry registry); // 其他配置方法... }
-
WebFluxConfigurer
类似WebMvcConfigurer
,用于响应式编程(WebFlux)。
7. 其他扩展点
-
BeanDefinitionRegistryPostProcessor
在 Bean 定义加载后、实例化前修改 Bean 定义(比BeanFactoryPostProcessor
更底层)。 -
ImportSelector
/ImportBeanDefinitionRegistrar
动态注册 Bean(通过@Import
注解引入)。public interface ImportSelector { String[] selectImports(AnnotationMetadata importingClassMetadata); }
-
FactoryBean
自定义复杂对象的创建逻辑(返回的 Bean 由容器管理)。public interface FactoryBean<T> { T getObject() throws Exception; Class<?> getObjectType(); boolean isSingleton(); }
-
BeanDefinitionParser
自定义 XML 配置解析(需结合NamespaceHandler
)。
总结
Spring 的扩展点覆盖了从 Bean 生命周期、容器管理、事件监听到 Web 开发的各个环节,开发者可以根据需求选择合适的扩展点进行定制。核心思想是通过接口或注解将业务逻辑注入到 Spring 的流程中,实现非侵入式扩展。