在本节中要使用AOP的命名空间,所以你需要导入下面spring aop的框架:
<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>
在你应用程序的CLASSPATH中,你也需要添加AspectJ库的路径。这些库可以在AspectJ的安装路径中的‘lib’目录中活得。除此之外,你还可以从网络中下载这些库:
- aspectjrt.jar
- aspectjweaver.jar
- aspectj.jar
- aopalliance.jar
声明一个切面:一个切面可以使用
标签来声明,后面的bean可以使用ref
来引用。
"myAspect" ref = "aBean">
...
"aBean" class = "...">
...
这里的 aBean 会被配置并依赖注入,就和前面章节中看到的其他Spring bean一样。
声明一个切入点:一个切入点有助于确定使用不同建议执行的感兴趣的连接点(即方法)。在处理基于配置的 XML 架构时,切入点将会按照如下所示定义:
"myAspect" ref = "aBean">
"businessService"
expression = "execution(*com.xyz.myapp.service.*.*(..))"/>
...
"aBean" class = "...">
...
下面的例子定义一个叫 businessService
的切入点,将会匹配Student类中的getName()方法:
"myAspect" ref = "aBean">
"businessService"
expression = "execution(*com.tutorialspoint.Student.getName(..))"/>
...
"aBean" class = "...">
...
你可以声明下面五种任何一种的advice:
<aop:config>
<aop:aspect id = "myAspect" ref = "aBean">
<aop:pointcut id = "businessService"
expression = "execution(* com.xyz.myapp.service.*.*(..))"/>
<aop:before pointcut-ref = "businessService" method = "doRequiredTask"/>
<aop:after pointcut-ref = "businessService" method = "doRequiredTask"/>
<aop:after-returning pointcut-ref = "businessService"
returning = "retVal" method = "doRequiredTask"/>
<aop:after-throwing pointcut-ref = "businessService"
throwing = "ex" method = "doRequiredTask"/>
<aop:around pointcut-ref = "businessService" method = "doRequiredTask"/>
...
aop:aspect>
aop:config>
<bean id = "aBean" class = "...">
...
bean>
你可以使用同一个doRequiredTask
方法,或者为不同的advices使用不同的方法。这些方法将会被定义在aspect模块的一部分。
Student.java
package com.soygrow.XmlAop;
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
System.out.println("Age : " + age);
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
System.out.println("Name : " + name);
return name;
}
public void printThrowException() {
System.out.println("Exception raised");
throw new IllegalArgumentException();
}
}
Logging.java
package com.soygrow.XmlAop;
public class Logging {
/**
* This is the method which I would like to execute
* before a selected method execution.
*/
public void beforeAdvice() {
System.out.println("Going to setup stedent profile.");
}
/**
* This is the method which I would like to execute
* when any method returns.
*/
public void afterAdvice() {
System.out.println("Stedent profile has been setup.");
}
/**
* This is the method which I would like to execute
* when any method returns.
*/
public void afterReturningAdvice(Object retVal) {
System.out.println("Returning: " + retVal.toString());
}
/**
* This is the method which I would like to execute
* if there is an exception raised.
*/
public void AfterThrowingAdvice(IllegalArgumentException ex) {
System.out.println("There has been an exception: " + ex.toString());
}
}
MainApp.java
package com.soygrow.XmlAop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("XmlAopBeans.xml");
Student student = (Student) context.getBean("student");
student.getName();
student.getAge();
student.printThrowException();
}
}
XmlAopBeans.xml
<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">
<aop:config>
<aop:pointcut id="selectAll" expression="execution(* com.soygrow.XmlAop.Student.*(..))"/>
<aop:aspect id="log" ref="logging">
<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="logging" class="com.soygrow.XmlAop.Logging"/>
<bean id="student" class="com.soygrow.XmlAop.Student">
<property name="age" value="11"/>
<property name="name" value="Zara"/>
bean>
beans>
如果一切正常,运行结果:
Going to setup stedent profile.
Name : Zara
Stedent profile has been setup.
Returning: Zara
Going to setup stedent profile.
Age : 11
Stedent profile has been setup.
Returning: 11
Going to setup stedent profile.
Exception raised
Stedent profile has been setup.
There has been an exception: java.lang.IllegalArgumentException
Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0'
aop:pointcut
中execution
中内容不知道是怎么写的,这个问题我在下面会进行补充// 匹配指定包中的所有的方法
execution(* com.xys.service.*(..))
// 匹配当前包中的指定类的所有方法
execution(* UserService.*(..))
// 匹配指定包中的所有 public 方法
execution(public * com.xys.service.*(..))
// 匹配指定包中的所有 public 方法, 并且返回值是 int 类型的方法
execution(public int com.xys.service.*(..))
// 匹配指定包中的所有 public 方法, 并且第一个参数是 String, 返回值是 int 类型的方法
execution(public int com.xys.service.*(String name, ..))
// 匹配指定包中的所有的方法, 但不包括子包
within(com.xys.service.*)
// 匹配指定包中的所有的方法, 包括子包
within(com.xys.service..*)
// 匹配当前包中的指定类中的方法
within(UserService)
// 匹配一个接口的所有实现类中的实现的方法
within(UserDao+)
// 匹配以指定名字结尾的 Bean 中的所有方法
bean(*Service)
// 匹配以 Service 或 ServiceImpl 结尾的 bean
bean(*Service || *ServiceImpl)
// 匹配名字以 Service 结尾, 并且在包 com.xys.service 中的 bean
bean(*Service) && within(com.xys.service.*)
那么基于以上内容,上面例子中aop:pointcut
切入点选择com.soygrow.XmlAop.Student
中定义的所有方法。你也可以将Student
换成,表示XmlAop包下的所有类的所有方法,同时也可以缩小你的切入点使用确切的类以及类中的方法来替代,下面需要修改上面的XML配置表示将类中的一个方法作为切入点:
<aop:pointcut id="selectAll" expression="execution(* com.soygrow.XmlAop.Student.getName(..))"/>
如果一切正常,运行的结果是:
Going to setup stedent profile.
Name : Zara
Stedent profile has been setup.
Returning: Zara
Age : 11
Exception raised
如果将getName
修改成setName
,运行结果是:
Name : Zara
Age : 11
Exception raised
aop:aspect
只是针对那一个方法的setName()
时,aop:aspect
配置没有起作用的时候,因为该方法没有执行,所以运行结果只有那些内容