spring_aop

一、概念部分

  aop:在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

  面向切面编程(AOP)通过提供另外一种思考程序结构的途经来弥补面向对象编程(OOP)的不足。在OOP中模块化的关键单元是类(classes),而在AOP中模块化的单元则是切面。切面能对关注点进行模块化,例如横切多个类型和对象的事务管理。(在AOP术语中通常称作横切(crosscutting)关注点。)

  可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。

  切面(Aspect):一个关注点的模块化,这个关注点可能会横切多个对象。事务管理是J2EE应用中一个关于横切关注点的很好的例子。在Spring AOP中,切面可以使用基于模式)或者基于@Aspect注解的方式来实现。
  连接点(Joinpoint):在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候。在Spring AOP中,一个连接点总是表示一个方法的执行。
  通知(Advice):在切面的某个特定的连接点上执行的动作。其中包括了“around”、“before”和“after”等不同类型的通知(通知的类型将在后面部分进行讨论)。许多AOP框架(包括Spring)都是以拦截器做通知模型,并维护一个以连接点为中心的拦截器链。
  切入点(Pointcut):匹配连接点的断言。通知和一个切入点表达式关联,并在满足这个切入点的连接点上运行(例如,当执行某个特定名称的方法时)。切入点表达式如何和连接点匹配是AOP的核心:Spring缺省使用AspectJ切入点语法。
  引入(Introduction):用来给一个类型声明额外的方法或属性(也被称为连接类型声明(inter-type declaration))。Spring允许引入新的接口(以及一个对应的实现)到任何被代理的对象。例如,你可以使用引入来使一个bean实现IsModified接口,以便简化缓存机制。
  目标对象(Target Object): 被一个或者多个切面所通知的对象。也被称做被通知(advised)对象。 既然Spring AOP是通过运行时代理实现的,这个对象永远是一个被代理(proxied)对象。
  AOP代理(AOP Proxy):AOP框架创建的对象,用来实现切面契约(例如通知方法执行等等)。在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。
  织入(Weaving):把切面连接到其它的应用程序类型或者对象上,并创建一个被通知的对象。这些可以在编译时(例如使用AspectJ编译器),类加载时和运行时完成。Spring和其他纯Java AOP框架一样,在运行时完成织入。
二、通知类型:
  前置通知(Before advice):在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常)。
  后置通知(After returning advice):在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
  异常通知(After throwing advice):在方法抛出异常退出时执行的通知。
  最终通知(After (finally) advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。
  环绕通知(Around Advice):包围一个连接点的通知,如方法调用。这是最强大的一种通知类型。环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它自己的返回值或抛出异常来结束执行。

三、AOP在Spring Framework中的作用
  提供声明式企业服务,特别是为了替代EJB声明式服务。最重要的服务是声明性事务管理。
  允许用户实现自定义切面,用AOP来完善OOP的使用。

四、AOP的三种实现方式 

  4.1实现spring提供接口

    4.1.1日志前置通知  

package edu.aeon.aop1.log;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
/**
 * [说明]:前置通知
 * @author aeon(qq:1584875179)
 *
 */
public class BeforeLog implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] arg, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行!");
    }
    
}

    4.1.2日志后置通知  

package edu.aeon.aop1.log;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
/**
 * [说明]:后置通知
 * @author aeon(qq:1584875179)
 *
 */
public class AfterLog implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] arg2, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行!");
    }
}

    4.1.3业务接口  

package edu.aeon.aop1.service;
/**
 * [说明]:业务接口
 * @author aeon(qq:1584875179)
 *
 */
public interface UserService {
    void add();
    void delete();
    void update();
    void select();
}

    4.1.4业务实现类  

package edu.aeon.aop1.service.impl;

import edu.aeon.aop1.service.UserService;
/**
 * [说明]:业务实现类
 * @author aeon(qq:1584875179)
 *
 */
public class UserServiceImpl implements UserService {

    @Override
    public void add() {
        System.out.println("UserServiceImpl.add()");
    }

    @Override
    public void delete() {
        System.out.println("UserServiceImpl.delete()");
    }

    @Override
    public void update() {
        System.out.println("UserServiceImpl.update()");
    }

    @Override
    public void select() {
        System.out.println("UserServiceImpl.select()");
    }

}

  4.1.5核心配置文件  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     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="beforeLog" class="edu.aeon.aop1.log.BeforeLog" />
     <bean id="afterLog" class="edu.aeon.aop1.log.AfterLog"/>
     <bean id="userService" class="edu.aeon.aop1.service.impl.UserServiceImpl"/>
     <aop:config>
         <aop:pointcut expression="execution(* edu.aeon.aop1.service.impl.*.*(..))" id="pointcut"/>
         <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
         <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
     </aop:config>
 </beans>

    4.1.6测试类  

package edu.aeon.aop1.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import edu.aeon.aop1.service.UserService;

/**
 * [说明]:aop测试
 * @author aeon(qq:1584875179)
 *
 */
public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
        UserService userService= (UserService) applicationContext.getBean("userService");
        userService.add();
    }
}

  测试结果截图如下:

  

  4.2自定义类实现aop

    4.2.1日志类  

package edu.aeon.aop2.log;

/**
 * [说明]:日志
 * @author aeon(qq:1584875179)
 *
 */
public class Log {
    /**
     * 前置通知
     */
    public void before(){
        System.out.println("Log.before()");
    }
    /**
     * 后置通知
     */
    public void after(){
        System.out.println("Log.after()");
    }
}

    4.2.2业务接口  

package edu.aeon.aop2.service;

/**
 * [说明]:业务接口
 * @author aeon(qq:1584875179)
 *
 */
public interface UserService {
    void add();
    void delete();
    void update();
    void select();
}

    4.2.3业务实现类  

package edu.aeon.aop2.service.impl;

import edu.aeon.aop3.service.UserService;
/**
 * [说明]:业务实现类
 * @author aeon(qq:1584875179)
 *
 */
public class UserServiceImpl implements UserService {

    @Override
    public void add() {
        System.out.println("UserServiceImpl.add()");
    }

    @Override
    public void delete() {
        System.out.println("UserServiceImpl.delete()");
    }

    @Override
    public void update() {
        System.out.println("UserServiceImpl.update()");
    }

    @Override
    public void select() {
        System.out.println("UserServiceImpl.select()");
    }

}

    4.2.4核心配置文件  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     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="log" class="edu.aeon.aop2.log.Log" />
     <bean id="userService" class="edu.aeon.aop2.service.impl.UserServiceImpl"/>
    <aop:config>
        <aop:aspect ref="log">
            <aop:pointcut expression="execution(* edu.aeon.aop2.service.impl.UserServiceImpl.add(..))" id="pointcout"/>
            <aop:before method="before" pointcut-ref="pointcout"/>
            <aop:after method="after" pointcut-ref="pointcout"/>
        </aop:aspect>        
    </aop:config>     
 </beans>

    4.2.5测试类  

package edu.aeon.aop2.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import edu.aeon.aop3.service.UserService;
/**
 * [说明]:aop测试
 * @author aeon(qq:1584875179)
 *
 */
public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
        UserService userService= (UserService)applicationContext.getBean("userService");
        userService.add();
    }
}

  测试结果截图如下:

  

  4.3注解方式实现AOP

    4.3.1日志类(关注点/切面)  

package edu.aeon.aop3.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * [说明]:日志
 * @author aeon(qq:1584875179)
 *
 */
//声明一个切面
@Aspect
public class Log {
    /**
     * 前置通知
     */
    @Before("execution(* edu.aeon.aop3.service.impl.*.*(..))")
    public void before(){
        System.out.println("Log.before()");
    }
    /**
     * 后置通知
     */
    @After("execution(* edu.aeon.aop3.service.impl.*.*(..))")
    public void after(){
        System.out.println("Log.after()");
    }
    @Around("execution(* edu.aeon.aop3.service.impl.*.*(..))")
    public Object aroud(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        //方法签名
        System.out.println("方法签名:"+proceedingJoinPoint.getSignature());
        System.out.println("执行目标方法前做某些工作...");
        //执行目标方法
        Object result=proceedingJoinPoint.proceed();
        System.out.println("执行目标方法后做某些工作...");
        //返回目标方法结果
        return result;
    }
}

    4.3.2业务接口      

package edu.aeon.aop3.service;

/**
 * [说明]:业务接口
 * @author aeon(qq:1584875179)
 *
 */
public interface UserService {
    void add();
    void delete();
    void update();
    void select();
}

    4.3.3业务实现类  

package edu.aeon.aop3.service.impl;

import edu.aeon.aop3.service.UserService;
/**
 * [说明]:业务实现类
 * @author aeon(qq:1584875179)
 *
 */
public class UserServiceImpl implements UserService {

    @Override
    public void add() {
        System.out.println("UserServiceImpl.add()");
    }

    @Override
    public void delete() {
        System.out.println("UserServiceImpl.delete()");
    }

    @Override
    public void update() {
        System.out.println("UserServiceImpl.update()");
    }

    @Override
    public void select() {
        System.out.println("UserServiceImpl.select()");
    }

}

    4.3.4核心配置文件  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     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="log" class="edu.aeon.aop3.log.Log" />
     <bean id="userService" class="edu.aeon.aop3.service.impl.UserServiceImpl"/>
    <aop:aspectj-autoproxy/>
 </beans>

    4.3.5测试类  

package edu.aeon.aop3.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import edu.aeon.aop3.service.UserService;
/**
 * [说明]:aop测试
 * @author aeon(qq:1584875179)
 *
 */
public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
        UserService userService= (UserService)applicationContext.getBean("userService");
        userService.delete();
    }
}

  测试结果截图如下:

  

 

如有任何疑问可联系邮箱: 给我发邮件、或直接联系QQ:1584875179 || 点返回首页

原文地址:https://www.cnblogs.com/aeon/p/10281362.html