Spring AOP(4)Advisor

"advisor"这个概念来自Spring1.2对AOP的支持,而在AspectJ中没有等价的概念。Advisor表示只有一个通知一个切入点的切面,Advisor可以使用<aop:config>标签下的<aop:advisor>标签定义,下面配置一个前置通知的Advisor:

<aop:config>
    <aop:pointcut id="pointcut" expression="execution (* com.sohu.tv.crm.service.*.*(..))"/>
    <aop:advisor advice-ref="beforeAdvice" pointcut-ref="pointcut" />
</aop:config>
<bean id="beforeAdvice" class="com.sohu.tv.crm.aop.BeforeAdviceImpl"/>
BeforeAdviceImpl类实现MethodBeforeAdvice接口:


public class BeforeAdviceImpl implements MethodBeforeAdvice {
    private static final Logger LOGGER = LoggerFactory.getLogger(BeforeAdviceImpl.class);

    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        LOGGER.info("method name:" + method.getName());
        LOGGER.info("arguments:" + args);
    }
}
这样就实现了前置通知,其他的还包括后置返回通知接口AfterReturningAdvice :
public class AfterReturningAdviceImpl implements AfterReturningAdvice {
    private static final Logger LOGGER = LoggerFactory.getLogger(AfterReturningAdviceImpl.class);

    @Override
    public void afterReturning(Object result, Method method, Object[] args, Object target) throws Throwable {
        LOGGER.info("result:" + result);
    }
}


Advisor大部分情况下和transaction advice一起使用,如下:


<aop:config>
  <aop:pointcut id="businessService"
        expression="execution(* com.xyz.myapp.service.*.*(..))"/>

  <aop:advisor 
      pointcut-ref="businessService"
      advice-ref="tx-advice"/>
      
</aop:config>

<tx:advice id="tx-advice">
  <tx:attributes>
    <tx:method name="*" propagation="REQUIRED"/>
  </tx:attributes>
</tx:advice>



你可能感兴趣的:(spring,AOP,advice)