Spring学习笔记day03——AOP(一)

AOP

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术语

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

AOP实现原理

  1. aop底层将采用代理机制进行实现。

  2. 接口 + 实现类 :spring采用 jdk 的动态代理Proxy。

  3. 实现类:spring 采用 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 已经整合以上两个内容

手动代理

  • JDK代理
  • cglib增强字节码
示例
  • 目标类
package spring.service;

public interface UserService {
    //切面编程
    public void addUser();
    public void updateUser();
    public void deleteUser();
}
package spring.service.impl;

import org.springframework.stereotype.Service;
import spring.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Override
    public void addUser() {
        System.out.println("添加用户");
    }

    @Override
    public void updateUser() {
        System.out.println("更新用户");
    }

    @Override
    public void deleteUser() {
        System.out.println("删除用户");
    }
}


package spring.service;

public class StudentService {
    public void add()
    {
        System.out.println("添加学生");
    }

    public void update()
    {
        System.out.println("修改学生");
    }

    public void delete()
    {
        System.out.println("删除学生");
    }

}

```
  • 切面类
package spring.service;

/**
 * 切面类:增强代码与切入点的结合
 */
public class MyAspect {
    public void before()
    {
        System.out.println("开启事务");
    }

    public void after()
    {
        System.out.println("提交事务");
    }
}

  • 工厂类
package spring.service;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import spring.service.impl.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ServiceFactory {
    /**
     * jdk实现的代理方法
     * @return
     */
    public static UserService createUserService()
    {
        //1. 创建日标对象
        final UserService service = new UserServiceImpl();

        //2. 声明界面类对象
        final MyAspect myAspect = new MyAspect();

        //3. 把切面类的2个方法应用到目标类
        //3.1 创建一个jdk的代理,拦截方法

        /*public static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h);
         * ClassLoader loader  类加载器
         * Class<?>[] interfaces  接口,指定接口下所有方法都要被拦截
         * InvocationHandler h  处理
         */
        UserService serviceProxy = (UserService) Proxy.newProxyInstance(
                ServiceFactory.class.getClassLoader(),
                service.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //开启事务
                        myAspect.before();

                        //这个方法的返回值是业务方法的返回值
                        Object obj = method.invoke(service, args);
                        System.out.println("拦截的返回值"+obj);

                        //提交事务
                        myAspect.after();
                        return obj;
                    }
                }
        );

        return serviceProxy;
    }

    /**
     * cglib实现的代理
     * @return
     */
    public static StudentService createStudentService()
    {
        //1. 创建日标对象
        final StudentService service = new StudentService();

        //2. 声明切面 类对象
        final MyAspect myAspect = new MyAspect();

        //3. 创建增强对象
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(service.getClass());
        //设置回调拦截
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object proxy, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
               //开启事务
                myAspect.before();

                //放行方法
                Object invoke = methodProxy.invokeSuper(proxy,objects);

                //提交事务
                myAspect.after();
                return invoke;
            }
        });

        //创建代理对象
        StudentService serviceProxy = (StudentService) enhancer.create();
        return serviceProxy;
    }
}

  • 测试类
/**
     * 测试jdk实现的代理
     * @throws Exception
     */
    @Test
    public void test1() throws Exception {
        UserService service = ServiceFactory.createUserService();
        service.deleteUser();
    }
 /**
     * 使用cglib来实现代理
     */
    @Test
    public void test2(){
        StudentService service = ServiceFactory.createStudentService();
        service.add();
    }

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编写代理半自动

  • 导包
    com.springsource.org.aopalliance-1.0.0.jar
    spring-aop-3.2.0.RC2.jar
    在这里插入图片描述
  • 配置beans.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:context="http://www.springframework.org/schema/context"
               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
"
>
    <!--配置UserService-->
    <bean id="userService" class="spring.service.impl.UserServiceImpl"/>
    <!--配置切面对象-->
    <bean id="myAspect" class="spring.service.MyAspect2"/>
    <!--配置代理对象-->
    <bean id="serviceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--接口-->
        <property name="interfaces" value="spring.service.UserService"/>
        <!--目标对象-->
        <property name="target" ref="userService"/>
        <!--切面类-->
        <property name="interceptorNames" value="myAspect"/>
        <!--配置cglib生成代理-->
        <property name="optimize" value="true"/>
    </bean>


</beans>
  • 切面类
package spring.service;

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


/**
 * 切面类:增强代码与切入点的结合
 */
public class MyAspect2 implements MethodInterceptor {


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

        return obj;
    }
}

  • 测试
 @Test
    public void test3()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
        UserService service = (UserService) context.getBean("serviceProxy");
        service.deleteUser();
    }
  • 提示,Spring编写代理半自动默认的是jdk生成代理,可通过配置文件来使cglib生成代理
		<!--配置cglib实现代理-->
        <property name="optimize" value="true"/>

Spring全自动编程

  • 导包
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
  • 配置beans.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: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
"
>
    <!--配置UserService-->
    <bean id="userService" class="spring.service.impl.UserServiceImpl"/>
    <!--配置切面对象-->
    <bean id="myAspect" class="spring.aspect.MyAspect2"/>

    <!--全自动AOP配置
    1.在beans.xml中配置约束
    2.配置aop:config内容,把切入点和通知结合
    proxy-target-class="true"  使用cglib实现代理
    -->
    <aop:config proxy-target-class="true">
        <!--切入点
            expression:表达式

            execution(* spring.*.*(..)) 表示 spring.service包下 所有的类 所有的方法 所有的参数类型 都要加事务
        -->
        <aop:pointcut id="myPointcut" expression="execution(* spring.service.*.*(..))"/>

        <!--通知-->
        <aop:advisor advice-ref="myAspect" pointcut-ref="myPointcut"></aop:advisor>
    </aop:config>


</beans>
  • 测试类
 @Test
    public void test4()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
        UserService service = (UserService) context.getBean("userService");
        service.deleteUser();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值