当前位置: 首页 > news >正文

Aspect的AOP实现

        Spring的Aop实现的常见形式是通过两种动态代理的形式,除此之外还有另一种实现,它可以通过改写目标类的class文件来实现AOP增强。

1.引入Aspectj编译器的依赖

 <!-- AspectJ Maven Plugin --><plugin><groupId>org.codehaus.mojo</groupId><artifactId>aspectj-maven-plugin</artifactId><version>1.14.0</version> <!-- 请使用最新版本 --><configuration><!-- 指定使用哪个版本的AspectJ工具链 --><aspectjVersion>1.9.19</aspectjVersion><!-- 指定Java版本,与你的项目一致 --><source>11</source><target>11</target><!-- 启用Xlint警告,帮助调试切面 --><Xlint>warning</Xlint><!-- 这个配置很重要,确保插件的行为符合预期 --><complianceLevel>11</complianceLevel><encoding>UTF-8</encoding></configuration><executions><execution><goals><!-- 目标:在编译主代码时进行织入 --><goal>compile</goal><!-- 目标:在编译测试代码时进行织入 --><goal>test-compile</goal></goals></execution></executions></plugin>

2.Aspect切面类

package com.example.springdemo.demos.a28.aop;import com.example.springdemo.demos.a27.a27Application;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** @author zhou* @version 1.0* @description TODO* @date 2025/10/18 20:04*/
@Aspect
public class MyAspect {private static final Logger log = LoggerFactory.getLogger(MyAspect.class);@Before("execution(* com.example.springdemo.demos.a28.service.MyService.test())")public void before(){log.info("before()");}}

3.MyService

package com.example.springdemo.demos.a28.service;import com.example.springdemo.demos.a27.a27Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;/*** @author zhou* @version 1.0* @description TODO* @date 2025/10/18 20:08*/
@Service
public class MyService {private static final Logger log = LoggerFactory.getLogger(MyService.class);public void test(){log.info("test");}
}

4.测试主类

package com.example.springdemo.demos.a28;import com.example.springdemo.demos.a27.a27Application;
import com.example.springdemo.demos.a28.service.MyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;/*** @author zhou* @version 1.0* @description TODO* @date 2025/10/18 20:08*/
@SpringBootApplication
public class a28Application {private static final Logger log = LoggerFactory.getLogger(a28Application.class);public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(a28Application.class, args);MyService service = context.getBean(MyService.class);log.info("service class:{}",service.getClass());service.test();context.close();}
}

5.结果

        在Service的test方法前实现了before增强。查看Service的class文件可以知道,它是通过改写Service的class文件并进行了方法增强。

6.直接new对象的方式调用

        由于这种AOP的实现形式是不依赖于Spring容器的,所以可以直接创建对象并调用方法也能使用AOP增强,它完全依赖于Aspectj编译器。

package com.example.springdemo.demos.a28;import com.example.springdemo.demos.a27.a27Application;
import com.example.springdemo.demos.a28.service.MyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;/*** @author zhou* @version 1.0* @description TODO* @date 2025/10/18 20:08*/
@SpringBootApplication
public class a28Application {private static final Logger log = LoggerFactory.getLogger(a28Application.class);public static void main(String[] args) {/*ConfigurableApplicationContext context = SpringApplication.run(a28Application.class, args);MyService service = context.getBean(MyService.class);log.info("service class:{}",service.getClass());service.test();context.close();*/new MyService().test();}
}

http://www.dtcms.com/a/499736.html

相关文章:

  • Orleans Stream SubscriptionId 生成机制详解
  • FMIT,一款专业的乐器调音助手
  • 医疗器械招商网站大全建一个信息 类网站
  • 不用域名推广网站开源网站后台管理系统
  • 欧司朗与日亚签署广泛的知识产权协议
  • Kotlin 与 Java 互操作中常用注解
  • 计算机操作系统:程序的装入与链接
  • 怎么建设网站手机网站制作价格在线考试类网站怎么做
  • 3.1 栈
  • 国贸网站建设公司服务专业的网站建站公司
  • 手写观察者模式:原理、实现与应用
  • 商贸有限公司网站案例wordpress模版安装
  • Rclone、rsync、Docker 的 COPY/ADD:路径末尾加不加「/」的含义大不相同!
  • 【复习】计网每日一题1016--可分配
  • 【STM32】hal库 多通道ADC(+DMA)采集 连续模式
  • 网站收录量是什么意思开网站卖茶要怎么做
  • 软著下证又又+1
  • 常见分析方法与对应图表汇总
  • 【图像处理】常见图像插值算法与应用
  • Python爬虫入门:从零到数据采集
  • 免费创建app网站网站推广的基本方法是哪四个
  • 建设网站需要的资金清单建设银行网站会员怎么用
  • 深度学习——YOLO 原理与各版本演化
  • 实验室安全准入考试答案
  • 设计网络网站郑州专业公司网站建设公司
  • 17做网店一样的网站html投票代码
  • 网站建设与维护薪资网页版梦幻西游答题器
  • Form表单._表单元素(本文为个人学习笔记,内容整理自哔哩哔哩UP主【非学者勿扰】的公开课程。 > 所有知识点归属原作者,仅作非商业用途分享)
  • 合肥企业做网站wap游戏中心
  • React 02