面向切面编程:
基于Spring的AOP(注解方式)
1-配置:pom文件:
<packaging > jar</packaging >
<dependencies >
<dependency >
<groupId > org.springframework</groupId >
<artifactId > spring-context</artifactId >
<version > 5.3.1</version >
</dependency >
<dependency >
<groupId > junit</groupId >
<artifactId > junit</artifactId >
<version > 4.12</version >
<scope > test</scope >
</dependency >
<dependency >
<groupId > org.springframework</groupId >
<artifactId > spring-aspects</artifactId >
<version > 5.3.1</version >
</dependency >
</dependencies >
2-规定包结构
因为我们使用的是jdk的aop,所以必须需要 "接口":
细节:
"接口、实现类、切面类" ---- > "必须被spring管理" (所以需要spring包扫描)
<?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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd" >
<context:component-scan base-package ="com.atguigu.spring.aop.annotation" />
</beans >
public interface Calculator {
int add (int i, int j ) ;
int sub (int i, int j ) ;
int mul (int i, int j ) ;
int div (int i, int j ) ;
}
@Component
public class CalculatorPureImpl implements Calculator {
@Override
public int add (int i, int j) {
int result = i + j;
return result;
}
@Override
public int sub (int i, int j) {
int result = i - j;
return result;
}
@Override
public int mul (int i, int j) {
int result = i * j;
return result;
}
@Override
public int div (int i, int j) {
int result = i / j;
return result;
}
}
package com.atguigu.spring.aop.annotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Aspect
@Component
@ComponentScan("com.atguigu.spring.aop.annotation")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class LogAspect {
@Pointcut("execution( * com.atguigu.spring.aop.annotation.CalculatorPureImpl.* (..))")
public void pointCut () {}
@Before("pointCut()")
public void beforeMethod (JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
System.out.println("Logger-->前置通知,方法名:" + methodName + ",参 数:" + args);
}
@After("pointCut()")
public void afterMethod (JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("Logger-->后置通知,方法名:" + methodName +" 执行完毕····" );
}
@AfterReturning(value = "pointCut()",returning = "result")
public void afterRunningMethod (JoinPoint joinPoint , Object result) {
String methodName = joinPoint.getSignature().getName();
System.out.println("Logger-->中间通知,方法名:" + methodName +" 方法的返回值" +result);
}
@AfterThrowing(value = "pointCut()",throwing = "ex")
public void affterThrowingMethod (JoinPoint joinPoint , Exception ex) {
String methodName = joinPoint.getSignature().getName();
System.out.println("Logger-->异常通知,方法名:" + methodName +" 方法存在异常: " +ex);
}
}
3-测试:
细节:
被spring配置成代理类,就不可以使用"目标对象" ,只能使用他的"代理类和父类" ,
所以这个地方不可以使用CalculatorPureImpl这个目标对象,只能使用它的"父类" -Calculator获取代理对象
package com.atguigu.spring.testes;
import com.atguigu.spring.aop.annotation.Calculator;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTestes {
@Test
public void getAnnotationAop () {
ApplicationContext ioc = new ClassPathXmlApplicationContext ("aop-annotation.xml" );
Calculator calculatorBean = ioc.getBean(Calculator.class);
calculatorBean.add(1 ,1 );
calculatorBean.div(1 ,0 );
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)