基于AspectJ的AOP开发

2021-03-11 19:32

阅读:730

标签:nts   ssr   work   encoding   utf-8   text   coding   rod   find   

AspectJ的简介

技术图片

AspectJ的注解开发AOP(上)

环境准备

 junit
      junit
      4.11org.springframework
      spring-beans
      4.2.4.RELEASEorg.springframework
      spring-core
      4.2.4.RELEASEorg.springframework
      spring-context
      4.2.4.RELEASEorg.springframework
      spring-expression
      4.2.4.RELEASEaopalliance
      aopalliance
      1.0org.springframework
      spring-aop
      4.2.4.RELEASEorg.aspectj
      aspectjweaver
      1.8.9org.springframework
      spring-aspects
      4.2.4.RELEASEorg.springframework
      spring-test
      4.2.4.RELEASE

通知类型的介绍

技术图片

切入点表达式的定义

技术图片

AspectJ的注解开发AOP(下)

前置通知

技术图片

package com.jesse.aspectj.demo1;

public class ProductDao {
    public void save(){
        System.out.println("保存商品");
    }

    public void update(){
        System.out.println("修改商品");
    }

    public void delete(){
        System.out.println("删除商品");
    }

    public void findOne(){
        System.out.println("查询一个商品");
    }

    public void findAll(){
        System.out.println("查询所有商品");
    }
}

package com.jesse.aspectj.demo1;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * 切面类
 */
@Aspect
public class MyAsepctAnno {

    @Before(value = "execution(* com.jesse.aspectj.demo1.ProductDao.save(..))")
    public void before(JoinPoint joinPoint){
        System.out.println("前置通知 " + joinPoint);
    }
}

package com.jesse.aspectj.demo1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

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

    @Resource(name = "productDao")
    private ProductDao productDao;

    @Test
    public void demo1(){
        productDao.save();
        productDao.update();
        productDao.delete();
        productDao.findAll();
        productDao.findOne();
    }

}

后置通知

技术图片

    public String update(){
        System.out.println("修改商品");
        return "hello";
    }
    @AfterReturning(value = "execution(* com.jesse.aspectj.demo1.ProductDao.update(..))",returning = "result")
    public void afterReturing(Object result){
        System.out.println("后置通知 " + result);
    }

环绕通知

技术图片

    @Around(value = "execution(* com.jesse.aspectj.demo1.ProductDao.delete(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前通知");
        Object obj = joinPoint.proceed();//执行目标方法
        System.out.println("环绕后通知");
        return obj;
    }

异常抛出通知

技术图片

    public void findOne(){
        System.out.println("查询一个商品");
        //int i = 1/0;
    }
    @AfterThrowing(value = "execution(* com.jesse.aspectj.demo1.ProductDao.findOne(..))" ,throwing = "e")
    public void afterThrowing(Throwable e){
        System.out.println("异常抛出通知 " + e.getMessage());
    }

最终通知

技术图片

    public void findAll(){
        System.out.println("查询所有商品");
        //int j = 1/0;
    }
    @After(value = "execution(* com.jesse.aspectj.demo1.ProductDao.findAll(..))")
    public void after(){
        System.out.println("最终通知");
    }

切点命名

技术图片

    @Before(value = "myPointcut1()")
    public void before(JoinPoint joinPoint){
        System.out.println("前置通知 " + joinPoint);
    }

    @Pointcut(value = "execution(* com.jesse.aspectj.demo1.ProductDao.save(..))")
    private void myPointcut1(){}

AspectJ的XML方式开发AOP

前置通知

技术图片

package com.jesse.aspectj.demo2;

public interface CustomerDao {
    public void save();
    public void update();
    public void delete();
    public void findOne();
    public void findAll();
}

package com.jesse.aspectj.demo2;

public class CustomerDaoImpl implements CustomerDao {
    @Override
    public void save() {
        System.out.println("保存客户");
    }

    @Override
    public void update() {
        System.out.println("修改客户");

    }

    @Override
    public void delete() {
        System.out.println("删除客户");

    }

    @Override
    public void findOne() {
        System.out.println("查询一个客户");

    }

    @Override
    public void findAll() {
        System.out.println("查询多个客户");

    }
}

package com.jesse.aspectj.demo2;

public class MyAspectXml {

    //前置通知
    public void before(){
        System.out.println("XML方式的前置通知");
    }
}

package com.jesse.aspectj.demo2;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:applicationContext2.xml")
public class SpringDemo2 {

    @Resource(name = "customerDao")
    private CustomerDao customerDao;;

    @Test
    public void demo1(){
        customerDao.save();
        customerDao.update();
        customerDao.delete();
        customerDao.findOne();
        customerDao.findAll();
    }
}

其他通知类型的配置

    
    
        
        
        
        
        
        
        
        
            
            
            
            
            
            
            
            
            
            
        
    

基于AspectJ的AOP开发

标签:nts   ssr   work   encoding   utf-8   text   coding   rod   find   

原文地址:https://www.cnblogs.com/jessekkk/p/12831876.html


评论


亲,登录后才可以留言!