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

青岛网站建设外贸茂名建设局网站

青岛网站建设外贸,茂名建设局网站,邯郸现代建设集团网站,vue 做企业网站行不本章概要 Spring AOP底层技术组成初步实现获取通知细节信息切点表达式语法重用#xff08;提取#xff09;切点表达式环绕通知切面优先级设置CGLib动态代理生效注解实现小结 5.5.1 Spring AOP 底层技术组成 动态代理#xff08;InvocationHandler#xff09;#xff1a;…本章概要 Spring AOP底层技术组成初步实现获取通知细节信息切点表达式语法重用提取切点表达式环绕通知切面优先级设置CGLib动态代理生效注解实现小结 5.5.1 Spring AOP 底层技术组成 动态代理InvocationHandlerJDK原生的实现方式需要被代理的目标类必须实现接口。因为这个技术要求代理对象和目标对象实现同样的接口兄弟两个拜把子模式。cglib通过继承被代理的目标类认干爹模式实现代理所以不需要目标类实现接口。AspectJ早期的AOP实现的框架SpringAOP借用了AspectJ中的AOP注解。 5.5.2 初步实现 加入依赖 !-- spring-aspects会帮我们传递过来aspectjweaver -- dependencygroupIdorg.springframework/groupIdartifactIdspring-aop/artifactIdversion6.0.6/version /dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion6.0.6/version /dependency准备接口 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);}纯净实现类package com.atguigu.proxy; /*** 实现计算接口,单纯添加 - * / 实现! 掺杂其他功能!*/ Component public class CalculatorPureImpl implements Calculator {Overridepublic int add(int i, int j) {int result i j;return result;}Overridepublic int sub(int i, int j) {int result i - j;return result;}Overridepublic int mul(int i, int j) {int result i * j;return result;}Overridepublic int div(int i, int j) {int result i / j;return result;} }声明切面类 import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;// Aspect表示这个类是一个切面类 Aspect // Component注解保证这个切面类能够放入IOC容器 Component public class LogAspect {// Before注解声明当前方法是前置通知方法// value属性指定切入点表达式由切入点表达式控制当前通知方法要作用在哪一个目标方法上Before(value execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int)))public void printLogBeforeCore() {System.out.println([AOP前置通知] 方法开始了);}AfterReturning(value execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int)))public void printLogAfterSuccess() {System.out.println([AOP返回通知] 方法成功返回了);}AfterThrowing(value execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int)))public void printLogAfterException() {System.out.println([AOP异常通知] 方法抛异常了);}After(value execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int)))public void printLogFinallyEnd() {System.out.println([AOP后置通知] 方法最终结束了);}}开启 aspectj 注解支持 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/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-packagecom.atguigu /!-- 开启aspectj框架注解支持--aop:aspectj-autoproxy / /beans配置类方式 Configuration ComponentScan(basePackages com.atguigu) //作用等于 aop:aspectj-autoproxy / 配置类上开启 Aspectj注解支持! EnableAspectJAutoProxy public class MyConfig { }测试效果 //SpringJUnitConfig(locations classpath:spring-aop.xml) SpringJUnitConfig(value {MyConfig.class}) public class AopTest {Autowiredprivate Calculator calculator;Testpublic void testCalculator(){calculator.add(1,1);} }输出结果 5.5.3 获取通知细节信息 JointPoint 接口 需要获取方法签名、传入的实参等信息时可以在通知方法声明JoinPoint类型的形参。 要点1JoinPoint 接口通过 getSignature() 方法获取目标方法的签名方法声明时的完整信息要点2通过目标方法签名对象获取方法名要点3通过 JoinPoint 对象获取外界调用目标方法时传入的实参列表组成的数组 JointPoint.java public class JointPoint {// Before注解标记前置通知方法// value属性切入点表达式告诉Spring当前通知方法要套用到哪个目标方法上// 在前置通知方法形参位置声明一个JoinPoint类型的参数Spring就会将这个对象传入// 根据JoinPoint对象就可以获取目标方法名称、实际参数列表Before(value execution(public int com.atguigu.aop.api.Calculator.add(int,int)))public void printLogBeforeCore(JoinPoint joinPoint) {// 1.通过JoinPoint对象获取目标方法签名对象// 方法的签名一个方法的全部声明信息Signature signature joinPoint.getSignature();// 2.通过方法的签名对象获取目标方法的详细信息String methodName signature.getName();System.out.println(methodName methodName);int modifiers signature.getModifiers();System.out.println(modifiers modifiers);String declaringTypeName signature.getDeclaringTypeName();System.out.println(declaringTypeName declaringTypeName);// 3.通过JoinPoint对象获取外界调用目标方法时传入的实参列表Object[] args joinPoint.getArgs();// 4.由于数组直接打印看不到具体数据所以转换为List集合ListObject argList Arrays.asList(args);System.out.println([AOP前置通知] methodName 方法开始了参数列表 argList);} }方法返回值 在返回通知中通过 AfterReturning 注解的 returning 属性获取目标方法的返回值 import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;// Aspect表示这个类是一个切面类 Aspect // Component注解保证这个切面类能够放入IOC容器 Component public class LogAspect {// Before注解声明当前方法是前置通知方法// value属性指定切入点表达式由切入点表达式控制当前通知方法要作用在哪一个目标方法上Before(value execution(public int com.atguigu.CalculatorPureImpl.add(int,int)))public void printLogBeforeCore() {System.out.println([AOP前置通知] 方法开始了);}AfterReturning(value execution(public int com.atguigu.CalculatorPureImpl.add(int,int)))public void printLogAfterSuccess() {System.out.println([AOP返回通知] 方法成功返回了);}// AfterReturning注解标记返回通知方法// 在返回通知中获取目标方法返回值分两步// 第一步在AfterReturning注解中通过returning属性设置一个名称// 第二步使用returning属性设置的名称在通知方法中声明一个对应的形参AfterReturning(value execution(public int Calculator.add(int,int)),returning targetMethodReturnValue)public void printLogAfterCoreSuccess(JoinPoint joinPoint, Object targetMethodReturnValue) {String methodName joinPoint.getSignature().getName();System.out.println([AOP返回通知] methodName方法成功结束了返回值是 targetMethodReturnValue);}AfterThrowing(value execution(public int com.atguigu.CalculatorPureImpl.add(int,int)))public void printLogAfterException() {System.out.println([AOP异常通知] 方法抛异常了);}After(value execution(public int com.atguigu.CalculatorPureImpl.add(int,int)))public void printLogFinallyEnd() {System.out.println([AOP后置通知] 方法最终结束了);}}异常对象捕捉 在异常通知中通过AfterThrowing注解的throwing属性获取目标方法抛出的异常对象 package com.atguigu;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;// Aspect表示这个类是一个切面类 Aspect // Component注解保证这个切面类能够放入IOC容器 Component SuppressWarnings(all) public class LogAspect {// Before注解声明当前方法是前置通知方法// value属性指定切入点表达式由切入点表达式控制当前通知方法要作用在哪一个目标方法上Before(value execution(public int com.atguigu.CalculatorPureImpl.add(int,int)))public void printLogBeforeCore() {System.out.println([AOP前置通知] 方法开始了);}AfterReturning(value execution(public int com.atguigu.CalculatorPureImpl.add(int,int)))public void printLogAfterSuccess() {System.out.println([AOP返回通知] 方法成功返回了);}// AfterReturning注解标记返回通知方法// 在返回通知中获取目标方法返回值分两步// 第一步在AfterReturning注解中通过returning属性设置一个名称// 第二步使用returning属性设置的名称在通知方法中声明一个对应的形参AfterReturning(value execution(public int Calculator.add(int,int)),returning targetMethodReturnValue)public void printLogAfterCoreSuccess(JoinPoint joinPoint, Object targetMethodReturnValue) {String methodName joinPoint.getSignature().getName();System.out.println([AOP返回通知] methodName方法成功结束了返回值是 targetMethodReturnValue);}AfterThrowing(value execution(public int com.atguigu.CalculatorPureImpl.add(int,int)))public void printLogAfterException() {System.out.println([AOP异常通知] 方法抛异常了);}// AfterThrowing注解标记异常通知方法// 在异常通知中获取目标方法抛出的异常分两步// 第一步在AfterThrowing注解中声明一个throwing属性设定形参名称// 第二步使用throwing属性指定的名称在通知方法声明形参Spring会将目标方法抛出的异常对象从这里传给我们AfterThrowing(value execution(public int Calculator.add(int,int)),throwing targetMethodException)public void printLogAfterCoreException(JoinPoint joinPoint, Throwable targetMethodException) {String methodName joinPoint.getSignature().getName();System.out.println([AOP异常通知] methodName方法抛异常了异常类型是 targetMethodException.getClass().getName());}After(value execution(public int com.atguigu.CalculatorPureImpl.add(int,int)))public void printLogFinallyEnd() {System.out.println([AOP后置通知] 方法最终结束了);}}5.5.4 切点表达式语法 切点表达式作用 AOP切点表达式Pointcut Expression是一种用于指定切点的语言它可以通过定义匹配规则来选择需要被切入的目标对象。 切点表达式语法 切点表达式总结 语法细节 第一位execution( ) 固定开头第二位方法访问修饰符 public private 直接描述对应修饰符即可第三位方法返回值 int String void 直接描述返回值类型注意 特殊情况 不考虑 访问修饰符和返回值 execution(* * ) 这是错误语法 execution( *) 你只要考虑返回值 或者 不考虑访问修饰符 相当于全部不考虑了 第四位指定包的地址 固定的包: com.atguigu.api | service | dao 单层的任意命名: com.atguigu.* com.atguigu.api com.atguigu.dao * 任意一层的任意命名 任意层任意命名: com.. com.atguigu.api.erdaye com.a.a.a.a.a.a.a ..任意层,任意命名 用在包上! 注意: ..不能用作包开头 public int .. 错误语法 com.. 找到任何包下: *..第五位指定类名称 固定名称: UserService 任意类名: * 部分任意: com..service.impl.*Impl 任意包任意类: *..*第六位指定方法名称 语法和类名一致 任意访问修饰符,任意类的任意方法: * *..*.*第七位方法参数 具体值: (String,int) ! (int,String) 没有参数 () 模糊值: 任意参数 有 或者 没有 (..) ..任意参数的意识 部分具体和模糊:第一个参数是字符串的方法 (String..)最后一个参数是字符串 (..String)字符串开头,int结尾 (String..int)包含int类型(..int..)切点表达式案例 1.查询某包某类下访问修饰符是公有返回值是int的全部方法 2.查询某包下类中第一个参数是String的方法 3.查询全部包下无参数的方法 4.查询com包下以int参数类型结尾的方法 5.查询指定包下Service开头类的私有返回值int的无参数方法5.5.5 重用提取切点表达式 重用切点表达式优点 // Before注解声明当前方法是前置通知方法 // value属性指定切入点表达式由切入点表达式控制当前通知方法要作用在哪一个目标方法上 Before(value execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int))) public void printLogBeforeCore() {System.out.println([AOP前置通知] 方法开始了); }AfterReturning(value execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int))) public void printLogAfterSuccess() {System.out.println([AOP返回通知] 方法成功返回了); }AfterThrowing(value execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int))) public void printLogAfterException() {System.out.println([AOP异常通知] 方法抛异常了); }After(value execution(public int com.atguigu.proxy.CalculatorPureImpl.add(int,int))) public void printLogFinallyEnd() {System.out.println([AOP后置通知] 方法最终结束了); }上面案例是我们之前编写切点表达式的方式发现 所有增强方法的切点表达式相同 出现了冗余如果需要切换也不方便统一维护 我们可以将切点提取在增强上进行引用即可 同一类内部引用 提取 // 切入点表达式重用 Pointcut(execution(public int com.atguigu.aop.api.Calculator.add(int,int)))) public void declarPointCut() {}注意提取切点注解使用Pointcut(切点表达式) 需要添加到一个无参数无返回值方法上即可 引用 Before(value declarPointCut()) public void printLogBeforeCoreOperation(JoinPoint joinPoint) {不同类中引用 不同类在引用切点只需要添加类的全限定符方法名即可 Before(value com.atguigu.spring.aop.aspect.LogAspect.declarPointCut()) public Object roundAdvice(ProceedingJoinPoint joinPoint) {切点统一管理 建议将切点表达式统一存储到一个类中进行集中管理和维护 Component public class AtguiguPointCut {Pointcut(value execution(public int *..Calculator.sub(int,int)))public void atguiguGlobalPointCut(){}Pointcut(value execution(public int *..Calculator.add(int,int)))public void atguiguSecondPointCut(){}Pointcut(value execution(* *..*Service.*(..)))public void transactionPointCut(){} }5.5.6 环绕通知 环绕通知对应整个 try…catch…finally 结构包括前面四种通知的所有功能。 // 使用Around注解标明环绕通知方法 Around(value com.atguigu.aop.aspect.AtguiguPointCut.transactionPointCut()) // 通过在通知方法形参位置声明ProceedingJoinPoint类型的形参 // Spring会将这个类型的对象传给我们 public Object manageTransaction(ProceedingJoinPoint joinPoint) {// 通过ProceedingJoinPoint对象获取外界调用目标方法时传入的实参数组Object[] args joinPoint.getArgs();// 通过ProceedingJoinPoint对象获取目标方法的签名对象Signature signature joinPoint.getSignature();// 通过签名对象获取目标方法的方法名String methodName signature.getName();// 声明变量用来存储目标方法的返回值Object targetMethodReturnValue null;try {// 在目标方法执行前开启事务模拟log.debug([AOP 环绕通知] 开启事务方法名 methodName 参数列表 Arrays.asList(args));// 过ProceedingJoinPoint对象调用目标方法// 目标方法的返回值一定要返回给外界调用者targetMethodReturnValue joinPoint.proceed(args);// 在目标方法成功返回后提交事务模拟log.debug([AOP 环绕通知] 提交事务方法名 methodName 方法返回值 targetMethodReturnValue);} catch (Throwable e) {// 在目标方法抛异常后回滚事务模拟log.debug([AOP 环绕通知] 回滚事务方法名 methodName 异常 e.getClass().getName());} finally {// 在目标方法最终结束后释放数据库连接log.debug([AOP 环绕通知] 释放数据库连接方法名 methodName);}return targetMethodReturnValue; }5.5.7 切面优先级设置 相同目标方法上同时存在多个切面时切面的优先级控制切面的内外嵌套顺序。 优先级高的切面外面优先级低的切面里面 使用 Order 注解可以控制切面的优先级 Order(较小的数)优先级高Order(较大的数)优先级低 实际意义 实际开发时如果有多个切面嵌套的情况要慎重考虑。例如如果事务切面优先级高那么在缓存中命中数据的情况下事务切面的操作都浪费了。 此时应该将缓存切面的优先级提高在事务操作之前先检查缓存中是否存在目标数据。 5.5.8 CGLib 动态代理生效 在目标类没有实现任何接口的情况下Spring会自动使用cglib技术实现代理。为了证明这一点我们做下面的测试 Service public class EmployeeService {public void getEmpList() {System.out.print(方法内部 com.atguigu.aop.imp.EmployeeService.getEmpList);} }测试 Autowired private EmployeeService employeeService;Test public void testNoInterfaceProxy() {employeeService.getEmpList(); }没有接口 有接口 使用总结 如果目标类有接口,选择使用jdk动态代理如果目标类没有接口,选择cglib动态代理如果有接口,接口接值如果没有接口,类进行接值 5.5.9 注解实现小结
文章转载自:
http://www.morning.bktly.cn.gov.cn.bktly.cn
http://www.morning.wdlg.cn.gov.cn.wdlg.cn
http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn
http://www.morning.kztts.cn.gov.cn.kztts.cn
http://www.morning.tzpqc.cn.gov.cn.tzpqc.cn
http://www.morning.yubkwd.cn.gov.cn.yubkwd.cn
http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn
http://www.morning.sjpbh.cn.gov.cn.sjpbh.cn
http://www.morning.mtmph.cn.gov.cn.mtmph.cn
http://www.morning.qxlhj.cn.gov.cn.qxlhj.cn
http://www.morning.yrjym.cn.gov.cn.yrjym.cn
http://www.morning.rhmk.cn.gov.cn.rhmk.cn
http://www.morning.yktwr.cn.gov.cn.yktwr.cn
http://www.morning.ltrz.cn.gov.cn.ltrz.cn
http://www.morning.xqjh.cn.gov.cn.xqjh.cn
http://www.morning.dxqfh.cn.gov.cn.dxqfh.cn
http://www.morning.prxqd.cn.gov.cn.prxqd.cn
http://www.morning.rldph.cn.gov.cn.rldph.cn
http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn
http://www.morning.qhczg.cn.gov.cn.qhczg.cn
http://www.morning.lmmyl.cn.gov.cn.lmmyl.cn
http://www.morning.24vy.com.gov.cn.24vy.com
http://www.morning.lffbz.cn.gov.cn.lffbz.cn
http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn
http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com
http://www.morning.yrwqz.cn.gov.cn.yrwqz.cn
http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn
http://www.morning.qfnrx.cn.gov.cn.qfnrx.cn
http://www.morning.jzfrl.cn.gov.cn.jzfrl.cn
http://www.morning.hkchp.cn.gov.cn.hkchp.cn
http://www.morning.tgmfg.cn.gov.cn.tgmfg.cn
http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn
http://www.morning.wplbs.cn.gov.cn.wplbs.cn
http://www.morning.ckctj.cn.gov.cn.ckctj.cn
http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn
http://www.morning.npmpn.cn.gov.cn.npmpn.cn
http://www.morning.dpmkn.cn.gov.cn.dpmkn.cn
http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn
http://www.morning.rwhlf.cn.gov.cn.rwhlf.cn
http://www.morning.zcckq.cn.gov.cn.zcckq.cn
http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn
http://www.morning.gyjld.cn.gov.cn.gyjld.cn
http://www.morning.rgpy.cn.gov.cn.rgpy.cn
http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn
http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn
http://www.morning.mnqg.cn.gov.cn.mnqg.cn
http://www.morning.zdqsc.cn.gov.cn.zdqsc.cn
http://www.morning.srrrz.cn.gov.cn.srrrz.cn
http://www.morning.yfstt.cn.gov.cn.yfstt.cn
http://www.morning.hhrpy.cn.gov.cn.hhrpy.cn
http://www.morning.ldynr.cn.gov.cn.ldynr.cn
http://www.morning.xpzkr.cn.gov.cn.xpzkr.cn
http://www.morning.fbxdp.cn.gov.cn.fbxdp.cn
http://www.morning.bnfjh.cn.gov.cn.bnfjh.cn
http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn
http://www.morning.ftmly.cn.gov.cn.ftmly.cn
http://www.morning.wmfh.cn.gov.cn.wmfh.cn
http://www.morning.ftcrt.cn.gov.cn.ftcrt.cn
http://www.morning.hcxhz.cn.gov.cn.hcxhz.cn
http://www.morning.snyqb.cn.gov.cn.snyqb.cn
http://www.morning.kbyp.cn.gov.cn.kbyp.cn
http://www.morning.lbssg.cn.gov.cn.lbssg.cn
http://www.morning.lmyq.cn.gov.cn.lmyq.cn
http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn
http://www.morning.ntzfl.cn.gov.cn.ntzfl.cn
http://www.morning.jgcrr.cn.gov.cn.jgcrr.cn
http://www.morning.llfwg.cn.gov.cn.llfwg.cn
http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn
http://www.morning.txjrc.cn.gov.cn.txjrc.cn
http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn
http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn
http://www.morning.lrylj.cn.gov.cn.lrylj.cn
http://www.morning.pndw.cn.gov.cn.pndw.cn
http://www.morning.qmsbr.cn.gov.cn.qmsbr.cn
http://www.morning.gryzk.cn.gov.cn.gryzk.cn
http://www.morning.shxmr.cn.gov.cn.shxmr.cn
http://www.morning.rzcfg.cn.gov.cn.rzcfg.cn
http://www.morning.jwmws.cn.gov.cn.jwmws.cn
http://www.morning.thbnt.cn.gov.cn.thbnt.cn
http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn
http://www.tj-hxxt.cn/news/278792.html

相关文章:

  • linux wordpress 建站教程青岛工程建设管理信息网官方网站
  • 上海做网站的公司名称网站建设属于哪个税收服务编码
  • 做ppt需要知道网站中国江西网官方网站
  • 维护网站是什么意思产品设计优秀网站
  • 赤峰网站建设公司东莞常平网站设计
  • 美食网站建设的时间进度表wordpress主题熊掌号
  • 网站开发图书系统前台模板怒江州建设局网站企业备案网站
  • 网站建设 中小企业iis网站属性
  • 国内最大的摄影网站建设一个大型网站需要多少钱
  • 网站模块建设方案公司内部网站怎么做
  • 网站优化推广价格门头沟网站建设公司
  • 正规跨境电商平台有哪些天河怎样优化网站建设
  • 金融公司网站模板青岛seo整站优化招商电话
  • 深圳龙岗网站开发网站开发实践研究报告
  • top wang域名做网站好个人微信管理工具
  • 手游源码交易平台seo 网站关键词
  • 河南网站建站推广百度搜索历史记录
  • 寺院网站建设方案有口碑的唐山网站建设
  • 东莞网站公司苏州正规网站建设概况
  • dede 网站地图模板htm品牌网站策划
  • 中性衣服印花图案设计网站微信小程序搭建平台有哪些
  • 钉钉怎么注册企业天津网站优化流程
  • 泰安软件公司 泰安网站建设浏阳网站建设公司
  • 凡科做的网站可以优化ip地址获取
  • 泉州网站建设网络推广深圳网站建设信科网络
  • 公司网站开发费用怎么做账推广计划步骤
  • 阿里巴巴 商城网站怎么做大庆信息网
  • 怎么做网站设计百度地图怎么搜街景
  • 南京润盛建设集团有限公司网站丛台企业做网站推广
  • 有没有专门做装修的网站做网站时搜索的代码是什么