Spring重點類/標籤介紹

MethodBeforeAdvice

Spring AOP 通知,配置它的類會在方法執行之前執行。

AfterReturningAdvice

Spring AOP 通知,配置它的類會在方法執行之後執行。

ThrowsAdvice

Spring AOP 通知,配置它的類會在異常拋出之後執行。

MethodInterceptor

它結合了上面的三個通知,在方法執行過程中執行。創建一個實現了MethodInterceptor接口的類。必須調用“methodInvocation.proceed();” 繼續在原來的方法執行,否則原來的方法將不會執行。

ProxyFactoryBean

代理類。

RegexpMethodPointcutAdvisor

可通過正則表達式匹配需要的方法,並對匹配到的方法配置AOP通知。

BeanNameAutoProxyCreator

可通過正則表達式匹配需要的bean,然後對這些bean進行代理。

DefaultAdvisorAutoProxyCreator

這個 DefaultAdvisorAutoProxyCreator 是非常強大的,如果有任何的 bean 可相提並論,Spring會自動創建一個代理。

aop:config

  1. 先啓用AspectJ
<aop:aspectj-autoproxy />
  1. Spring AOP+AspectJ在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-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy />

<bean id="customerBo" class="com.yiibai.customer.bo.impl.CustomerBoImpl" />

<!-- Aspect -->
<bean id="logAspect" class="com.yiibai.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect">

    <!-- @Before -->
    <aop:pointcut id="pointCutBefore"
      expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))" />

    <aop:before method="logBefore" pointcut-ref="pointCutBefore" />

    <!-- @After -->
    <aop:pointcut id="pointCutAfter"
       expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))" />

    <aop:after method="logAfter" pointcut-ref="pointCutAfter" />

    <!-- @AfterReturning -->
    <aop:pointcut id="pointCutAfterReturning"
       expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerReturnValue(..))" />

    <aop:after-returning method="logAfterReturning"
      returning="result" pointcut-ref="pointCutAfterReturning" />

    <!-- @AfterThrowing -->
    <aop:pointcut id="pointCutAfterThrowing"
      expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerThrowException(..))" />

    <aop:after-throwing method="logAfterThrowing"
      throwing="error" pointcut-ref="pointCutAfterThrowing" />

    <!-- @Around -->
    <aop:pointcut id="pointCutAround"
      expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerAround(..))" />

    <aop:around method="logAround" pointcut-ref="pointCutAround" />

  </aop:aspect>

</aop:config>

</beans>

TransactionInterceptor

事務管理。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章