基于AspectJ的XML方式进行AOP开发
来源:互联网 发布:矢量 软件 编辑:程序博客网 时间:2024/06/05 06:14
-------------------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 中添加如下约束:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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 {
publicvoid add() {
System.out.println("----- add -----");
}
}
(2)编写一个增强类
MyBook.java:
package com.siwuxie095.aop;
import org.aspectj.lang.ProceedingJoinPoint;
//增强类
public class MyBook {
publicvoid beforeAdd() {
System.out.println("-----前置增强 -----");
}
publicvoid afterAdd(){
System.out.println("-----后置增强 -----");
}
publicvoid aroundAdd(ProceedingJoinPoint proceedingJoinPoint)throws Throwable {
System.out.println("-----环绕增强(方法之前) -----");
//执行被增强的方法
proceedingJoinPoint.proceed();
System.out.println("-----环绕增强(方法之前) -----");
}
}
(3)在配置文件中进行配置
applicationContext.xml:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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">
<!--配置对象 -->
<beanid="book"class="com.siwuxie095.aop.Book"></bean>
<beanid="myBook"class="com.siwuxie095.aop.MyBook"></bean>
<!--配置 AOP -->
<aop:config>
<!--配置切入点:哪些类的哪些方法需要增强 -->
<aop:pointcutexpression="execution(* com.siwuxie095.aop.Book.add(..))"id="pt"/>
<!--配置切面:把增强应用到切入点上 -->
<aop:aspectref="myBook">
<aop:beforemethod="beforeAdd"pointcut-ref="pt"/>
<aop:after-returningmethod="afterAdd"pointcut-ref="pt"/>
<aop:aroundmethod="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
publicvoid testAop() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Book book=(Book) context.getBean("book");
book.add();
}
}
(5)运行一览:
【made by siwuxie095】
- 基于AspectJ的XML方式进行AOP开发
- 基于AspectJ的注解方式进行AOP开发
- Spring学习-22:Spring的AOP:基于AspectJ的XML配置方式开发
- Spring使用AspectJ进行AOP的开发:注解方式
- Spring AOP技术(基于AspectJ)的XML开发
- Spring Aop开发基于AspectJ注解方式的案例
- Spring的基于AspectJ的AOP开发
- Spring AOP使用Aspectj基于xml方式,初始化Bean参数
- 基于@AspectJ的AOP
- 基于@AspectJ的AOP
- Spring(十一)AspectJ框架开发AOP(基于xml)
- 使用Aspectj进行AOP开发
- 使用Aspectj进行AOP开发
- 使用Aspectj进行AOP开发
- Spring AOP基于@AspectJ开发
- AOP的annotation实现方式是基于AspectJ的实现
- Spring的AOP(基于ASpectj的方式配置)
- AOP-AspectJ-基于XML的实现——各种通知
- lua and or not
- windows10+vs2010+opencv2.4.13opencv配置的方法
- 线段树
- Java开发中的23种设计模式详解(转)
- 浅析商业银行数据安全保护体系建设思路
- 基于AspectJ的XML方式进行AOP开发
- 分析IBM JVM上的heap dump文件a.phd
- PM2 介绍
- Android延时发送信息的几类方法!
- test
- msf install
- java多态杂记
- Linux学习之路
- lua 转义字符