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>

当然这些操作也是可以混合的去使用,如果你愿意的话。


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