IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Spring-AOP -> 正文阅读

[Java知识库]Spring-AOP

一、引入maven库如下:?

  • AOP(Aspect Oriented Programming),面向切面编程
  • Spring使用AOP技术封装了动态代理的功能,使用起来非常简单
  • 它依赖于AspectJ库如下:
        <!--Spring AOP-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.6</version>
        </dependency>

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

二、创建aop相关类如下:

1、在业务方法之前调用,无法配置:

package com.mj.aop;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class LogAdvice implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("LogAdvice---before--------");
    }
}

2、可以进行手动配置如下:

package com.mj.aop;

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

public class LogInterceptor implements MethodInterceptor {
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("LogInterceptor ----- 1");
        /*调用目标对象的目标方法*/
        Object result = methodInvocation.proceed();
        System.out.println("LogInterceptor ----- 2");
        return result;
    }
}

?三、配置文件applicationContext.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: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/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">


    <bean id="userService" class="com.mj.service.impl.UserServiceImpl"/>
    <bean id="personService" class="com.mj.service.impl.PersonServiceImpl"/>
    <bean id="skillService" class="com.mj.service.SkillService"/>

    <!--附加代码-->
<!--    <bean id="logAdvice" class="com.mj.aop.LogAdvice"/>-->
    <bean id="interceptor" class="com.mj.aop.LogInterceptor"/>
    <!--aop切面-->
    <!--true代表强制使用CGLib;false代表使用默认方案-->
    <aop:config proxy-target-class="true">
        <!--切入点:给哪些类的哪些方法增加附加代码-->
        <!--execution(* *(..)) 代表所有类的所有方法都会被切入-->
        <aop:pointcut id="pc" expression="execution(* *(..))"/>
        <!--附加代码-->
        <aop:advisor advice-ref="interceptor" pointcut-ref="pc"/>
    </aop:config>

</beans>

切入点表达式:

  • 任意公共方法:execution(public * *(..))?
  • 名字以set开头的任意方法:execution(* set*(..))
  • UserService接口定义的任意方法:execution(* com.mj.service.UserService.*(..))
  • service包中定义的任意方法:execution(* com.mj.service.*.*(..))
  • service包中定义的任意方法(包括子包):execution(* com.mj.service..*.*(..))
  • 包含2个String参赛的任意方法:execution(* *(String, String))
  • 带有自定义注解(Hehe)的方法:@annotation(com.mj.aop.Hehe)

能配置多个pointcut、advisor如下图:

?

四、测试类

public class UserTest {

    @Test
    public void test(){
        // 创建容器
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService service = ctx.getBean("userService", UserService.class);
        service.login("小马哥","123456");

        PersonService personService = ctx.getBean("personService",PersonService.class);
        personService.run();

        SkillService service1 = ctx.getBean("skillService", SkillService.class);
        service1.save(null);

        // 关闭容器
        ctx.close();
    }

}

五、AOP动态代理的底层实现

  • 如果目标类有实现接口,使用JDK实现
  • 如果目标类没有实现接口,使用CGLib实现
  • 可以通过proxy-target-class属性修改底层实现方案:true代表强制使用CGLib;false代表使用默认方案
  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-10-08 20:27:04  更:2022-10-08 20:29:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年3日历 -2025/3/10 15:30:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码