Spring(9)Aop

9.AOP

9.1 什么是AOP?

   Spring AOP 是 Spring 框架的核心模块之一,它使用纯 Java 实现,因此不需要专门的编译过程和类加载器,可以在程序运行期通过代理方式向目标类织入增强代码。

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

  

9.2 Aop在Spring中的作用 

  提供声明式事务;允许用户自定义切面

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等...
  • 切面:横切关注点被模块化的特殊对象。即,它是一个类。
  • 通知:切面必须要完成的工作。即,它是类中的一个方法。
  • 目标:被通知对象
  • 代理:向目标对象应用通知之后创建的对象
  • 切入点:切面通知执行的“地点”的定义。

  

AOP 联盟为通知(Advice)定义了一个 org.aopalliance.aop.Interface.Advice 接口。

Spring AOP 按照通知(Advice)织入到目标类方法的连接点位置,为 Advice 接口提供了 6 个子接口,如下表。

通知类型接口描述
前置通知  org.springframework.aop.MethodBeforeAdvice 在目标方法执行前实施增强。
后置通知 org.springframework.aop.AfterReturningAdvice 在目标方法执行后实施增强。
后置返回通知 org.springframework.aop.AfterReturningAdvice 在目标方法执行完成,并返回一个返回值后实施增强。
环绕通知 org.aopalliance.intercept.MethodInterceptor 在目标方法执行前后实施增强。
异常通知 org.springframework.aop.ThrowsAdvice 在方法抛出异常后实施增强。
引入通知 org.springframework.aop.IntroductionInterceptor 在目标类中添加一些新的方法和属性。

9.3 使用Spring实现Aop

  使用之前需要导入一个包:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>

9.4代码演示

  1.搭建项目环境

  

   2.service层

//用户业务层处理一些业务
public interface UserService {

    //增加用户
    public void addUser();
    //删除用户
    public void deleteUser();
    //修改用户
    public void updateUser();
    //查找用户
    public void queryUser();
}
public class UserServiceImpl implements UserService {

    public void addUser() {
        System.out.println("增加一个用户");
    }

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

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

    public void queryUser() {
        System.out.println("查找一个用户");
    }
}

  2.log层 

public class Log implements MethodBeforeAdvice {

    //method:方法
    //args:参数
    //target:目标对象
    public void before(Method method, Object[] args, Object target) throws Throwable {

        System.out.println(target.getClass().getName()+"的"+method.getName()+"方法实现了");
    }
}
import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {

    //returnValue
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {

        System.out.println("执行了"+method.getName()+"方法,结果为:"+returnValue);
    }
}

  3.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-3.0.xsd
        http://www.springframework.org/schema/context
        http://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="afterLog" class="com.luo.log.AfterLog"></bean>
    <bean id="log" class="com.luo.log.Log"></bean>
    <bean id="userService" class="com.luo.service.UserServiceImpl"></bean>
    <!-- 方式一:使用原生Spring API接口-->
    <aop:config>
        <!-- 切入点 expression:表达式,execution(要执行的位置!* * * * *) -->
        <aop:pointcut id="pointcut" expression="execution(* com.luo.service.UserServiceImpl.*(..))"/>
        <!--执行增加环绕 -->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"></aop:advisor>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>
</beans>

其中,id 用于指定切入点的唯一标识名称,execution 用于指定切入点关联的切入点表达式。
  execution 的语法格式格式为:

  execution([权限修饰符] [返回值类型] [类的完全限定名] [方法名称]([参数列表]) 

 4.测试

public class Test {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        UserService userService = (UserService) context.getBean("userService");
        userService.addUser();
    }
}

   以上是方法一:使用原生Spring API接口

 

  下面是方法二:自定义来实现AOP【主要是切面自定义】

  自己建好包:diy层

public class DiyPointCut {

    public void before(){
        System.out.println("============方法执行前============");
    }

    public void after(){
        System.out.println("============方法执行后=============");
    }
}
<!-- 自定义类-->
    <aop:config>
        <!--自定义切面,ref要引用的类-->
        <aop:aspect ref="diyPointCut" >
            <!-- 切入点-->
            <aop:pointcut id="pointcut" expression="execution(* com.luo.service.UserServiceImpl.*(..))"/>
            <!-- 通知-->
            <aop:before method="before" pointcut-ref="pointcut"></aop:before>
            <aop:after method="after" pointcut-ref="pointcut"></aop:after>
        </aop:aspect>
    </aop:config>

   以下是方法三,基于注解的实现

  在 Spring 中,虽然我们可以使用 XML 配置文件可以实现 AOP 开发,但如果所有的配置都集中在 XML 配置文件中,就势必会造成 XML 配置文件过于臃肿,从而给维护和升级带来一定困难。
  为此,AspectJ 框架为 AOP 开发提供了一套 @AspectJ 注解。它允许我们直接在 Java 类中通过注解的方式对切面(Aspect)、切入点(Pointcut)和增强(Advice)进行定义,Spring 框架可以根据这些注解生成 AOP 代理。
  关于注解的介绍如表 1 所示。

表 1 Annotation 注解介绍
名称说明
@Aspect 用于定义一个切面。
@Pointcut 用于定义一个切入点。
@Before 用于定义前置通知,相当于 BeforeAdvice。
@AfterReturning 用于定义后置通知,相当于 AfterReturningAdvice。
@Around 用于定义环绕通知,相当于 MethodInterceptor。
@AfterThrowing 用于定义抛出通知,相当于 ThrowAdvice。
@After 用于定义最终通知,不管是否异常,该通知都会执行。
@DeclareParents 用于定义引介通知,相当于 IntroductionInterceptor(不要求掌握)。

@Aspect
public class AnnotationPointCut {

    @Before("execution(* com.luo.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("===========方法执行前=============");
    }

    @After("execution(* com.luo.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("===========方法执行前=============");
    }
}

  beans.xml

    <!-- 方式三 -->
    <bean id="annotationPointCut" class="com.luo.diy.AnnotationPointCut"></bean>
    <!-- 开启注解支持! -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

 

posted @ 2022-05-19 13:14  十三加油哦  阅读(36)  评论(0编辑  收藏  举报