网站建设与维护考试题,软件外包公司为什么不好,廊坊网站建设推广经验,沧州网站建设代理价格SpringBoot、Java AOP实现方式
搭建项目环境
我这里直接使用Maven创建项目之后再pom.xml中导入包 Spring版本
如果你的版本有最新的#xff0c;最简单的办法就是#xff0c;将版本都换成统一的#xff0c;因为发布时候都是每个版本统一发布的#xff0c;如果出现不兼容的…SpringBoot、Java AOP实现方式
搭建项目环境
我这里直接使用Maven创建项目之后再pom.xml中导入包 Spring版本
如果你的版本有最新的最简单的办法就是将版本都换成统一的因为发布时候都是每个版本统一发布的如果出现不兼容的情况最简单有效的办法就是将版本换成统一的。
导入的有
spring-contextspring-aopspring-aspectsspring-testjunit
dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion6.1.1/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-aop/artifactIdversion6.1.1/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion6.1.1/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion6.1.1/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/version/dependency
/dependencies新建Java文件Calculator接口
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);
}新建实现类CalculatorImpl
记得再上面加上Component否则SpringBoot扫描不到
package org.example.calc.impl;import org.example.calc.Calculator;
import org.springframework.stereotype.Component;Component
public class CalculatorImpl implements Calculator {Overridepublic int add(int i, int j) {return 0;}Overridepublic int sub(int i, int j) {return 0;}Overridepublic int mul(int i, int j) {return 0;}Overridepublic int div(int i, int j) {return 0;}
}新建LogAspect切面
待会就在这个文件中写入切面方法其中有这几个方法
环绕通知上面四个都会包含在内
Before 前置通知
After 后置通知
AfterReturning 返回通知
AfterThrowing 异常通知
Around 环绕通知新建文件是这样的
package org.example.calc;import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;Aspect
Component
public class LogAspect {}一定要在这个文件夹下新建否则不会生效 新建xml文件
文件内容是这样的还没有写入内容这个文件名随便取一个我就叫bean.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd/beans创建切面
xml内容
在你resource下创建的xml文件中添加下面代码其中context:component-scan base-package指向的是目录下Calculator接口/。
这一步至关重要否则后面找xml文件中包时如果找不到没有效果。
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdcontext:component-scan base-packageorg.example.calc/aop:aspectj-autoproxy/
/beansLogAspect实现
Before简单的实现前置操作
第一步先不搞复杂的先试试效果
在文件中写入
其中Before(execution(表达式))
Aspect
Component
public class LogAspect {Before(value execution(public int org.example.calc.Calculator.*(..)))public void beforeMethod() {System.out.println(前置操作);}
}之后在测试文件中写下测试代码
import org.example.calc.Calculator;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class AopTest {Testpublic void test() {ApplicationContext contextnew ClassPathXmlApplicationContext(bean.xml);Calculator contextBean context.getBean(Calculator.class);contextBean.add(1, 2);}
}运行成功 Before 带参数前置操作
要在方法中加入JoinPoint
Aspect
Component
public class LogAspect {Before(value execution(public int org.example.calc.Calculator.*(..)))public void beforeMethod(JoinPoint joinPoint) {String name joinPoint.getSignature().getName();// 获取连接点的签名信息Object[] args joinPoint.getArgs();// 实参信息System.out.println(前置通知--name name args Arrays.toString(args));}
}运行成功 复用value中表达式
因为有时一个项目中会有很多相同的表达式这是需要复用可以做以下操作
只需要在这个文件中加入这个方法即可方法名字可以随便取
需要注意的是这个里面不要填写value
调用时要加括号不要只写方法名
如After(value pointcut())
Pointcut(execution(public int org.example.calc.Calculator.*(..)))
public void pointcut() {
}After 后置通知
package org.example.calc;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;import java.util.Arrays;Aspect
Component
public class LogAspect {/*** 前置通知** param joinPoint JoinPoint*/Before(value execution(public int org.example.calc.Calculator.*(..)))public void beforeMethod(JoinPoint joinPoint) {String name joinPoint.getSignature().getName();// 获取连接点的签名信息Object[] args joinPoint.getArgs();// 实参信息System.out.println(前置通知--name name args Arrays.toString(args));}/*** 后置通知** param joinPoint JoinPoint*/After(value pointcut())public void afterMethod(JoinPoint joinPoint) {String name joinPoint.getSignature().getName();Object[] args joinPoint.getArgs();System.out.println(Logger--后置通知方法名称 name 参数 Arrays.toString(args));}Pointcut(execution(public int org.example.calc.Calculator.*(..)))public void pointcut() {}
}AfterReturning 返回通知引入外部表达式
如果这个表达式是外部的如何引入
直接在前面加上方法名即可
AfterReturning(value org.example.calc.LogAspect.pointcut())返回通知写法
/*** 返回通知** param joinPoint JoinPoint*/
AfterReturning(value org.example.calc.LogAspect.pointcut())
public void afterReturningMethod(JoinPoint joinPoint) {String name joinPoint.getSignature().getName();Object[] args joinPoint.getArgs();System.out.println(Logger--返回通知方法名称 name 参数 Arrays.toString(args));
}AfterThrowing 异常通知
/*** 异常通知* param joinPoint JoinPoint*/
AfterThrowing(value pointcut())
public void afterThrowingMethod(JoinPoint joinPoint) {String name joinPoint.getSignature().getName();Object[] args joinPoint.getArgs();System.out.println(Logger--异常通知方法名称 name 参数 Arrays.toString(args));
}Around 环绕通知
注意的是这次在方法中使用的是ProceedingJoinPoint
Around(value pointcut())
public Object aroundMethod(ProceedingJoinPoint joinPoint) {String name joinPoint.getSignature().getName();Object[] args joinPoint.getArgs();Object result null;try {System.out.println(环绕通知--目标对象方法执行之前);result joinPoint.proceed();System.out.println(环绕通知--目标对象方法返回值之后} catch (Throwable e) {e.printStackTrace();System.out.println(环绕通知--目标对象方法出现异常时);}finally {System.out.println(环绕通知--目标对象方法执行完毕);}return result;
}测试通过 在xml中配置切面
删除注解
因为在配置文件的xml中写入所以要将5个注解全部删除之后创建新的xml文件你用旧的也可以。
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;import java.util.Arrays;Aspect
Component
public class LogAspect {public void beforeMethod(JoinPoint joinPoint) {String name joinPoint.getSignature().getName();// 获取连接点的签名信息Object[] args joinPoint.getArgs();// 实参信息System.out.println(前置通知--name name args Arrays.toString(args));}public void afterMethod(JoinPoint joinPoint) {String name joinPoint.getSignature().getName();Object[] args joinPoint.getArgs();System.out.println(Logger--后置通知方法名称 name 参数 Arrays.toString(args));}public void afterReturningMethod(JoinPoint joinPoint) {String name joinPoint.getSignature().getName();Object[] args joinPoint.getArgs();System.out.println(Logger--返回通知方法名称 name 参数 Arrays.toString(args));}public void afterThrowingMethod(JoinPoint joinPoint) {String name joinPoint.getSignature().getName();Object[] args joinPoint.getArgs();System.out.println(Logger--异常通知方法名称 name 参数 Arrays.toString(args));}public Object aroundMethod(ProceedingJoinPoint joinPoint) {String name joinPoint.getSignature().getName();Object[] args joinPoint.getArgs();Object result null;try {System.out.println(环绕通知--目标对象方法执行之前);result joinPoint.proceed();System.out.println(环绕通知--目标对象方法返回值之后);} catch (Throwable e) {e.printStackTrace();System.out.println(环绕通知--目标对象方法出现异常时);}finally {System.out.println(环绕通知--目标对象方法执行完毕);}return result;}
}xml文件中内容
新的基础内容如下
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdcontext:component-scan base-packageorg.example.calc/aop:config!-- 配置切面类 --aop:aspect reflogAspect!-- 表达式 --aop:pointcut idpointcut expressionexecution(public int org.example.calc.Calculator.*(..))/!-- 前置操作 --aop:before methodbeforeMethod pointcut-refpointcut/!-- 后置操作 --aop:after methodafterMethod pointcut-refpointcut/!-- 返回操作 --aop:after-returning methodafterReturningMethod returningresult pointcut-refpointcut/!-- 异常通知 --aop:after-throwing methodafterThrowingMethod throwingex pointcut-refpointcut/aop:around methodaroundMethod pointcut-refpointcut//aop:aspect/aop:config
/beans运行成功 xml文件解释
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdcontext:component-scan base-packageorg.example.calc/aop:config!-- 配置切面类 --aop:aspect ref你的类开头字母小写!-- 表达式 --aop:pointcut id这个名字随便取 expression表达式的值/!-- 前置操作 --aop:before method前置操作方法正常会有提示的 pointcut-ref注意这里是pointcut-ref是上面填写表达式的值不要加括号/!-- 后置操作 --aop:after method后置操作方法 pointcut-ref注意这里是pointcut-ref是上面填写表达式的值不要加括号/!-- 返回操作 --aop:after-returning method返回操作方法 returning返回值你的返回值是什么变量就填什么 pointcut-ref注意这里是pointcut-ref是上面填写表达式的值不要加括号/!-- 异常通知 --aop:after-throwing method异常通知方法 throwing抛出异常命名见下面图片 pointcut-ref注意这里是pointcut-ref是上面填写表达式的值不要加括号/!-- 环绕通知 --aop:around methodaroundMethod pointcut-ref注意这里是pointcut-ref是上面填写表达式的值不要加括号//aop:aspect/aop:config
/beans抛出异常名根据这个来的
不要加括号/ !-- 返回操作 --aop:after-returning method返回操作方法 returning返回值你的返回值是什么变量就填什么 pointcut-ref注意这里是pointcut-ref是上面填写表达式的值不要加括号/!-- 异常通知 --aop:after-throwing method异常通知方法 throwing抛出异常命名见下面图片 pointcut-ref注意这里是pointcut-ref是上面填写表达式的值不要加括号/!-- 环绕通知 --aop:around methodaroundMethod pointcut-ref注意这里是pointcut-ref是上面填写表达式的值不要加括号//aop:aspect
/aop:config抛出异常名根据这个来的