使用 Spring 实现 AOP.(代码实例)-- 使用 Spring 的 API 接口实现、使用自定义类实现、使用注解方式实现

使用三种方式实现AOP:

  • 使用 Spring 的 API 接口实现
  • 使用自定义类实现
  • 使用注解方式实现

一. AOP 简介

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

二. AOP操作术语

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

三. 代码实例

  • 首先定义 UserService 接口
public interface UserService {
    
    

    public void add();
    public void delete();
    public void update();
    public void search();
}

  • 定义 UserServiceImpl 实现类
public class UserServiceImpl implements UserService{
    
    

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

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

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

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

3.1 使用 Spring 的 API 接口实现

  • 定义 applicationContext.xml
    <!--注册bean-->
    <bean id="userService" class="com.service.UserServiceImpl"/>
    <bean id="log" class="com.log.Log"/>
    <bean id="afterLog" class="com.log.AfterLog"/>
    
    <!-- 第一种方法  使用Spring的API接口 -->
    <!--aop的配置-->
    <aop:config>
    <!--切入点  expression:表达式匹配要执行的方法-->
        <aop:pointcut id="pointcut" expression="execution(* com.service.UserServiceImpl.*(..))"/>
        <!--执行环绕; advice-ref执行方法 . pointcut-ref切入点-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>
  • 实现 MyTest 测试类
public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }

}

3.2 使用自定义类实现

  • 定义 DiyPointcut 自定义类
public class DiyPointcut {
    
    

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

    public void after(){
    
    
        System.out.println("方法执行后......");
    }
}
  • 定义 applicationContext.xml
    <bean id="diy" class="com.diy.DiyPointcut"/>
     第二种方法 使用自定义方法 
    <aop:config>
        <!-- 切面 -->
        <aop:aspect ref="diy">
            <!-- 切点 -->
            <aop:pointcut id="pointcut" expression="execution(* com.service.UserServiceImpl.*(..))"/>
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
  • 测试类 MyTest
public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }

}

3.3. 使用注解方式实现

  • 开启注解说明
<?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:util="http://www.springframework.org/schema/util"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 指定要扫描的包 -->
    <context:component-scan base-package="com.diy"/>

    <!-- 开启注解支持 -->
    <context:annotation-config/>
  • 定义 AnnotationPointcut 类
// 切面
@Component(value = "annotationPointcut")
@Aspect
public class AnnotationPointcut {
    
    

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

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

    @Around("execution(* com.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
    
    
        System.out.println("环绕前。。。。。");

        // 执行目标方法
        Object proceed = jp.proceed();

        System.out.println("环绕后。。。。。");
    }
}
  • applicationContext.xml 配置文件
    <!-- 第三种方法 注解方式-->
    <!-- 开启注解支持 -->
    <aop:aspectj-autoproxy/>
  • 测试类 MyTest
public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }

}

四. 补充说明

Aop的重要性 : 很重要 . 一定要理解其中的思路 , 主要是思想的理解这一块 .

Spring的Aop就是将公共的业务 (日志 , 安全等) 和领域业务结合起来 , 当执行领域业务时 , 将会把公共业务加进来 . 实现公共业务的重复利用 . 领域业务更纯粹 , 程序猿专注领域业务 , 其本质还是动态代理 .

猜你喜欢

转载自blog.csdn.net/Kc635908933/article/details/114272544
今日推荐