XMl的配置后执行顺序是
  around之前通知:
  before通知:
  after通知:
  around之后通知:
配置如下:<!--ADVICE-->
<bean id="theAspectAdvice" class="xmlAOP.TheAspect" />
<!--CLASS-->
<bean id="beanTarget" class="xmlAOP.NaiveWaiter" />

<!-- 配置代理,制定目标类,和使用的切面-->
<bean id="factoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>xmlAOP.Waiter</value>
</property>
<property name="target">
<ref local="beanTarget" />
</property>
<property name="interceptorNames">
<list>
<value>theAspect</value>
</list>
</property>
</bean>


<!--ADVISOR-->
<!--Note: An advisor assembles pointcut and advice-->
<bean id="theAspect" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref  local="theAspectAdvice" />
</property>
<property name="pattern">
<value>
xmlAOP\.Waiter\.greetTo
</value>
</property>

<!-- 
<property name="proxyTargetClass" value="false" />
-->
</bean>
AsepctJAOP配置后执行的顺序是: 
  before通知:
  around之前通知:
  after通知:
  around之后通知:配置如下:    <bean id="TheAspect" class="aspectJAOP.TheAspect" />
    <bean id="TheAspect2" class="aspectJAOP.TheAspect2" />
    <bean id="Waiter" class="aspectJAOP.NaiveWaiter" />  <!-- 非动态代理,需要配置切面 -->
   <aop:config ><!-- proxy-target-class="true" -->
        <aop:aspect id="aspectTheAspect2" ref="TheAspect2">
            <!--配置com.spring.service包下所有类或接口的所有方法-->
            <aop:pointcut id="businessService"
                expression="within(aspectJAOP.*Waiter)" /> 
                <!-- 
                  其他几种配置
                execution(* aspectJAOP.Waiter.*(..))
                expression="within(aspectJAOP.*Waiter)"
                expression="target(aspectJAOP.Waiter)"  只有target可以指定接口或接口的实现类,within 的指定必须是实现类。即不能这样:within(aspectJAOP.Wait*)用抽象接口名 
                -->
            <aop:before pointcut-ref="businessService" method="doBefore"/>
            <aop:after pointcut-ref="businessService" method="doAfter"/>
            <aop:around pointcut-ref="businessService" method="doAround"/>
            <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>
        </aop:aspect>
    </aop:config>
直接写Java代码不用配置文件的执行顺序是:
  before通知:
  around之前通知:
  around之后通知:
  after通知:直接写的Java代码如下:
AspectJProxyFactory factory = new AspectJProxyFactory();
Waiter target = new NaiveWaiter();
factory.setTarget(target);
factory.addAspect(TheAspect.class);
target = factory.getProxy();
target.greetTo("fc");

解决方案 »

  1.   

    如果有多个通知想要在同一连接点运行会发生什么?Spring AOP遵循跟AspectJ一样的优先规则来确定通知执行的顺序。 在“进入”连接点的情况下,最高优先级的通知会先执行(所以给定的两个前置通知中,优先级高的那个会先执行)。 在“退出”连接点的情况下,最高优先级的通知会最后执行。(所以给定的两个后置通知中, 优先级高的那个会第二个执行)。当定义在不同的切面里的两个通知都需要在一个相同的连接点中运行, 那么除非你指定,否则执行的顺序是未知的。你可以通过指定优先级来控制执行顺序。 在标准的Spring方法中可以在切面类中实现org.springframework.core.Ordered 接口或者用Order注解做到这一点。在两个切面中, Ordered.getValue()方法返回值(或者注解值)较低的那个有更高的优先级。 当定义在相同的切面里的两个通知都需要在一个相同的连接点中运行, 执行的顺序是未知的(因为这里没有方法通过反射javac编译的类来获取声明顺序)。 考虑在每个切面类中按连接点压缩这些通知方法到一个通知方法,或者重构通知的片段到各自的切面类中 - 它能在切面级别进行排序。