美文网首页
Spring框架之AOP概念详解和应用(二)

Spring框架之AOP概念详解和应用(二)

作者: 程序猿峰岑 | 来源:发表于2022-07-14 23:31 被阅读0次
AOP概述
  • 在软件业,AOP为Aspect Oriented Programming的缩写,意为面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
  • AOP是OOP面向对象编程的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范围型。
  • 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
  • AOP采用横向抽取机制,取代了传统纵向继承体系重复性代码。
  • 经典应用,事务管理,性能监听,安全检查,缓存和日志等
  • Spring AOP使用纯Java实现,不需要专门的编译和类加载器,在运行期通过代理方式向目标类织入增强代码。
  • Aspect是基于Java语言的AOP框架,Spring2.0开始,Spring AOP进入对Aspect的支持,AspectJ扩展Java语言,提供了一个专门的编译器,在编译时提供横向代码的织入。
AOP实现原理
  • aop底层将采用代理机制进行实现
  • 接口+实现类,Spring采用jdk的动态代理proxy
  • 实现类:Spring采用cglib字节码增强
AOP术语
  • target:目标类,需要被代理的类,例如:UserService
  • JoinPoint(连接点):所谓连接点是指那些可能被拦截到的地方。如所有的方法。
  • advice通知/增强代码。如after、before。
  • weaving(织入):是指把增强advice应用到目标对象target来创建新的代理对象Proxy的过程。
  • Proxy:代理
  • Aspect(切面):是切入点pointcut和通知advice的结合。一条线是一个特殊的面。
    一个切入点和一个通知,组成一个特殊的面。


    image.png

手动代理

JDK动态代理
 public static IUserService createUserService() {
        final MyAspect myAspect = new MyAspect();
        final IUserService userService = new UserServiceImpl();
        IUserService proxy = (IUserService)Proxy.newProxyInstance(MyFactory.class.getClassLoader(), userService.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                myAspect.before();
                Object obj = method.invoke(userService,args);
                System.out.println("拦截返回值:" + obj);
                myAspect.after();
                return obj;
            }
        });

        return proxy;
    }
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已经整合以上两个内容。
public static IUserService createUserService() {
   //目标类
   final IUserService userService = new UserServiceImpl();
   //切面类
   final MyAspect aspect = new MyAspect();
   //3 cglib核心类
   Enhancer enhancer = new Enhancer();
   enhancer.setSuperclass(userService.getClass());
   enhancer.setCallback(new MethodInterceptor() {
      @Override
      public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
             aspect.before();
             Object obj = methodProxy.invokeSuper(proxy,args);
             aspect.after();
             return obj;
         }
      });
  UserServiceImpl proxy = (UserServiceImpl) enhancer.create();
  return proxy;
}
AOP联盟通知类型

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

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

目标掌握让Spring创建代理对象,从Spring容器中手动的获取代理对象。
第一步:导入jar包
核心4+1,AOP联盟(规范)、Spring-aop(实现)
com.springsource.org-aopalliance-1.0.0.jar AOP联盟
spring-aop-3.2.0.RELEASE.jar AOP实现。
第二步:目标类
IUserService

public interface IUserService {
    void addUser(User user);
    void updateUser(User user);
    void delete(String username);
}

UserServiceImpl

@Service("userService")
public class UserServiceImpl implements IUserService {
    
    private UserDaoImpl userDao = new UserDaoImpl();

    public void setUserDao(UserDaoImpl userDao) {
        this.userDao = userDao;
    }

    @Override
    public void addUser(User user) {
        userDao.save(user);
    }

    @Override
    public void updateUser(User user) {
        userDao.update(user);
    }

    @Override
    public void delete(String username) {
        userDao.delete(username);
    }
}

第三步:切面类

public class UserAspect implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("before...................");
        Object obj = methodInvocation.proceed();
        System.out.println("after....................");
        return obj;
    }
}

第四步:Spring配置

<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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="myAspect" class="com.zzx.aspect.UserAspect"></bean>
    <bean id="userDao" class="com.zzx.dao.impl.UserDaoImpl"></bean>
    <bean id="userService" class="com.zzx.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="interfaces" value="com.zzx.service.IUserService"/>
        <property name="target" ref="userService"/>
        <property name="interceptorNames" value="myAspect"/>
        <!--配置使用cglib-->
        <property name="optimize" value="true"></property>
    </bean>
</beans>
Spring AOP全自动编程

第一步:导入jar包 aspectjweaver-1.8.13.jar下载及Maven、Gradle引入代码,pom文件及包内class -时代Java (nowjava.com)
第二步: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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.zzx.service.impl.UserServiceImpl"/>
<bean id="myAspect" class="com.zzx.aspect.UserAspect"/>

<aop:config proxy-target-class="true">
    <aop:pointcut id="pointcut" expression="execution(* com.zzx.service.*.*(..))"/>
    <aop:advisor advice-ref="myAspect" pointcut-ref="pointcut"/>
</aop:config>

</beans>

切入点 expression表达式 execution(* 报名..(..)) 第一个“”固定写法,第二个“”表示的是任意类名,第三个“*”表示的是任意方法名称,"(..)"表示的是任意参数。

相关文章

  • Spring框架之AOP概念详解和应用(二)

    AOP概述 在软件业,AOP为Aspect Oriented Programming的缩写,意为面向切面编程,通过...

  • Spring的aop实现

    参考 Spring 使用笔记之(二) - AOP和Interceptor(拦截器)spring AOP概念讲解AO...

  • Spring学习(二)

    spring框架核心技术的两大概念之AOP: AOP(Aspect Oriented Programming)...

  • Spring4-2-AOP配置

    一.AOP概念 二.AOP术语 三.Spring AOP框架AspectJ配置使用(基于注解的方式) (1)必要的...

  • Spring中AOP源码深入解析

    有关AOP相关概念以及Spring AOP相关概念和Spring AOP的使用不再重复。关于AOP在Spring中...

  • Spring框架学习笔记

    Spring框架介绍 Spring框架是轻量级J2EE应用框架,主要部分是IoC和AOP容器框架,主要用于对Ja...

  • Java框架常见面试题-Spring

    1.什么是spring Spring是一个开源的轻量级的IOC和AOP容器框架。Spring框架的核心功能可以应用...

  • Spring学习-概念

    Spring概念 1.Spring 是一个轻量级的 DI / IoC 和 AOP 容器的开源框架 2.Spring...

  • 面试:spring

    Spring 的核心 Spring 框架主要负责技术整合,该框架提供 IOC 和 AOP 机制,可以降低系统组件之...

  • spring

    spring简介 spring是J2EE应用程序框架,是轻量级的IoC和AOP的容器框架,主要是针对javaBea...

网友评论

      本文标题:Spring框架之AOP概念详解和应用(二)

      本文链接:https://www.haomeiwen.com/subject/bttobrtx.html