( 十七 ) Spring AOP:基于AspectJ XML开发
( 十七 ) Spring AOP:基于AspectJ XML开发
1、简介
基于 XML 的声明式是指通过 Spring 配置文件的方式来定义切面、切入点及通知,而所有的切面和通知都必须定义在 <aop:config> 元素中。
在使用 <aop:config> 元素之前,我们需要先导入 Spring 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>
2、定义切面<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。
3、定义切入点<aop:pointcut>
<aop:pointcut> 用来定义一个切入点,当 <aop:pointcut>元素作为 <aop:config> 元素的子元素定义时,表示该切入点是全局切入点,它可被多个切面所共享;当 <aop:pointcut> 元素作为 <aop:aspect> 元素的子元素时,表示该切入点只对当前切面有效。
<aop:config>
<aop:pointcut id="myPointCut"
expression="execution(* com.dw.study.service.*.*(..))"/>
</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.dw.*.*(..))
表示匹配 com.dw包中任意类的任意方法。
4、定义通知
AspectJ 支持 5 种类型的 advice,如下:
<aop:aspect id="myAspect" ref="aBean">
<!-- 前置通知 -->
<aop:before pointcut-ref="myPointCut" method="..."/>
<!-- 后置通知 -->
<aop:after-returning pointcut-ref="myPointCut" method="..."/>
<!-- 环绕通知 -->
<aop:around pointcut-ref="myPointCut" method="..."/>
<!-- 异常通知 -->
<aop:after-throwing pointcut-ref="myPointCut" method="..."/>
<!-- 最终通知 -->
<aop:after pointcut-ref="myPointCut" method="..."/>
....
</aop:aspect>
5、示例
Logging 类的代码如下,定义了在各个点要调用的方法:
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 类的代码如下:
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: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 ">
<aop:config>
<aop:aspect id="log" ref="logging">
<aop:pointcut id="selectAll" expression="execution(* net.biancheng.*.*(..))" />
<aop:before pointcut-ref="selectAll" method="beforeAdvice" />
<aop:after pointcut-ref="selectAll" method="afterAdvice" />
<aop:after-returning pointcut-ref="selectAll" returning="retVal" method="afterReturningAdvice" />
<aop:after-throwing pointcut-ref="selectAll" throwing="ex" method="afterThrowingAdvice" />
</aop:aspect>
</aop:config>
<bean id="man" class="com.dw.study.Man">
<property name="name" value="test" />
<property name="age" value="12" />
</bean>
<bean id="logging" class="com.dw.study.Logging" />
</beans>
MainApp 类代码如下:
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();
}
}
运行结果如下:
前置通知
后置通知
返回值为:test
前置通知
后置通知
返回值为:12
前置通知
抛出异常
后置通知
这里的异常为:java.lang.IllegalArgumentException
Exception in thread "main" java.lang.IllegalArgumentException