Spring AOP如何基于AspectJ XML开发呢?
下文讲述使用xml配置文件的方式为Spring AOP定义切面、切入点及通知
那么如何定义呢?
AspectJ使用xml配置前,首先需引入命名空间
然后再使用 <aop:config>进行相关配置
采用<aop:aspect>元素定义切面,
该元素可以将定义好的 Bean 转换为切面 Bean, 所以使用 <aop:aspect>之前需要先定义一个普通的 Spring Bean
当<aop:pointcut>元素作为<aop:config>元素的子元素定义时 表示该切入点是全局切入点,它可被多个切面所共享
当 <aop:pointcut> 元素作为<aop:aspect>元素的子元素时,表示该切入点只对当前切面有效
前置通知
后置通知
返回值为:java265
前置通知
后置通知
返回值为:888
前置通知
抛出异常
后置通知
这里的异常为:java.lang.IllegalArgumentException
Exception in thread "main" java.lang.IllegalArgumentExceptio
那么如何定义呢?
AspectJ使用xml配置前,首先需引入命名空间
然后再使用 <aop:config>进行相关配置
引入aop命名空间
<?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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> ... </beans>
定义切面<aop:aspect>
在Spring 配置文件中,采用<aop:aspect>元素定义切面,
该元素可以将定义好的 Bean 转换为切面 Bean, 所以使用 <aop:aspect>之前需要先定义一个普通的 Spring Bean
<aop:config> <aop:aspect id="myAspect" ref="aBean"> ... </aop:aspect> </aop:config> id 用来定义该切面的唯一表示名称,ref 用于引用普通的 Spring Bean
定义切入点<aop:pointcut>
<aop:pointcut> 用来定义一个切入点当<aop:pointcut>元素作为<aop:config>元素的子元素定义时 表示该切入点是全局切入点,它可被多个切面所共享
当 <aop:pointcut> 元素作为<aop:aspect>元素的子元素时,表示该切入点只对当前切面有效
<aop:config> <aop:pointcut expression="execution(* com.java265.service.*.*(..))" id="myPointCut" /> </aop:config> 其中,id 用于指定切入点的唯一标识名称,execution 用于指定切入点关联的切入点表达式。
execution 格式为: execution(modifiers-pattern returning-type-pattern declaring-type-pattern name-pattern(param-pattern)throws-pattern) 其中: returning-type-pattern、name-pattern、param-pattern 是必须的,其它参数为可选项。 modifiers-pattern:指定修饰符,如 private、public。 returning-type-pattern:指定返回值类型,*表示可以为任何返回值。如果返回值为对象,则需指定全路径的类名。 declaring-type-pattern:指定方法的包名。 name-pattern:指定方法名,*代表所有,set* 代表以 set 开头的所有方法。 param-pattern:指定方法参数(声明的类型),(..)代表所有参数,(*)代表一个参数,(*,String)代表第一个参数可以为任何值,第二个为 String 类型的值。 throws-pattern:指定抛出的异常类型。例:execution(* com.java265.*.*(..))表示匹配 com.java265 包中任意类的任意方法
定义通知
AspectJ 支持 5 种类型的 advice,如下<aop:aspect id="myAspect" ref="aBean"> <!-- 前置通知 --> <aop:before method="..." pointcut-ref="myPointCut" /> <!-- 后置通知 --> <aop:after-returning method="..." pointcut-ref="myPointCut" /> <!-- 环绕通知 --> <aop:around method="..." pointcut-ref="myPointCut" /> <!-- 异常通知 --> <aop:after-throwing method="..." pointcut-ref="myPointCut" /> <!-- 最终通知 --> <aop:after method="..." pointcut-ref="myPointCut" /> .... </aop:aspect>例
- 创建 SpringDemo 项目
- 在 src 目录下创建 com.java265 包
- 导入 Spring 相关 JAR 包及 Aspectjrt.jar、Aspectjweaver.jar、Aspectj.jar
- 在 com.java265 包下创建 Logging、Man、Beans.xml 和 MainApp
- 运行 SpringDemo 项目
package com.java265;
public class Logging {
/**
* 前置通知
*/
public void beforeAdvice() {
System.out.println("前置通知");
}
/**
* 后置通知
*/
public void afterAdvice() {
System.out.println("后置通知");
}
/**
* 返回后通知
*/
public void afterReturningAdvice(Object retVal) {
System.out.println("返回值为:" + retVal.toString());
}
/**
* 抛出异常通知
*/
public void afterThrowingAdvice(IllegalArgumentException ex) {
System.out.println("这里的异常为:" + ex.toString());
}
}
Man 类
package com.java265;
public class Man {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void throwException() {
System.out.println("抛出异常");
throw new IllegalArgumentException();
}
}
Beans.xml
<!--?xml version="1.0" encoding="UTF-8"?--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:config> <aop:aspect id="log" ref="logging"> <aop:pointcut expression="execution(* com.java265.*.*(..))" id="selectAll"> <aop:before method="beforeAdvice" pointcut-ref="selectAll"> <aop:after method="afterAdvice" pointcut-ref="selectAll"> <aop:after-returning method="afterReturningAdvice" pointcut-ref="selectAll" returning="retVal"> <aop:after-throwing method="afterThrowingAdvice" pointcut-ref="selectAll" throwing="ex"> </aop:after-throwing></aop:after-returning></aop:after></aop:before></aop:pointcut></aop:aspect> </aop:config> <bean class="com.java265.Man" id="man"> <property name="name" value="java265"> <property name="age" value="888"> </property></property></bean> <bean class="com.java265.Logging" id="logging"> </bean></beans>MainApp 类
package com.java265;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Man man = (Man) context.getBean("man");
man.getName();
man.getAge();
man.throwException();
}
}
运行结果---前置通知
后置通知
返回值为:java265
前置通知
后置通知
返回值为:888
前置通知
抛出异常
后置通知
这里的异常为:java.lang.IllegalArgumentException
Exception in thread "main" java.lang.IllegalArgumentExceptio
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


