Spring AOP 之 Schema配置切面

這篇文章是基於Spring AOP 之 Aspect文章的一個延伸,如果不熟悉如何使用AspectJ annotation去配置切面的話 建議先去看《Spring AOP 之 Aspect》 下面我們就使用一個比較簡單的例子介紹如何使用spring配置文件去配置切面:

定義Aspect類,但是我們不標記任何AspectJ的annotation:

public class AspectExample {

    public void begin(JoinPoint point){
        System.out.println("method name : " + point.getSignature().getName());
    }

}

配置文件配置如下:

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">


    <!-- 業務service -->
    <bean id="userService" class="com.maxfunner.service.impl.UserService" />
    <bean id="adminService" class="com.maxfunner.service.impl.AdminService" />

    <bean id="aspectExample" class="com.maxfunner.aspect.AspectExample" />

    <aop:config proxy-target-class="true">
        <aop:aspect ref="aspectExample">
            <aop:before method="begin" pointcut="execution(* com.maxfunner.service..*(..))" />
        </aop:aspect>
    </aop:config>

</beans>

可以發現這個跟我們使用annotation配置基本上差不多,其理念幾乎相同,非常容易理解。當然我們可以額外的去定義一個pointcut標籤進行引用:

<aop:config proxy-target-class="true">
    <aop:pointcut id="allServiceMethod" expression="execution(* com.maxfunner.service..*(..))" />
    <aop:aspect ref="aspectExample">
        <aop:before method="begin" pointcut-ref="allServiceMethod" />
    </aop:aspect>
</aop:config>

其他基本上大同小異,但是我們在這裏貼出一下引介增強,這個還是比較提別的:

<aop:config proxy-target-class="true">
    <aop:aspect>
        <aop:declare-parents types-matching="com.maxfunner.service.impl.UserService"
                             implement-interface="com.maxfunner.service.AccountSecurity"
                             default-impl="com.maxfunner.service.impl.AccountSecurityImpl" />
    </aop:aspect>
</aop:config>

其調用調用方式基本上與AspectJ annotation配置方式的使用方式一致。


Advisor配置

在之前我們會使用定義一個advisor的類去繼承如StaticMethodMatcherPointcutAdvice的類,進行advisor定義,但是我們可以直接使用aop:advisor去簡化這一切,當然如果需要使用advisor配置,我們需要定義相應的advice進行增強操作:

public class UserAdvice implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(">>>>>>>>>>" + method.getName() + " invoked !!!");
    }
}
下面我們就使用Advisor配置去使用這個advice:

<bean id="userAdvice" class="com.maxfunner.advice.UserAdvice" />
<aop:config proxy-target-class="true">
    <aop:advisor advice-ref="userAdvice" pointcut="execution(* com.maxfunner.service.impl.UserService.login(..))" />
</aop:config>

當然這些操作也是可以混合的去使用,如果你願意的話。


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