在使用 <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>
定义切面<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 id="myPointCut"
expression="execution(* net.biancheng.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(* net.biancheng.*.*(..)) 表示匹配 net.biancheng 包中任意类的任意方法。
定义通知
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>
示例
下面使用 Eclipse IDE 演示 AspectJ 基于 XML 开发 AOP,步骤如下:
- 创建 SpringDemo 项目,并在 src 目录下创建 net.biancheng 包。
- 导入 Spring 相关 JAR 包及 Aspectjrt.jar、Aspectjweaver.jar、Aspectj.jar。
- 在 net.biancheng 包下创建 Logging、Man、Beans.xml 和 MainApp。
- 运行 SpringDemo 项目。
Logging 类的代码如下,定义了在各个点要调用的方法。
package net.biancheng;
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 net.biancheng;
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="net.biancheng.Man">
<property name="name" value="bianchengbang" />
<property name="age" value="12" />
</bean>
<bean id="logging" class="net.biancheng.Logging" />
</beans>
MainApp 类代码如下。
package net.biancheng;
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();
}
}
运行结果如下。
前置通知
后置通知
返回值为:bianchengbang
前置通知
后置通知
返回值为:12
前置通知
抛出异常
后置通知
这里的异常为:java.lang.IllegalArgumentException
Exception in thread "main" java.lang.IllegalArgumentException
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/4593.html