我的输出结果(代码是一样的):
MoocAspect before.
MoocAspect aroud 1.
AspectBiz biz.
MoocAspect afterReturning.
MoocAspect after.
MoocAspect aroud 2.
因为是随机的,你可以用order设置顺序,详见spring文档。文档中的finally仅仅表示一定会被执行,并不代表最后被执行
public void before()
{
System.out.println("MoocAspect before.");
}
public void afterReturning()
{
System.out.println("MoocAspect afterReturning.");
}
public void afterThrowing()
{
System.out.println("MoocAspect afterThrowing.");
}
public void after()
{
System.out.println("MoocAspect after.");
}
public Object around(ProceedingJoinPoint pjp)
{
Object obj = null;
try
{
System.out.println("MoocAspect aroud 1.");
obj = pjp.proceed();
System.out.println("MoocAspect aroud 2.");
} catch (Throwable e)
{
e.printStackTrace();
}
return obj;
}
你定义的顺序和结果是一样的么?你看一下你的定义顺序,都是按定义的顺序执行的。
配置文件如下:
<?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.1.xsd
">
<bean id="moocAspect" class="com.yxq.aop.MoocAspect"></bean>
<bean id="aspectBiz" class="com.yxq.aop.AspectBiz"></bean>
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:pointcut expression="execution(* com.yxq.aop.*Biz.*(..))" id="moocPointcut"/>
<aop:before method="before" pointcut-ref="moocPointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="moocPointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="moocPointcut"/>
<aop:after method="after" pointcut-ref="moocPointcut"/>
<aop:around method="around" pointcut-ref="moocPointcut"/>
</aop:aspect>
</aop:config>
</beans>
你的配置文件也要和视频的一样