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

SpringAOP学习

面向切面编程,指导开发者如何组织程序结构

增强原始设计的功能

oop:面向对象编程

1.导入aop相关坐标,创建

<!--spring依赖--><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.10.RELEASE</version></dependency>
<!--spring-text依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.2.10.RELEASE</version></dependency>
 <!-- 导入切入点表达式依赖--><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.4</version></dependency>
 <!-- 导入junit依赖 测试单元--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency>

2.创建配置类进行Spring注解包扫描

package com.heima.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//写入注解是spring的配置类
@Configuration
//扫描包
@ComponentScan("com.heima")
public class SpringConfig {}

3.定义业务接口与实现类

jisaunqi

package com.heima.service;public interface JiSuanQi {
//    加法int add(int a, int b);//    除法int div(int a, int b);}

jisuanqiimpl 

package com.heima.service.impl;import com.heima.service.JiSuanQi;public class JiSuanQiImpl implements JiSuanQi {@Overridepublic int add(int a, int b) {int r = a + b;return r;}@Overridepublic int div(int a, int b) {int r = a / b;return r;}
}

生成

 测试库

package com.heima.service.impl;import com.heima.config.SpringConfig;
import com.heima.service.JiSuanQi;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;//junit测试单元
//启动整合
@RunWith(SpringJUnit4ClassRunner.class)
//指定启动类
@ContextConfiguration(classes={SpringConfig.class})
public class JiSuanQiImplTest extends TestCase {@Autowiredprivate JiSuanQi jiSuanQi;@Testpublic void testAdd() {System.out.println(jiSuanQi.add(1, 2));}@Testpublic void testDiv() {System.out.println(jiSuanQi.div(4, 2));}
}

 运行测试

4.定义通知,制作通知方法

5.定义切入点表达式,配置切面

建立软件包

建立类

 

类中要写入注解被扫描,

通知方法有4种

1.前置通知

  //1.前置通知@Before("pt()")public void before(JoinPoint joinPoint) {System.out.println("参数为:"+ Arrays.toString(joinPoint.getArgs()));System.out.println("我要开始计算了...");}

2.后置通知

  //2.后置通知@After("pt()")public void after() {System.out.println("我结束计算了");}

3.返回通知

//returning="返回结果对象"@AfterReturning(value = "pt()",returning = "r")public void afterReturning( Object r) {System.out.println("结果为:"+r);}

4.异常通知

 //4.异常通知  捕获异常触发@AfterThrowing(value = "pt()",throwing = "e")public void afterThrowing(Throwable e) {System.out.println("计算出现异常"+e.getMessage());}

要在配置开启AOP功能

回到测试类里运行测试

 MyAdivice

package com.heima.advice;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;import java.sql.Array;
import java.util.Arrays;//通知类,写入注解
@Aspect
//被Spring容器扫描到,由于spring容器已写入扫描器
@Component
public class MyAdivice {//切点表达式 你想在哪个方法上切入//int是参数类型//com.heima.service.impl.JiSuanQiImpl.add:找到add方法//(int,int)两个参数 参数类型为int@Pointcut("execution(int com.heima.service.impl.JiSuanQiImpl.add(int,int))")public void pt() {}//通知方法//1.前置通知@Before("pt()")public void before(JoinPoint joinPoint) {System.out.println("参数为:"+ Arrays.toString(joinPoint.getArgs()));System.out.println("我要开始计算了...");}//2.后置通知@After("pt()")public void after() {System.out.println("我结束计算了");}//3.返回通知  获取返回结果后触发//returning="返回结果对象"@AfterReturning(value = "pt()",returning = "r")public void afterReturning( Object r) {System.out.println("结果为:"+r);}//4.异常通知  捕获异常触发@AfterThrowing(value = "pt()",throwing = "e")public void afterThrowing(Throwable e) {System.out.println("计算出现异常"+e.getMessage());}}

6.在配置类中开启aop功能

package com.heima.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;//写入注解是spring的配置类
@Configuration//开启AOP功能
@EnableAspectJAutoProxy
//扫描包
@ComponentScan("com.heima")
public class SpringConfig {}

简化

    //@Pointcut("execution(int com.heima.service.impl.JiSuanQiImpl.add(int,int))")简化//@Pointcut("execution(int com.heima.service.impl.JiSuanQiImpl.add(*,*))")返回类型可以是多个//@Pointcut("execution(int com.heima.service.impl.JiSuanQiImpl.add(..))")返回类型可以是多个//@Pointcut("execution(int com.heima.service.impl.JiSuanQiImpl.*(..))")这里*代表包里面所有方法都可以运行//@Pointcut("execution(* com..impl.*.*(..))")代表意思为包的类型com包下面的所有包和方法//@Pointcut("execution(* com..*.*(..))")简化结果

http://www.lryc.cn/news/450168.html

相关文章:

  • 智能网联汽车飞速发展,安全危机竟如影随形,如何破局?
  • Android常用C++特性之std::function
  • 人工智能与机器学习原理精解【27】
  • XXL-JOB在SpringBoot中的集成
  • 前端工程规范-3:CSS规范(Stylelint)
  • Qt系列-1.Qt安装
  • 《自控原理》最小相位系统
  • SpringBoot3脚手架
  • 【C语言软开面经】
  • YOLOv11训练自己的数据集(从代码下载到实例测试)
  • HTML粉色烟花秀
  • 从零开发操作系统
  • SigmaStudio中部分滤波器算法有效性频谱分析
  • ArcGIS与ArcGIS Pro去除在线地图服务名单
  • 滚雪球学MySQL[10.1讲]:常见问题与解决
  • 利用 Llama-3.1-Nemotron-51B 推进精度-效率前沿的发展
  • SpringBoot+Thymeleaf发票系统
  • Updates were rejected because the tip of your current branch is behind 的解决方法
  • Redis桌面工具:Tiny RDM
  • 【Java】酒店管理系统
  • 【数据库】Java 中 MongoDB 使用指南:步骤与方法介绍
  • MySQL之内置函数
  • JVM 基本组成
  • Ubuntu 离线安装 docker
  • 【C++】set详解
  • C++游戏开发:构建高性能、沉浸式游戏体验的关键
  • 【STM32开发笔记】移植AI框架TensorFlow到STM32单片机【上篇】
  • 第三方供应商不提供API接口?教你四步破解集成难题
  • WebAssembly 为什么能提升性能,怎么使用它 ?
  • golang学习笔记13-函数(二):init函数,匿名函数,闭包,defer