相比較于使用annotation去配置,使用XML去配置AOP反而簡單些,無需在切面類中為每個通知方法定義切入點表達式,切面類簡潔且不受代碼入侵,非常的靈活。
切面類,去除掉和AOP有關(guān)的注解,變得非常簡潔
1 package org.duyt.autoProxy;
2
3 import org.aspectj.lang.JoinPoint;
4 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.duyt.util.Logger;
6 import org.springframework.stereotype.Component;
7
8 @Component("loggerAspect")
9 public class LoggerAspect {
10 public void loggerBefore(JoinPoint jp) {
11 Logger.info("前置切入點:execute==>" + jp.getTarget() + "==>"
12 + jp.getSignature().getName() + " method");
13 }
14 public void loggerAfter(JoinPoint jp) {
15 Logger.info("后置切入點:execute==>" + jp.getTarget() + "==>"
16 + jp.getSignature().getName() + " method");
17 }
18 public void loggerAround(ProceedingJoinPoint pjp) throws Throwable {
19 Logger.info("環(huán)繞開始切入點:execute==>" + pjp.getTarget() + "==>"
20 + pjp.getSignature().getName() + " method");
21 pjp.proceed();
22 Logger.info("環(huán)繞結(jié)束切入點:execute==>" + pjp.getTarget() + "==>"
23 + pjp.getSignature().getName() + " method");
24 }
25 }
26
beans.xml變更為以下的配置
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7 http://www.springframework.org/schema/context
8 http://www.springframework.org/schema/context/spring-context-3.0.xsd
9 http://www.springframework.org/schema/aop
10 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
11
12 <!-- 開啟注解 -->
13 <context:annotation-config />
14 <!-- 指定哪些需要加入掃描 -->
15 <context:component-scan base-package="org.duyt.*" />
16
17 <!-- 開啟AOP切面自動代理 -->
18 <!-- 無須再開啟 -->
19 <!-- <aop:aspectj-autoproxy /> -->
20
21 <!-- 使用xml配置AOP實現(xiàn) -->
22 <aop:config>
23 <!-- 定義切面類并引用 -->
24 <aop:aspect id="loggerAspect" ref="loggerAspect">
25 <!-- 定義切入點 -->
26 <aop:pointcut id="loggerPointcut"
27 expression="execution (* org.duyt.dao.*.add*(..))||
28 execution (* org.duyt.dao.*.delete*(..))||
29 execution (* org.duyt.dao.*.update*(..))" />
30 <!-- 定義通知,前置 -->
31 <aop:before method="loggerBefore" pointcut-ref="loggerPointcut"/>
32 <!-- 后置 -->
33 <aop:after method="loggerAfter" pointcut-ref="loggerPointcut"/>
34 <!-- 環(huán)繞 -->
35 <aop:around method="loggerAround" pointcut-ref="loggerPointcut"/>
36 </aop:aspect>
37 </aop:config>
38
39 </beans>
測試類
1 package org.duyt.test;
2
3 import org.duyt.action.UserAction;
4 import org.junit.Test;
5 import org.springframework.beans.factory.BeanFactory;
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8 public class IocTest {
9
10 private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
11
12 @Test
13 public void test(){
14 //基于XML的AOP
15 UserAction ua = (UserAction) factory.getBean("userAction");
16 ua.addUser();
17 }
18
19 }
20
結(jié)果和使用annotation無異:
前置切入點:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method
環(huán)繞開始切入點:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method
用戶增加方法
后置切入點:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method
環(huán)繞結(jié)束切入點:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method