Spring核心技术AOP案例

在SpringAOP开发之前,首先要了解一下这几个概念。Target:目标,通俗的说对哪个类做增强,那个类就是目标。JoinPoint:连接点,在实际开发中可以被增强的点。PointCut:切入点,在实际开发中真正被增强了的点称为PointCut ,对连接点进行拦截的定义。Advice:通知,在拦截到连接点之后,执行的代码,该通知分为前置,后置,环绕,异常,最终。Weaving:织入,把通知加入到连接点的过程。Proxy:代理,增强之后的对象就称为代理。Introduction:引介,指的是类层面的增强,不需要掌握。Aspect:切面,多个advince和pointCut的组合。下面来着重介绍advince在开发中的运用。在运用之前首先开启aspectj代理(注解开发),如果对以下实体类做增强,如下(接口):

public interface CustomDao {
    
    public void save();//
    
    public Integer delete();//
    
    public void update();//
    
    public void list();//
    
}

实现的类:

@Repository("customDao")
public class CustomDaoImpl implements CustomDao {

    public void save() {
        System.out.println("持久层:保存");
    }

    public Integer delete() {
        System.out.println("持久层:删除");
        return 100;
    }

    public void update() {
        System.out.println("持久层:更新");
    }

    public void list() {
        System.out.println("持久层:查询");
        int i=100/0;
    }
}

首先先来说说前置通知,顾名思义,就是,在方法之前执行的一串代码,这相当于一个拦截器的形式。后置通知跟前置通知类似,它是在目标方法执行之后执行,其注解为:@afterReturning,其中属性中value表示切入点表达式,returning属性指定返回值得名字,该方法形参的名字要与returning的值一致。环绕通知:其注解为:@Around,其属性value指定切入点表达式,但务必要给该方法增加一个形参ProceedingJoinPoint,表示正在执行的连接点(目标),也就是目标方法,joinpoint.proceed( ):表示调用目标方法。异常通知:其注解为:@afterThrowing,其属性value指定切入点表达式,通过throwing属性指定异常的名字,在该方法中增加一个形参,表示发生的的异常,形参的名字与throwing属性值一致。最终通知:其注解为:@After,其属性value指定切入点表达式。在这里,最终通知和后置通知的区别在于,不管异常与否,都执行,而后置通知,在异常时不执行。这些通知将会在切面类中:

@Component
@Aspect  //该注解表示这类为切面类
public class MyAspectAnnotation {
    
//    前置通知
    @Before("execution(*  com.cus.dao.impl.CustomDaoImpl.save(..))")
    public void checkBefore(JoinPoint jp){
        System.out.println("前置通知..."+jp);
    }
    
//    后置通知
    @AfterReturning(value="execution(*  com.cus.dao.impl.CustomDaoImpl.delete(..))",returning="result")
    public void checkAfter(Object result){
        System.out.println("后置通知..."+result);
    }
    
    //环绕通知
    @Around(value="execution(*  com.cus.dao.impl.CustomDaoImpl.update(..))")
    /*
     * ProceedingJoinPoint:正在执行的连接点
     * */
    public Object checkAround(ProceedingJoinPoint pjp){
        System.out.println("环绕通知first...");
        
        Object result=null;
        
        try {
            result = pjp.proceed(); //调用目标的方法
        } catch (Throwable e) {
            e.printStackTrace();
        }
        
        System.out.println("环绕通知last..."+result);
        return result;
    }
    
//    异常通知
    @AfterThrowing(value="execution(*  com.cus.dao.impl.CustomDaoImpl.list(..))",throwing="ex")
    public void checkException(Exception ex){
        System.out.println("这是异常通知..."+ex.getMessage());
    }
    
//    最终通知
    @After(value="execution(*  com.cus.dao.impl.CustomDaoImpl.list(..))")
    public void checkFinal(){
        System.out.println("最终通知");
    }
}

整合Spingjunit测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemo {

    @Autowired
    private CustomDao customDao;
    
    @Test
    public void test() {
        
//        customDao.save();
        
//        customDao.delete();
        
//        customDao.update();
        
        customDao.list();
    }
}

 

posted on 2018-09-20 17:51  lichangyun  阅读(323)  评论(1编辑  收藏  举报