springAOP学习

AOP概述

多用于事务配置,日志记录

  1. 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
  2. AOP是OOP(面向对象编程)的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。
  3. 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
  4. AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码
  5. 经典应用:事务管理、性能监视、安全检查、缓存 、日志等【画图】
  6. Spring AOP使用纯Java实现,不需要专门的编译过程和类加载器,在运行期通过代理方式向目标类织入增强代码
  7. AspectJ是一个基于Java语言的AOP框架,Spring2.0开始,Spring AOP引入对Aspect的支持,AspectJ扩展了Java语言,提供了一个专门的编译器,在编译时提供横向代码的织入

AOP手动实现方法

a. aop底层将采用代理机制进行实现。示例:

package com.yejj.service;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class StudentServiceFactory {
    public static StudentService createStudentService(){
        final StudentService stu = new StudentServiceImpl();
        final MyAspect aspect = new MyAspect();
        //创建jdk代理
        /* public static Object newProxyInstance(ClassLoader loader,  类加载器,默认写当前类
                                          Class<?>[] interfaces,    接口,此接口中的方法会被拦截
                                          InvocationHandler h)   调用处理
                                          */
        StudentService studentService = (StudentService) Proxy.newProxyInstance(StudentServiceFactory.class.getClassLoader(), stu.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                aspect.before();//调用方法前的业务处理
                Object obj = method.invoke(stu,args);
                System.out.println("拦截的返回值:"+obj);
                aspect.after();//调用方法后的业务处理
                //这个方法的返回值是业务方法的返回值
                return obj;
            }
        });
        return studentService;
    }
}

b. 接口 + 实现类 :采用 jdk 的Proxy来生成代理对象。
c. 普通类+(接口+实现类):都可以采用 cglib字节码增强来实现代理。

1.3 AOP术语

1.target:目标类,需要被代理的类。例如:UserService
2.Joinpoint(连接点):所谓连接点是指那些可能被拦截到的方法。例如:所有的方法
3.PointCut 切入点:已经被增强的连接点。例如:addUser()
4.advice 通知/增强,增强代码。例如:after、before
5. Weaving(织入):是指把增强advice应用到目标对象target来创建新的代理对象proxy的过程.
6.proxy 代理类
7. Aspect(切面): 是切入点pointcut和通知advice的结合
一个线是一个特殊的面。
一个切入点和一个通知,组成成一个特殊的面。
在这里插入图片描述

cglib 增强字节码

 没有接口,只有实现类。
 采用字节码增强框架 cglib,在运行时动态创建目标类的子类,从而对目标类进行增强。
 导入jar包:
核心:hibernate-distribution-3.6.10.Final\lib\bytecode\cglib\cglib-2.2.jar
依赖:struts-2.3.15.3\apps\struts2-blank\WEB-INF\lib\asm-3.3.jar
spring-core…jar 已经整合以上两个内容
示例:

package com.yejj.service;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;

public class StudentServiceFactory2 {
    public static studentServerceNew createStudentService(){
        //创建目标类
        final studentServerceNew stu = new studentServerceNew();
        //声明切面对象
        final MyAspect aspect = new MyAspect();
        //创建增强对象
        Enhancer enhancer = new Enhancer();
        //设置拦截类
        enhancer.setSuperclass(stu.getClass());
        //设置拦截的回调方法
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                //
                aspect.before();
                //执行类中的方法
                 Object obj =method.invoke(stu,objects);
                aspect.after();
                return obj;
            }
        });
        //创建代理对象
        studentServerceNew stunew = (studentServerceNew) enhancer.create();
        return stunew;
    }
}

AOP联盟通知类型

AOP联盟为通知Advice定义了org.aopalliance.aop.Advice
Spring按照通知Advice在目标类方法的连接点位置,可以分为5类

•前置通知 org.springframework.aop.MethodBeforeAdvice
•在目标方法执行前实施增强
•后置通知 org.springframework.aop.AfterReturningAdvice
•在目标方法执行后实施增强
•环绕通知 org.aopalliance.intercept.MethodInterceptor
•在目标方法执行前后实施增强
•异常抛出通知 org.springframework.aop.ThrowsAdvice
•在方法抛出异常后实施增强
•引介通知 org.springframework.aop.IntroductionInterceptor
在目标类中添加一些新的方法和属性

Spring AOP半自动实现

package com.yejj.service;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/*
 * 切面类*/
public class MyAspect2 implements MethodInterceptor {
    public void before(){
        System.out.println("开启事务");
    }
    public void after(){
        System.out.println("提交事务");
    }

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        //拦截方法
        System.out.println("启动事务");
        //放行
       Object obj = methodInvocation.proceed();
        System.out.println("停止事务");

        return obj;
    }
}

spring配置:spring配置![在这里插入图片描述](https://img-blog.csdnimg.cn/20191008164618457.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1lJQU1TT0NPT0w=,size_16,color_FFFFFF,t_70

spring AOP全自动实现

<?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:context="http://www.springframework.org/schema/context"
       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/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd">
         
    <bean id="studentService" class="com.yejj.service.StudentServiceImpl"></bean>
    <!--配置切面类-->
    <bean id="myAspect2" class="com.yejj.service.MyAspect2"></bean>
    <!--AOP实现全自动配置
    proxy-target-class="true" 配置是否使用cglib
    expression="execution(* com.yejj.service.*.*(..))"
                         返回值     包名      类名  方法名(参数)
    AOP:多用于事务配置,日志记录
    -->
    <aop:config proxy-target-class="true">
        <!--切入点-->
        <aop:pointcut id="myPointcut" expression="execution(* com.yejj.service.*.*(..))"></aop:pointcut>
        <!--切面类关联切入点-->
        <aop:advisor advice-ref="myAspect2" pointcut-ref="myPointcut"></aop:advisor>
    </aop:config>
</beans>

AspectJ简介

 AspectJ是一个基于Java语言的AOP框架
 Spring2.0以后新增了对AspectJ切点表达式支持
 @AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
 新版本Spring框架,建议使用AspectJ方式来开发AOP
 主要用途:自定义开发
使用示例:

<?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:context="http://www.springframework.org/schema/context"
       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/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="studentService" class="com.yejj.service.StudentServiceImpl"></bean>

    <!--配置切面类-->
    <bean id="myAspect3" class="com.yejj.service.MyAspect2"></bean>

    <aop:config proxy-target-class="true">
        <!--切面-->
        <aop:aspect ref="myAspect3">
            <aop:pointcut id="myPointcut" expression="execution(* com.yejj.service.*.*(..))"></aop:pointcut>
            <aop:before method="before"pointcut-ref="myPointcut"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值