本文连接:https://www.cnblogs.com/qzhc/p/11969734.html

接下来我将用一个很简单的实例

1、 环境搭建

1.1、 第一步:准备必要的代码

业务层代码:

AccountServiceImpl.java

  1. package com.henu.service.impl;
  2.  
  3. import com.henu.service.AccountService;
  4.  
  5. public class AccountServiceImpl implements AccountService {
  6. public void saveAccount() {
  7. System.out.println("执行了保存");
  8. }
  9. }

测试类:

AOPtest.java

  1. package com.henu.test;
  2.  
  3. import com.henu.service.AccountService;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6.  
  7. public class AOPtest {
  8. public static void main(String[] args) {
  9. ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  10. AccountService as = (AccountService) ac.getBean("accountService");
  11. as.saveAccount();
  12. }
  13. }

1.2、导入所需要的jar包

我的demo是用maven管理的:pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>com.henu</groupId>
  8. <artifactId>day03_spring_adviceType</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <packaging>jar</packaging>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring-context</artifactId>
  15. <version>5.0.2.RELEASE</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.aspectj</groupId>
  19. <artifactId>aspectjweaver</artifactId>
  20. <version>1.8.7</version>
  21. </dependency>
  22. </dependencies>
  23.  
  24. </project>

1.3、创建 spring 的配置文件并导入约束和配置spring的ioc

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop.xsd">
  9. <!--配置spring的Ioc,把service对象配置起来-->
  10.  
  11. <bean id="accountService" class="com.henu.service.impl.AccountServiceImpl"></bean>

1.4、写一个公共类作为通知

logger.java

  1. package com.henu.utils;
  2. //记录日志
  3. public class Logger {
  4. /**
  5. * 前置通知
  6. * */
  7. public void befterPrintLog(){
  8. System.out.println("前置通知logger开始记录日志了");
  9. }
  10. /**
  11. * 后置通知
  12. * */
  13. public void afterReturningPrintLog(){
  14. System.out.println("后置通知logger开始记录日志了");
  15. }
  16. /**
  17. * 异常通知
  18. * */
  19. public void afterThrowingPrintLog(){
  20. System.out.println("异常通知logger开始记录日志了");
  21. }
  22. /**
  23. * 最终通知
  24. * */
  25. public void afterPrintLog(){
  26. System.out.println("最终通知logger开始记录日志了");
  27. }
  28.  
  29. }

2、aop配置步骤(先给代码,在讲)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop.xsd">
  9. <!--配置spring的Ioc,把service对象配置起来-->
  10.  
  11. <bean id="accountService" class="com.henu.service.impl.AccountServiceImpl"></bean>
  12. <!--配置logger类 -->
  13. <bean id="logger" class="com.henu.utils.Logger"></bean>
  14. <!--配置AOP-->
  15. <aop:config>
  16. <!--配置切面表达式 id属性用于指定表达式的唯一标示。execution属性用于指定表达式内容
  17. 此标签写在aop:aspect标签内部只能当前切面使用
  18. 他还可以写在aop:aspect外面,但必须至首,全局都可以使用
  19. -->
  20. <aop:pointcut id="pt1" expression="execution(* com.henu.service.impl.*.*(..))"/>
  21. <!--配置切面-->
  22. <aop:aspect id="logAdvice" ref="logger">
  23.  
  24. <!--配置通知的类型,并且建立通知方法和切入点方法的关联-->
  25. <!--配置前置通知:在切入点方法执行之前-->
  26. <aop:before method="befterPrintLog" pointcut-ref="pt1"></aop:before>
  27. <!--配置后置通知:在切入点方法正常执行之后。它与异常只执行一个-->
  28. <aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>
  29. <!--配置异常通知:在切入点方法执行产生异常之后。它与后置只执行一个-->
  30. <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>
  31. <!--配置最终通知:无论切入点是否正常执行,它都会在其后面执行-->
  32. <aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>
  33.  
  34. </aop:aspect>
  35. </aop:config>
  36.  
  37. </beans>

2.1、把通知类用bean标签配置起来

  1. <!--配置logger类 -->
  2. <bean id="logger" class="com.henu.utils.Logger"></bean>

2.2、使用aop:config声明aop配置

  aop:config:

   作用:用于声明开始 aop 的配置 

    <aop:config>

       <!--配置的代码都写在此处-->        

    </aop:config>

2.3、使用 aop:aspect 配置切面

  1. aop:aspect:
  2.   作用:
  3.     用于配置切面。
  4.   属性:
  5.     id:给切面提供一个唯一标识。
  6.     ref:引用配置好的通知类 bean id
  7. <aop:aspect id="logAdvice" ref="logger">
  8. <!--配置通知的类型要写在此处-->
  9. </aop:aspect>

2.4、使用 aop:pointcut 配置切入点表达式

  大家看到,使用下面的方法,会使之前的代码减少很多

  1. aop:pointcut
  2. 作用:
  3. 用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。
  4. 属性:
  5. expression:用于定义切入点表达式。
  6. id:用于给切入点表达式提供一个唯一标识
  7. <aop:pointcut id="pt1" expression="execution(* com.henu.service.impl.*.*(..))" />

2.4、使用aop:xxx配置对应的通知类型

  1. aop:before
  2. 作用:
  3.   用于配置前置通知。指定增强的方法在切入点方法之前执行
  4. 属性:
  5.   method:用于指定通知类中的增强方法名称
  6.   ponitcut-ref:用于指定切入点的表达式的引用
  7.    poinitcut:用于指定切入点表达式
  8. 执行时间点:
  9.   切入点方法执行之前执行
  10. <aop:before method="beginTransaction" pointcut-ref="pt1"/>

  11.    aop:after-returning
  12. 作用:
  13.   用于配置后置通知
  14. 属性:
  15.   method:指定通知中方法的名称。
  16.   pointct:定义切入点表达式
  17.   pointcut-ref:指定切入点表达式的引用
  18. 执行时间点:
  19.   切入点方法正常执行之后。它和异常通知只能有一个执行
  20. <aop:after-returning method="commit" pointcut-ref="pt1"/>

  21.    aop:after-throwing
  22. 作用:
  23.   用于配置异常通知
  24. 属性:
  25.   method:指定通知中方法的名称。
  26.   pointct:定义切入点表达式
  27.   pointcut-ref:指定切入点表达式的引用
  28. 执行时间点:
  29.   切入点方法执行产生异常后执行。它和后置通知只能执行一个
  30. <aop:after-throwing method="rollback" pointcut-ref="pt1"/>

  31.    aop:after
  32. 作用:
  33.   用于配置最终通知
  34. 属性:
  35.   method:指定通知中方法的名称。
  36.   pointct:定义切入点表达式
  37.   pointcut-ref:指定切入点表达式的引用
  38. 执行时间点:
  39.   无论切入点方法执行时是否有异常,它都会在其后面执行。
  40. <aop:after method="release" pointcut-ref="pt1"/>

2.5、切入点表达式说明

  1. execution:匹配方法的执行(常用)
  2. execution(表达式)
  3. 表达式语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
  4. 写法说明:
  5. 全匹配方式
  6.   public void com.henu.service.impl.AccountServiceImpl.saveAccount()
  7. 访问修饰符可以省略
  8.   void com.itheima.service.impl.AccountServiceImpl.saveAccount()
  9. 返回值可以使用*号,表示任意返回值
  10.   * com.henu.service.impl.AccountServiceImpl.saveAccount()
  11. 包名可以使用*号,表示任意包,但是有几级包,需要写几个*.
  12.   * *.*.*.*.AccountServiceImpl.saveAccount()
  13. 使用..来表示当前包,及其子包
  14.   * com..AccountServiceImpl.saveAccount()
  15. 类名和方法名可以使用*号来实现通配
  16.   * *..*.*()
  17. 参数列表可以使用*,表示参数可以是任意数据类型,但是必须有参数
  18.   * com..*.*(*)
  19. 参数列表可以使用..表示有无参数均可,有参数可以是任意类型
  20.   * com..*.*(..)
  21. 全通配方式:
  22.   * *..*.*(..)
  23. 注:
  24. 通常情况下,我们都是对业务层的方法进行增强,所以切入点表达式都是切到业务层实现类。
  25. execution(* com.henu.service.impl.*.*(..))

讲到这里基本都差不多了,少啥以后再补把。

基于 XML 的 AOP 配置(1)的更多相关文章

  1. 基于XML的AOP配置

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

  2. spring的基于xml的AOP配置案例和切入点表达式的一些写法

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  3. 基于XML的AOP配置(2)-环绕通知

    配置方式: <aop:config> <aop:pointcut expression="execution(* com.itheima.service.impl.*.*( ...

  4. 基于XML的AOP配置-转

    http://www.cnblogs.com/yangy608/archive/2010/11/14/1876839.html AOP(Aspect-Oriented Programming,面向切面 ...

  5. Spring中AOP的基于xml开发和配置

    pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

  6. Spring中基于xml的AOP

    1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...

  7. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

  8. 01Spring基于xml的IOC配置--入门

    01Spring基于xml的IOC配置 1.创建一个普通的maven工程 1.1 选择maven,不用骨架,点击下一步. 1.2 填写GroupId.ArtifactId.Version.填完点击下一 ...

  9. 面向切面编程AOP:基于XML文件的配置

    除了使用AspectJ注解声明切面,Spring也支持在bean的配置文件中声明切面,这种声明是通过aop scheme中的XML元素完成的. 首先建立一个类: package com.sevenhu ...

随机推荐

  1. spring AOP的相关术语

    连接点:Joinpoint   其实业务层接口的方法 切入点:Pointcut 被增强的是切入点,没被增强是永远都是连接点.连接点不一定是切入点,切入点一定是连接点 通知:Advice 就是指要增强的 ...

  2. IOS 伪类:active失效

    IOS中不兼容:active伪类,所以在状态控制上就会有问题,解决办法是,添加一个空的点击事件: 移动端添加:ontouchstart <a class="m-btn" on ...

  3. python之jupyter notebook

    jupyter是一种交互式计算和开发环境的笔记,ipython命令行比原生的python命令行更加友好和高效,还可以运行web版的界面,支持多语言,输出图形.音频.视频等功能. 安装 pip inst ...

  4. js相关的时间获取方法

    1.获取时间 var time=new Date();//返回的是GMT,格林尼治标准时间. console.log(time)://Thu Jul 27 2017 16:55:21 GMT+0800 ...

  5. CSS3总结四:盒模型(box)

    盒模型:标准盒模型.混杂盒模型 什么时候会触发混杂模式? 为什么要混杂模式渲染规则盒模型? box-sizing:content-box.border-box 关于盒模型的一些其他样式和属性值 一.盒 ...

  6. 快速写个node命令行工具

    1.package.json-bin配置 [创建bat文件,把bat路径添加到PATH中]这些固定的工作可以由npm帮我们完成.package.json中有个bin字段配置: bin: { " ...

  7. JSP中的普通路径写法

    <% String path = request.getContextPath();     String basePath = request.getScheme()+"://&qu ...

  8. Linux 永久挂载镜像文件和制作yum源

    Linux mount命令是经常会使用到的命令,它用于挂载Linux系统外的文件. 1.镜像挂载到系统指定目录下:[root@master cdrom]#  mount -t auto  /mnt/c ...

  9. springboot整合mybatis-plus基于纯注解实现一对一(一对多)查询

    因为目前所用mybatis-plus版本为3.1.1,感觉是个半成品,所有在实体类上的注解只能支持单表,没有一对一和一对多关系映射,且该功能还在开发中,相信mybatis-plus开发团队在不久的将来 ...

  10. pam_smb

    What is pam_smb? pam_smb is a PAM module/server which allows authentication of UNIX users using an N ...