AspectJ是一个基于java的aop框架。

切入点表达式:  execution() 用于描述方法

语法:  execution(修饰符  返回值 包.类.方法名(参数) throws 异常)

切面类:

package com.atchina.d_spring_aop_xml;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;public class MyAspect {public void before(JoinPoint joinPoint){System.out.println("aspect xml 事务开启 before "+joinPoint.getSignature().getName());}public void after(JoinPoint joinPoint, Object ret){System.out.println("aspect xml 事务开启 after "+joinPoint.getSignature().getName()+", -->"+ret);}public Object around(ProceedingJoinPoint joinPoint) throws Throwable{System.out.println("环绕通知,在目标方法之前...增强功能");// 手动执行目标方法Object obj = joinPoint.proceed();if(null !=obj){// 修改目标方法的执行结果obj = "update";}System.out.println("环绕通知,在目标方法之后...增强功能");return obj;}public void myThrowing(JoinPoint joinPoint, Throwable e){System.out.println("aspect xml 异常通知. "+joinPoint.getSignature().getName()+",  异常通知信息"+e.getMessage());}public void myAfter(JoinPoint joinPoint){System.out.println("aspect xml 最终通知 "+joinPoint.getSignature().getName());}
}

接口

public interface UserService {void addUser();String updateUser();
}
public class UserServiceImpl implements UserService {@Overridepublic void addUser() {System.out.println("config adduser...");}@Overridepublic String updateUser() {System.out.println("config updateUser...");// int i = 1/0;   // 测试异常通知return "updateUserRet";}
}

配置文件

<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 1.创建目标类 --><bean id="userService" class="com.atchina.d_spring_aop_xml.UserServiceImpl"/><!-- 2.创建切面类 --><bean id="myAspectId" class="com.atchina.d_spring_aop_xml.MyAspect"/><!-- 3. aop编程使用<aop:config>配置<aop:aspect> 将切面类声明"切面",从而获得通知方法ref:切面类引用<aop:pointcut> 声明一个切入点,所有的通知都可以使用expression:切入点表达式id:名称, 用于其他通知引用--> <aop:config><aop:aspect ref="myAspectId"><aop:pointcut expression="execution(* com.atchina.d_spring_aop_xml.UserServiceImpl.*(..))" id="myPointCut" /><!-- 前置通知 method:  通知,及方法名pointcut: 切入点表达式,此表达式只能在当前通知中使用。pointcut-ref: 切入点引用,可以与其他通知共享切入点通知方法格式:public void before(JoinPoint joinPoint)参数1:org.aspectj.lang.JoinPoint 用于描述连接点(目标方法),获得目标方法名等<aop:before method="before"  pointcut-ref="myPointCut"/>               --><!-- 后置通知 method:  通知,及方法名pointcut: 切入点表达式,此表达式只能在当前通知中使用。pointcut-ref: 切入点引用,可以与其他通知共享切入点returning: 通知方法第二个参数的名称通知方法格式:public void after(JoinPoint joinPoint, Object ret)参数1:org.aspectj.lang.JoinPoint 用于描述连接点(目标方法),获得目标方法名等参数2:类型Object, 参数名returning="ret"配置的                       如下:<aop:after-returning method="after" pointcut-ref="myPointCut" returning="ret"/>--><!--  环绕通知 method:  通知,及方法名pointcut: 切入点表达式,此表达式只能在当前通知中使用。pointcut-ref: 切入点引用,可以与其他通知共享切入点通知方法格式:public Object around(ProceedingJoinPoint joinPoint) throws Throwable返回值类型: Object方法名:    任意参数:     org.aspectj.lang.ProceedingJoinPoint 抛出异常:执行目标方法: Object obj = joinPoint.proceed();                     如下:<aop:around method="around" pointcut-ref="myPointCut"/>--><!-- 异常通知 method:  通知,及方法名pointcut: 切入点表达式,此表达式只能在当前通知中使用。pointcut-ref: 切入点引用,可以与其他通知共享切入点throwing: 通知方法第二个参数的名称通知方法格式:public void myThrowing(JoinPoint joinPoint, Throwable e)参数1:org.aspectj.lang.JoinPoint 用于描述连接点(目标方法),获得目标方法名等参数2:类型throwing, 参数名throwing="e"配置的                        如下:<aop:after-throwing method="myThrowing" pointcut-ref="myPointCut" throwing="e"/>--><!-- 最终通知 --><aop:after method="myAfter" pointcut-ref="myPointCut" /></aop:aspect></aop:config>
</beans>

测试类

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestFactoryBean {@Testpublic void test(){String xmlPath = "com/atchina/d_spring_aop_xml/applicationContext.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(xmlPath);UserService userService = (UserService)ac.getBean("userService");userService.addUser();String ret = userService.updateUser();System.out.println(ret);}
}

spring28: aspectJ--基于xml相关推荐

  1. AspectJ基于xml和基于注解

    一.基于xml 执行的切入点中具体方法有返回值,则方法结束会立即执行后置通知,然后再执行环绕通知的放行之后的代码: 2.连接点即所有可能的方法,切入点是正真被切的方法,连接点方法名: 其中,只有环绕通 ...

  2. spring 基于XML的申明式AspectJ通知的执行顺序

    spring 基于XML的申明式AspectJ通知的执行顺序 关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关 1. XML文件配置说明 图片来源:<Java EE企业级应用开发教程&g ...

  3. 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. 1 package com.y ...

  4. 基于xml的方式配置AOP

    用基于 XML 的配置声明切面 除了使用 AspectJ 注解声明切面, Spring 也支持在 Bean 配置文件中声明切面. 这种声明是通过 aop schema 中的 XML 元素完成的. 正常 ...

  5. spring基于XML的声明式事务控制-配置步骤

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  6. spring基于XML的AOP-编写必要的代码

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  7. Spring框架----Spring的基于XML的AOP的实现

    导入依赖 <dependency><groupId>org.springframework</groupId><artifactId>spring-co ...

  8. 基于XML的AOP配置

    创建spring的配置文件并导入约束 此处要导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> < ...

  9. Spring MVC之基于xml配置的web应用构建

    2019独角兽企业重金招聘Python工程师标准>>> 更多spring博文参考: http://spring.hhui.top/ 直接用SpringBoot构建web应用可以说非常 ...

  10. [JAVAEE]实验06:基于XML和基于注解的声明式事务管理方式模拟银行转账程序

    一.实验目的: 熟练掌握声明式事务管理. 二.实验内容: 编写一个模拟银行转账的程序,要求在转账时通过Spring对事务进行控制. 三.实验要求: 分别使用基于XML和基于注解的声明式事务管理方式来实 ...

最新文章

  1. 在批处理中调用JS操作MongoDB
  2. Shark Hive Spark Hadoop2 进行整合的测试。
  3. python pip如何安装wheel文件?.whl(pip install [wheel])
  4. npm的插件如何直接在html中使用,webpack插件之htmlWebpackPlugin
  5. 在Java应用程序中使用密码学
  6. python range函数范围_Python range函数
  7. python列表去重效率,你应该知道的python列表去重方法
  8. ajax post参数长度限制,Ajax中的POST数据大小是否有限制?
  9. 小一爬取一季度 15646 只基金持仓数据,下半年重点关注这些机会!
  10. centos6.5 安装python3.5
  11. SAX EntityResolver 的作用
  12. 阿里云基础产品技术月刊 2018年12月
  13. Wiki管理工具有哪些?11款Wiki工具对比
  14. Android端M3U8视频下载管理器----M3U8Manger
  15. MiniGUI——第一个程序helloworld
  16. element表格el-table表头显示斜杠
  17. Solidity语言详解
  18. [足式机器人]Part3机构运动微分几何学分析与综合Ch01-3 平面运动微分几何学——【读书笔记】
  19. 淘宝评价计分规则,店铺信用评价如何累计
  20. chromium 03 修改chromium 编译版本号

热门文章

  1. C语言建立有向图的邻接表及其遍历操作
  2. mode: 'history', 去掉路由地址的#
  3. 【转】浅析C语言的非局部跳转:setjmp和longjmp
  4. 网页中如何启用QQ交谈
  5. 旋转矩阵、欧拉角、四元数、轴/角之间的转换
  6. 穿越五年的时空,重回三元湖畔
  7. js如何判断字符串里面是否含有某个字符串
  8. 3.1.2 vernam 代数密码
  9. 移动APP开发中8大安全问题
  10. 这次是在没有外网yum仓库的情况下搭建内网yum仓库和无人值守pxe装机