AOP面向切面编程

Posted 97guoxiang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AOP面向切面编程相关的知识,希望对你有一定的参考价值。

一、相关概念

1、JoinPoint(连接点):所谓连接点是指那些被拦截的点,而spring中这些点就是指方法,因为spring只支持方法类型的连接点。
2、PointCut (切入点);所谓切入点就是指我们要对那些JoinPoint 进行拦截的定义。
3、 Advice (通知增强):所谓通知增强,就是指拦戴到JoinPoint后需要完成的事情。他分为前置通知/增强,后置通知增强,异常通知增强,最终通知增强,环绕通知增强(切面要完成的功能)。
4、 Introduction (引介):引介是一种特殊的Advice, 在不修改代码的前提下,引介可以在运行期为类动态的添加一些方法或Field。
5、Target (目标):代理对象的目标对象(要增强的类)。
6、 Weaving (织入):把Advice应用到Target 的过程。
7、 Proxy (代理): 一个类被AOP注入增强后,就产生了一个结果代理类。
8、Aspect (切面):是PointCut和Advice ( Introduction)的结合。

  如果要对com.gx.service包里面所有类进行加强,那么com.gx.service就是切面

  public void add(),public void update(),public void delete(),这些都叫连接点。

  我要对update()进行增强,那么update()就是切入点,增强/通知:你要扩展的功能,比如你要在删除的时候,记录日志,这个记录日志的方法就叫做增强。

二、应用场景

  对程序进行增强,不修改源码的情况下,权限校验、日志记录、性能监控、事务控制

三、aop的底层实现,代理机制

  jdk的动态代理,针对实现了接口的类产生代理

  cglib的动态代理,针对没有实现接口的类产生代理

四、具体源码,用注解的方式实现

  applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--  加入头文件-->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
        <context:component-scan base-package="com.gx.domain"></context:component-scan>
        <context:component-scan base-package="com.gx.advice"></context:component-scan>
        <!-- 开启注解aop -->
        <aop:aspectj-autoproxy ></aop:aspectj-autoproxy>
</beans>

  目标类

/**
 * 目标类
 * @author Administrator
 *
 */
@Component
public class Man {
    public void eat() {
        System.out.println("吃饭");
    }
}

  增强

/**
 * 通知
 * @author Administrator
 *
 */
@Component
@Aspect
public class MyAdvice {

    /**
     * 前置
     */
    //@Before("execution(* com.gx.domain.*.*(..))")
    public void before() {
        System.out.println("饭前");
        System.out.println("来一杯");
    }
    /**
     * 后置
     */
    //@After("execution(* com.gx.domain.*.*(..))")
    public void after() {
        System.out.println("饭后");
        System.out.println("去散步");
    }
    /**
     * 环绕
     */
    //@Around("execution(* com.gx.domain.*.*(..))")
    public Object aroun(ProceedingJoinPoint jp) {
        Object obj = null;
        before();
        try {
            obj = jp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        after();
        return obj;
    }
    
    /**
     * 异常通知
     * @tv tv代表目标对象的方法执行过程产生的异常对象
     * tv的名字要有xml里面配置的名字一致
     */
    @AfterThrowing(value="execution(* com.gx.domain.*.*(..))"  ,throwing = "tv")
    public void  exception(Throwable tv) {
        System.out.println("产生异常:"+tv.getMessage());
    }
}
public class UserTest {
    public static void main(String[] args) {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "classpath:applicationContext.xml");
    Man man=applicationContext.getBean(Man.class);
    man.eat();
    //int a= 10/0;
    }
}

 

以上是关于AOP面向切面编程的主要内容,如果未能解决你的问题,请参考以下文章

Spring的AOP面向切面编程

面向切面编程

Spring的AOP面向切面编程

三面向切面编程

面向切面编程AOP是面向对象编程OOP的补充

Spring的AOP面向切面编程