对应于java类的配置方式,同样可以使用xml的方式实现AOP。实现差不多,不多说。
package com.glodon.springdemo7;
import org.aspectj.lang.ProceedingJoinPoint;
public class SimpleProfiler {
public void profilerBefore() throws Throwable {
System.out.println("profiler before");
}
public void profilerAfter() throws Throwable {
System.out.println("profiler after");
}
public void profilerAfterReturning() throws Throwable {
System.out.println("profiler after returning");
}
public void profilerException() throws Throwable {
System.out.println("profiler exception");
}
public Object profilerAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("profiler around start");
Object obj = joinPoint.proceed();
System.out.println("profiler around end");
return obj;
}
}
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="helloService" class="com.glodon.springdemo7.DefaultHelloService"/>
<bean id="profiler" class="com.glodon.springdemo7.SimpleProfiler"/>
<aop:config>
<aop:aspect ref="profiler">
<aop:pointcut id="theExecutionOfSomeFooServiceMethod"
expression="execution(* com.glodon.springdemo7.DefaultHelloService.hello(..))"/>
<aop:before pointcut-ref="theExecutionOfSomeFooServiceMethod" method="profilerBefore"/>
<aop:after-returning pointcut-ref="theExecutionOfSomeFooServiceMethod"
method="profilerAfterReturning"/>
<aop:after pointcut-ref="theExecutionOfSomeFooServiceMethod" method="profilerAfter"/>
<aop:after-throwing pointcut-ref="theExecutionOfSomeFooServiceMethod"
method="profilerException"/>
<aop:around pointcut-ref="theExecutionOfSomeFooServiceMethod" method="profilerAround"/>
</aop:aspect>
</aop:config>
</beans>