spring 的注解aop要点

本文介绍如何使用Spring框架实现面向切面编程(AOP),包括必要的依赖包导入、配置文件设置及具体注解的运用。通过示例展示了前置、后置、最终、异常通知以及环绕通知的具体实现。

@Service用于标注service组件。@Repository标注dao组件

Jettison

1.需要导入aspectj包下的两个jar:aspectjrt.jar,aspectjweaver.jar.导入cglib包下的cglib-nodep-2.1_3.jar。j2ee包下的common-annotations.jar,spring.jar

2.在spring配置文件中:头部需要有:xmlns:aop="http://www.springframework.org/schema/aop",xsi:schemaLocation需要有

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

然后需要加<aop:aspectj-autoproxy />

 

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AopInterceptor {

    @Pointcut("execution (*com.huawei.service.impl.PersonServiceImpl.*(..))")
    private void anyMethod(){}
   
    @Before("anyMethod()")
    public void doAccessCheck(){
        System.out.println("前置通知");
    }
   
    @AfterReturning("anyMethod()")
    public void doAfterReturning(){
        System.out.println("后置通知");
    }
   
    @After("anyMethod()")
    public void doAfter(){
        System.out.println("最终通知");
    }   
    @AfterThrowing("anyMethod()")
    public void dofterThrowing(){
        System.out.println("异常通知");
    }
   
    @Around("anyMethod()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
//        if(){//判断用户权限
        System.out.println("进入方法");
            Object result = pjp.proceed();
            System.out.println("退出方法");
//        }
        return result;
       
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值