-------------------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. java study3

    面向过程与面向对象 面向过程:必须了解整个过程,每个步骤互成因果关系,每个因果关系都构成一个步骤,多个步骤就构成了一个系统.因为存在因果关系,每隔步骤难以分离,非常紧密,当任何一步出现问题,将会影响到 ...

  2. 1042 Shuffling Machine (20 分)

    1042 Shuffling Machine (20 分) Shuffling is a procedure used to randomize a deck of playing cards. Be ...

  3. windows 网管常用命令

    Windows网络命令行程序 这部分包括: 使用 ipconfig /all 查看配置 使用 ipconfig /renew 刷新配置 使用 ipconfig 管理 DNS 和 DHCP 类别 ID ...

  4. RedisCluster读写分离改造

      RedisCluster模式启动的环境中,通过Redis中的每个连接,都可以访问 cluster nodes 访问到所有的服务器列表以及其所处于的角色(master/slave).对于RedisC ...

  5. Oracle 11g trace events

    oracle的events,是我们在做自己的软件系统时可以借鉴的 Oracle 11g trace eventsORA-10001: control file crash event1ORA-1000 ...

  6. AFNetworkingErrorDomain 错误解决方法

    首先我们来看一下错误信息: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299 ...

  7. python(十二)下:ORM框架SQLAlchemy使用学习

    此出处:http://blog.csdn.net/fgf00/article/details/52949973 本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 ...

  8. BAT脚本编写教程(比较易懂和全面)

    这篇文章主要介绍了BAT脚本编写教程,比较易懂和全面.适合有一定编程基础的人   作者不详.敬意! echo.@.call.pause.rem(小技巧:用::代替rem)是批处理文件最常用的几个命令, ...

  9. 经典算法 Manacher算法详解

    内容: 1.原始问题   =>O(N^2) 2.Manacher算法   =>O(N) 1.原始问题 Manacher算法是由题目“求字符串中长回文子串的长度”而来.比如 abcdcb 的 ...

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

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