AOP面向切面编程

1AOP概述
AOP(Aspect-Oriented Programming,面向切面编程):是一种新的方法论,是对传统 OOP(Object-Oriented Programming,面向对象编程)的补充。两种编程思想。是对面向对象编程的一种补充。
面向切面编程:是指在程序运行期间将某段代码,动态的切入到某个类的指定方法的指定位置的这种编程思想叫做面向切面编程。
AOP编程操作的主要对象是切面(aspect),而切面模块化横切关注点。
在应用AOP编程时,仍然需要定义公共功能,但可以明确的定义这个功能应用在哪里,以什么方式应用,并且不必修改受影响的类。这样一来横切关注点就被模块化到特殊的类里——这样的类我们通常称之为“切面”。
AOP的好处:每个事物逻辑位于一个位置,代码不分散,便于维护和升级
业务模块更简洁,只包含核心业务代码

2AOP术语
2.1横切关注点:从每个方法中抽取出来的同一类非核心业务。
2.2切面(Aspect):封装横切关注点信息的类,每个关注点体现为一个通知方法。
2.3通知(Advice):切面必须要完成的各个具体工作
2.4目标(Target):被通知的对象
2.5代理(Proxy):向目标对象应用通知之后创建的代理对象
2.6连接点(Joinpoint)
横切关注点在程序代码中的具体体现,对应程序执行的某个特定位置。例如:类某个方法调用前、调用后、方法捕获到异常后等。
在应用程序中可以使用横纵两个坐标来定位一个具体的连接点:
2.7切入点(pointcut):定位连接点的方式。每个类的方法中都包含多个连接点,所以连接点是类中客观存在的事物。如果把连接点看作数据库中的记录,那么切入点就是查询条件——AOP可以通过切入点定位到特定的连接点。切点通过org.springframework.aop.Pointcut 接口进行描述,它使用类和方法作为连接点的查询条件。

AOP面向切面编程

实现AOP编程
1.创建目标类Taget

package com.bdqn.aop;

/**
 * Created by 18054 on 2018/9/25.
 */
//目标类
public class Taget {
    public String show(String name) throws Exception {
        System.out.println("这是目标方法。。。。");

        if (1/0==1){
            throw  new Exception("除数不能为0");
        }
        return "hello"+name;
    }
}

创建目标增强类AdviceTaget

package com.bdqn.aop;

import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;

/**
 * Created by 18054 on 2018/9/25.
 */
//增强类
public class AdviceTaget {
    public void before(JoinPoint jp){
        System.out.println("参数"+jp.getArgs()+",方法名"+jp.getSignature()+",目标对象:"+jp.getTarget());
    }
    public void after(JoinPoint jp){
        System.out.println("这是后置增强");
    }
    public void afterReturn(JoinPoint jp,String value ){
        System.out.println("后置带返回值"+value);
    }
    public void afterException(JoinPoint jp,Exception ex){
        System.out.println("后置带异常"+ex);
    }

}

创建spring_aop.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="target" class="com.bdqn.aop.Taget"></bean>
    <bean id="adviceTarget" class="com.bdqn.aop.AdviceTaget"></bean>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(public String show(String))"></aop:pointcut>
        <aop:aspect ref="adviceTarget">
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
            <aop:after-returning method="afterReturn" pointcut-ref="pointcut" returning="value"/>
            <aop:after-throwing method="afterException" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
        </aop:aspect>
    </aop:config>
</beans>

创建测试类Test

    @Test
    public void testaop(){
        ApplicationContext context=new ClassPathXmlApplicationContext("spring_aop.xml");
        Taget taget=(Taget) context.getBean("target");
        try {
            taget.show("郝鑫");
        } catch (Exception e) {
            System.out.println("除数不能为0");
        }
    }