-------------------siwuxie095

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

基于 AspectJ 的 XML 方式进行 AOP 开发

 
 

 
 

1、首先导入
jar 包(共 10 个包)

 
 

(1)导入核心 jar 包和日志相关的 jar 包

 
 

 
 

 
 

(2)导入 AOP 和
AspectJ 的 jar 包

 
 

 
 

 
 

其中:

 
 

aopalliance
下载链接:

 
 

http://mvnrepository.com/artifact/aopalliance/aopalliance

 
 

 
 

aspectjweaver
下载链接:

 
 

http://mvnrepository.com/artifact/org.aspectj/aspectjweaver

 
 

 
 

 
 

 
 

2、创建
Spring 核心配置文件,引入新的 XML 约束

 
 

spring-aop-4.3.xsd

 
 

注意:要引入和
Spring 版本对应的 XML 约束

 
 

 
 


applicationContext.xml 中添加如下约束:

 
 

<?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.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd">

 
 

 
 

 
 

 
 

3、使用表达式配置切入点:切入点表达式

 
 

(1)切入点,即
实际被增强的方法

 
 

 
 

(2)常用表达式:

 
 

execution(<访问修饰符>? <返回值类型> <类型声明>? <方法名>(<参数>) <异常>?)

 
 

1)必选:返回值类型、方法名、参数

 
 

2)可选:访问修饰符、类型声明、异常

 
 

 
 

(3)通配符:

 
 

1)*:匹配任意数量的字符

 
 

2)..:匹配任意数量的包

参数

 
 

 
 

(4)举例如下:

 
 

1)execution(* com.siwuxie095.aop.Book.add(..))

 
 

匹配特定包和类下的 add 方法

 
 

 
 

2)execution(* com.siwuxie095.aop.Book.*(..))

 
 

匹配特定包和类下的所有方法

 
 

 
 

3)execution(* *.*(..))

 
 

匹配所有方法

 
 

 
 

4)execution(* add*(..))

 
 

匹配所有 add 开头的方法

 
 

 
 

 
 

参考链接:http://www.cnblogs.com/softidea/p/6102770.html

 
 

 
 

 
 

 
 

4、具体实现

 
 

(1)编写一个被增强类

 
 

Book.java:

 
 

package com.siwuxie095.aop;

 
 

//被增强类

public class Book {

 
 

public
void add() {

System.out.println("----- add -----");

}

 

}

 
 

 
 

 
 

(2)编写一个增强类

 
 

MyBook.java:

 
 

package com.siwuxie095.aop;

 
 

import org.aspectj.lang.ProceedingJoinPoint;

 
 

// 增强类

public class MyBook {

 
 

public
void beforeAdd() {

System.out.println("----- 前置增强 -----");

}

 

public
void afterAdd(){

System.out.println("----- 后置增强 -----");

}

 

public
void aroundAdd(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

System.out.println("----- 环绕增强(方法之前) -----");

//执行被增强的方法

proceedingJoinPoint.proceed();

System.out.println("----- 环绕增强(方法之前) -----");

}

 

}

 
 

 
 

 
 

(3)在配置文件中进行配置

 
 

applicationContext.xml:

 
 

<?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.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd">

 
 

 
 

<!-- 配置对象 -->

<bean
id="book"
class="com.siwuxie095.aop.Book"></bean>

<bean
id="myBook"
class="com.siwuxie095.aop.MyBook"></bean>

 
 

 
 

<!-- 配置 AOP -->

<aop:config>

 

<!-- 配置切入点:哪些类的哪些方法需要增强 -->

<aop:pointcut
expression="execution(* com.siwuxie095.aop.Book.add(..))"
id="pt"/>

 

<!-- 配置切面:把增强应用到切入点上 -->

<aop:aspect
ref="myBook">

<aop:before
method="beforeAdd"
pointcut-ref="pt"/>

<aop:after-returning
method="afterAdd"
pointcut-ref="pt"/>

<aop:around
method="aroundAdd"
pointcut-ref="pt"/>

</aop:aspect>

 

</aop:config>

 
 

 
 

</beans>

 
 

 
 

 
 

(4)编写一个测试类

 
 

TestAop.java:

 
 

package com.siwuxie095.aop;

 
 

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 
 

public class TestAop {

 

/**

* 手动加上 @Test 以进行单元测试(将自动导入 JUnit 4 的 jar 包)

*

* 选中方法名,右键->Run As->JUint Test

*/

@Test

public
void testAop() {

 

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

 

Book book=(Book) context.getBean("book");

 

book.add();

}

 

}

 
 

 
 

 
 

(5)运行一览:

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

【made by siwuxie095】

基于AspectJ的XML方式进行AOP开发的更多相关文章

  1. 基于AspectJ的注解方式进行AOP开发

    -------------------siwuxie095                                     基于 AspectJ 的注解方式进行 AOP 开发         ...

  2. 【AOP】操作相关术语---【Spring】的【AOP】操作(基于aspectj的xml方式)

    [AOP]操作相关术语 Joinpoint(连接点):类里面哪些方法可以被增强,这些方法称为连接点. Pointcut(切入点):在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面add ...

  3. Spring的AOP开发入门,Spring整合Junit单元测试(基于ASpectJ的XML方式)

    参考自 https://www.cnblogs.com/ltfxy/p/9882430.html 创建web项目,引入jar包 除了基本的6个Spring开发的jar包外,还要引入aop开发相关的四个 ...

  4. Spring的AOP开发(基于AspectJ的XML方式)

    Spring的AOP的简介: AOP思想最早是由AOP联盟组织提出的.Spring是使用这种思想最好的框架 Spring的AOP有自己实现的方式(非常繁琐). Aspect是一个AOP的框架, Spr ...

  5. spring框架之AspectJ的XML方式完成AOP的开发

    1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...

  6. AspectJ的XML方式完成AOP的开发之切入点的表达式

    1. 再配置切入点的时候,需要定义表达式,重点的格式如下:execution(public * *(..)),具体展开如下: * 切入点表达式的格式如下: * execution([修饰符] 返回值类 ...

  7. Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)

    1. 步骤一:恢复转账开发环境(转账开发环境见“https://www.cnblogs.com/wyhluckdog/p/10137283.html”) 2.步骤二:引入AOP的开发包3.步骤三:引入 ...

  8. AspectJ的XML方式完成AOP的开发之AOP的通知类型

    1. 前置通知 * 在目标类的方法执行之前执行. * 配置文件信息:<aop:after method="before" pointcut-ref="myPoint ...

  9. Spring事务管理之声明式事务管理-基于AspectJ的XML方式

    © 版权声明:本文为博主原创文章,转载请注明出处 案例 - 利用Spring的声明式事务(AspectJ)管理模拟转账过程 数据库准备 -- 创建表 CREATE TABLE `account`( ` ...

随机推荐

  1. [转]NSIS:使用SectionSetFlags根据不同环境自动勾选特定区段

    转自: http://www.flighty.cn/html/bushu/20140526_232.html   在微软SQL2000+SP4集成安装版安装包中可以根据目标操作系统自动勾选对应的版本, ...

  2. PHP实现微信申请退款(证书权限必须设为可执行)

    前期准备: 当然是搞定了微信支付,不然怎么退款,这次还是使用官方的demo.当然网上可能也有很多大神自己重写和封装了demo,或许更加好用简洁,但是我还是不提倡用,原因如下: (1)可能功能不全,或许 ...

  3. 【转载】html中object标签详解

    [转载自http://blog.csdn.net/soliy/archive/2010/03/22/5404183.aspx] html标签之Object标签详解 作者:网络    出处:网络     ...

  4. Intellij IDEA 中如何查看maven项目中所有jar包的依赖关系图

    Maven 组件界面介绍 如上图标注 1 所示,为常用的 Maven 工具栏,其中最常用的有: 第一个按钮:Reimport All Maven Projects 表示根据 pom.xml 重新载入项 ...

  5. js判断各种浏览器

    <script type="text/javascript"> if (navigator.appName.indexOf("Microsoft Intern ...

  6. python并发编程之多进程理论部分

    原文连接:http://www.cnblogs.com/linhaifeng/articles/7430066.html#_label4 一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责 ...

  7. Redis-基本数据类型与内部存储结构

    1-概览 Redis是典型的Key-Value类型数据库,Key为字符类型,Value的类型常用的为五种类型:String.Hash .List . Set . Ordered Set 2- Redi ...

  8. JavaScript词法分析(尽力理解)

    JavaScript中在调用函数的那一瞬间之前,会先进行词法分析 词法分析的过程: 当函数调用的前一瞬间,会先形成一个激活对象:Avtive Object(AO),并会分析以下3个方面: 1:函数参数 ...

  9. WGCNA 分析

    https://www.jianshu.com/p/f80de3468c04 https://mp.weixin.qq.com/s/-DthUKY2RTY6vxtxapzLkw https://www ...

  10. 机器学习入门-K-means算法

    无监督问题,我们手里没有标签 聚类:相似的东西聚在一起 难点:如何进行调参 K-means算法 需要制定k值,用来获得到底有几个簇,即几种类型 质心:均值,即向量各维取平均值 距离的度量: 欧式距离和 ...