Spring AOP详解

AOP相关术语

  1. 连接点(joinpoint)

    被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法。

  2. 切入点(pointcut)

    切入点是指我们要对哪些连接点进行拦截的定义

  3. 通知(advice)

    所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

  4. 切面(aspect)

    是切入点和通知的结合

  5. 引介(introduction)

    是一种特殊的通知,在不修改代码的前提下,引介可以在运行期为类动态地添加一些方法或字段

  6. 目标对象(Target)

    要代理的目标对象(要增强的类)

  7. 织入(weave)

    将增强应用到目标的过程将advice应用到target的过程

  8. 代理(Proxy)

    一个类被AOP织入增强之后,就产生一个代理类

Spring AOP详解_第1张图片

Spring的AOP配置

配置pom.xml



    4.0.0

    com.by
    Spring_AOP_Xml
    1.0-SNAPSHOT

    
        
        
            org.springframework
            spring-context
            5.1.8.RELEASE
        
         
        
            org.springframework
            spring-aspects
            5.1.8.RELEASE
        
    

dao数据访问层

/**
 * 持久层实现类
 */
public class UserDaoImpl implements UserDao {

    @Override
    public void addUser(){
        System.out.println("insert into tb_user......");
    }
}

service业务层

/**
 * 业务层实现类
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void addUser(){
        userDao.addUser();
    }
}

applicationContext.xml




    
    
    
        
    

 web表现层

/**
 * 模拟表现层
 */
public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
        //使用对象
        UserService userService = ac.getBean("userService",UserService.class);
        System.out.println(userService.getClass());
        userService.addUser();
    }
}

 创建增强类

public class MyLogAdvice {
    //前置通知
    public void before() {
        System.out.println("前置通知");
    }

    //最终通知
    public void after() {
        System.out.println("最终通知通知");
    }
    //后置通知
    public void afterReturning(){
        System.out.println("后置通知");
    }
    public void afterThrowing(){
        System.out.println("异常通知");
    }


    //环绕通知
    public void around(ProceedingJoinPoint joinPoint) {
        try {
            System.out.println("方法执行前的环绕通知");
            joinPoint.proceed();
            System.out.println("方法执行后的环绕通知");
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}

applicationContext.xml配置增强类




    
    
        
    
    
    

    
        



        
        
        

            
            
            
            
        
    

测试结果

Spring AOP详解_第2张图片

Spring AOP详解_第3张图片

基于注解的AOP配置

pom.xml



    4.0.0

    com.by
    Spring_AOP_Annotation
    1.0-SNAPSHOT

    
        
        
            org.springframework
            spring-context
            5.1.8.RELEASE
        
        
            org.springframework
            spring-aspects
            5.1.8.RELEASE
        
    

dao

@Repository
public class UserDaoImpl implements UserDao {

    @Override
    public void addUser(){
        System.out.println("insert into tb_user......");
    }
}

service

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    public void addUser() {
        userDao.addUser();
    }
}

applicationContext.xml





    

    

 web表现层

/**
 * 模拟表现层
 */
public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
        //使用对象
        UserService userService = ac.getBean("userServiceImpl",UserService.class);
        userService.addUser();
    }
}

常用注解

  • @Aspect:把当前类声明为切面类

  • @Before:前置通知,可以指定切入点表达式

  • @AfterReturning:后置【try】通知,可以指定切入点表达式

  • @AfterThrowing:异常【catch】通知,可以指定切入点表达式

  • @After:最终【finally】通知,可以指定切入点表达式

  • @Around:环绕通知,可以指定切入点表达式

注解方式实现aop

@Component
@Aspect//增强类标志
public class MyLogAdvice {
    //前置通知
    @Before("execution(* com.by.service.*.*(..))")
    public void before() {
        System.out.println("前置通知");
    }

    //最终通知
    @AfterReturning("execution(* com.by.service.*.*(..))")
    public void after() {
        System.out.println("最终通知通知");
    }
    //后置通知
    @After("execution(* com.by.service.*.*(..))")
    public void afterReturning(){
        System.out.println("后置通知");
    }
    @AfterThrowing("execution(* com.by.service.*.*(..))")
    public void afterThrowing(){
        System.out.println("异常通知");
    }


    //环绕通知
    @Around("execution(* com.by.service.*.*(..))")
    public void around(ProceedingJoinPoint joinPoint) {
        try {
            System.out.println("方法执行前的环绕通知");
            joinPoint.proceed();
            System.out.println("方法执行后的环绕通知");
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}

测试结果

Spring AOP详解_第4张图片 

你可能感兴趣的:(spring,java,后端)