SpringAop

博客介绍了使用注解方式实现AOP,如用@Aspect标注切面,@Before、@After、@Around分别实现目标前后及环绕执行。还提及了xml文件配置和Java写法。

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

使用注解方式实现AOP

  • @Aspect
    • 标注这个类是个切面
  • @Before(“execution(* * * *)”)
    • 标注为目标前,相当于aop:before
  • @After(“execution(* * * *)”)
    • 标注为目标后,相当于aop:after
  • @Around(“execution(* * * *)”)
    • 环绕执行的注解

xml文件配置

	<bean id = "userService" class="com.xiaocheng.service.UserServiceImpl"/>
    <bean id = "annotationPointCut" class="com.xiaocheng.diy.annotationPointCut"/>
	<!--    开启aop注解支持-->
    <aop:aspectj-autoproxy/>

Java写法

//  注解定义为切面
@Aspect
public class annotationPointCut {
    @Before("execution(* com.xiaocheng.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("运行前");
    }
    @After("execution(* com.xiaocheng.service.UserServiceImpl.*(..))")
    public void after(){
    	System.out.println("运行后");
   	}
   	//	jp = 连接点
   	@Around("execution(* com.xiaocheng.service.UserServiceImpl.*(..))")
   	public void around(ProceedingJoinPoint jp){
   	System.out.println("环绕前");
   	//	执行方法
   	Object proceed = jp.proceed();
   	System.out.println("环绕后");
   	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值