温故知新:spring_07基于XML配置AOP

相比较于使用annotation去配置,使用XML去配置AOP反而简单些,无需在切面类中为每个通知方法定义切入点表达式,切面类简洁且不受代码入侵,非常的灵活。

切面类,去除掉和AOP有关的注解,变得非常简洁
 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("环绕开始切入点:execute==>" + pjp.getTarget() + "==>"
20                 + pjp.getSignature().getName() + " method");
21         pjp.proceed();
22         Logger.info("环绕结束切入点: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实现 -->
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             <!-- 环绕 -->
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 
结果和使用annotation无异:
前置切入点:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method
环绕开始切入点:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method
用户增加方法
后置切入点:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method
环绕结束切入点:execute==>org.duyt.dao.impl.UserDao@af08a49==>add method

posted on 2014-11-05 22:45 都较瘦 阅读(312) 评论(0)  编辑  收藏 所属分类: containerFramework


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

公告

博客定位:囿于目前的水平,博客定位在记录自己的学习心得和随手的练习

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜