如何通过Spring AOP的@Before切面在multiply()调用前执行assignValues()
如何通过Spring AOP在调用multiply()前自动执行assignValues()
嗨,我来帮你搞定这个Spring AOP的需求!你想要在调用MathService的无参multiply()方法前,自动触发assignValues()为a和b赋值,咱们只需要调整切面的切点逻辑,并在前置通知里主动调用赋值方法就行,具体步骤如下:
1. 调整切面类的@Before切点与逻辑
你现在的@Before切面是监听assignValues()自身的调用,这显然不是咱们要的效果。我们需要把切点改成监听无参的multiply()方法,然后在切面方法里获取目标MathService实例,调用它的assignValues()。
修改后的切面类代码:
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.stereotype.Component; import java.util.Date; import com.test.service.MathService; @Component @Aspect public class TimeLoggingAspect { // 保留原有的Around通知(如果需要的话) @Around("execution(* com.test.service.*.*(..))") public Object userAdvice(ProceedingJoinPoint joinPoint) throws Throwable{ System.out.println("@Around: Before calculation-"+ new Date()); Object result = joinPoint.proceed(); // 注意要返回结果,否则无参multiply的返回值会丢失 System.out.println("@Around: After calculation-"+ new Date()); return result; } // 修改后的@Before切面:监听无参的multiply()方法 @Before("execution(* com.test.service.MathService.multiply())") public void logBeforeAssignValues(JoinPoint joinPoint) { System.out.println("-before multiply(): 开始执行assignValues()赋值-"); // 获取目标MathService实例,调用assignValues() MathService mathService = (MathService) joinPoint.getTarget(); mathService.assignValues(); } }
关键说明:
- 切点表达式
execution(* com.test.service.MathService.multiply())精准匹配无参的multiply方法,不会影响带两个int参数的重载方法。 - 通过
joinPoint.getTarget()获取被代理的原始MathServicebean实例,直接调用assignValues()完成属性赋值。 - 注意原
Around通知里要返回joinPoint.proceed()的结果,否则无参multiply()的返回值会被吞掉。
2. 完善测试类的Spring上下文配置
你的测试类里需要补全Spring上下文的初始化逻辑,确保能扫描到Service和Aspect组件:
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import com.test.service.MathService; public class SpringAOPTest { public static void main(String[] args) { // 配置Spring上下文,开启AOP代理并扫描组件 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); MathService mathService = ctx.getBean(MathService.class); Integer result = mathService.multiply(); // 此时会自动先执行assignValues() System.out.println("最终结果:" + result); ctx.close(); } // 配置类:开启AOP并扫描指定包 @Configuration @ComponentScan("com.test") @EnableAspectJAutoProxy public static class AppConfig {} }
关键说明:
@EnableAspectJAutoProxy必须加上,否则Spring不会启用AOP代理功能。@ComponentScan("com.test")扫描你的com.test包下所有带@Service、@Component的类,包括MathService和TimeLoggingAspect。
3. 运行效果
当你运行测试类时,控制台会输出类似如下内容:
@Around: Before calculation-xxxx-xx-xx xx:xx:xx -before multiply(): 开始执行assignValues()赋值- --multiply called--- values :: 3*10= 30 @Around: After calculation-xxxx-xx-xx xx:xx:xx 最终结果:30
可以看到,在multiply()执行前,assignValues()已经被自动调用,a和b被赋值为3和10,乘法计算结果正确。
内容的提问来源于stack exchange,提问作者user3684675
相关产品推荐
相关产品推荐

