Spring AOP在Bean生命周期中的调用时机

本文探讨了如何使用AOP技术在Spring框架下实现对Bean生命周期方法的增强,通过实例展示了AOP在Bean初始化、配置和销毁过程中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前有写了一个生命周期的例子,直接拿来用,在每个生命周期方法中调用print方法。见上一篇


加上AOP的代码


package com.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AroundAspect {

	@Around("execution(* com.bean.MyMode.*(..))")
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		String methodName = pjp.getSignature().getName();
		System.out.println("----Aspect before " + methodName + " called ");
		Object retVal = pjp.proceed();
		System.out.println("----Aspect after " + methodName + " called ");
		return retVal;
	}


}


运行得到结果:


InstantiationBeanPostProcessor postProcessBeforeInstantiation called


MyMode constructor call
1
MyMode constructor finish


InstantiationBeanPostProcessor postProcessAfterInstantiation called


InstantiationBeanPostProcessor postProcessPropertyValues called


BeanPostProcessor postProcessBeforeInitialization called
1
BeanPostProcessor postProcessBeforeInitialization finish


Mode @PostConstruct anno init called
1
MyMode @PostConstruct anno init finish


Mode afterPropertiesSet called
1
MyMode afterPropertiesSet finish


Mode @Bean anno Init called
1
MyMode @Bean anno Initfinish


BeanPostProcessor postProcessAfterInitialization called
----Aspect before print called 
1
----Aspect after print called 
BeanPostProcessor postProcessAfterInitialization finish


----Aspect before print called 
1
----Aspect after print called 


Mode @PreDestroy anno destory called
1
MyMode @PostConstruct anno destory finish


Mode destroy called
1
MyMode destroy finish


Mode @Bean anno destory called
1
MyMode @Bean anno destory finish



结论是:在Bean的整个生命周期中,只有在初始化方法调用完之后销毁之前,AOP是有效的。

### Spring AOPSpring 应用生命周期中的位置 在 Spring 框架中,AOP(面向切面编程)主要关注于横切逻辑的分离与模块化处理。当涉及到 Spring 应用的生命周期时,AOP 主要在以下几个阶段发挥作用: #### 1. Bean 定义注册之后 一旦所有的 `BeanDefinition` 已经被加载并注册到了容器内,在此期间会分析哪些 bean 需要应用 AOP 增强功能[^2]。 #### 2. 初始化之前 对于那些标记为需要增强的目标对象,在其实际初始化前,Spring 将为其创建相应的代理实例。如果目标组件实现了特定接口,则采用 JDK 动态代理;反之则利用 CGLIB 创建子类形式的代理对象[^1]。 #### 3. 初始化过程中 在这个时刻,已经准备好的代理对象将会替代原始bean成为最终注入给其他依赖者的版本。此时,任何前置通知(Advice)都可以被执行,比如日志记录、权限验证等操作可以在属性填充完毕但尚未对外提供服务之际介入执行。 #### 4. 使用阶段 每当应用程序访问某个受保护的方法或资源时,拦截机制就会触发,并按照预设规则调用对应的 Advice 处理逻辑。这使得开发者能够在不改变业务代码的前提下轻松加入额外的功能特性。 ```java // 示例:定义一个简单的 Aspect 来展示如何围绕方法执行前后添加自定义行为 @Aspect public class LoggingAspect { @Around("execution(* com.example.service.*.*(..))") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long start = System.currentTimeMillis(); try { return joinPoint.proceed(); // 执行连接点本身的操作 } finally { long executionTime = System.currentTimeMillis() - start; System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms"); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值