JR 精品文章 - Spring AOP 用annotation 来实现
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » 开发框架 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
Spring AOP 用annotation 来实现
rdhj5566 原创   更新:2008-09-17 14:57:43  版本: 1.0   

首先建一个普通Java项目:com.longthsoft.learn.spring

把 spring.jar, commons-logging.jar, cglib-nodep-...jar, aspectjweaver.jar, aspectjrt.jar 放到 Build Path 下.

以止 lib 除了 spring 外, 其他的都可以在 spring 下载包的 lib 中找到

下面编码开始:

让我们先写两个简单的类:

代码
1 package om.longthsoft.learn.spring.models;   
2       
3 public class A {   
4     public void sayHello() {   
5             System.out.println("Hello, I'm a");   
6         }   
7     }  


1 package com.longthsoft.learn.spring.models;   
2 public class B {   
3         public void sayHi() {   
4             System.out.println("Hi, I'm b");   
5         }   
6     }   



没什么实际的东西, 只是小A和小B在打招呼

接下来把他们交给Spring吧(有点残忍)。

代码
 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"  
 4         xmlns:aop="http://www.springframework.org/schema/aop"  
 5         xsi:schemaLocation="   
 6             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
 7             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
 8            
 9 <bean id="a" class="com.longthsoft.learn.spring.models.A" />  
10 <bean id="b" class="com.longthsoft.learn.spring.models.B" />  
11 </beans>  
12 

接下来写个Boot

代码
 1     package com.longthsoft.learn.spring;   
 2       
 3     import org.springframework.context.ApplicationContext;   
 4     import org.springframework.context.support.ClassPathXmlApplicationContext;   
 5       
 6     import com.longthsoft.learn.spring.models.A;   
 7     import com.longthsoft.learn.spring.models.B;   
 8       
 9     public final class Boot {   
10       
11         public static void main(String[] args) {   
12             ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");   
13             A a = (A) ctx.getBean("a");   
14             a.sayHello();   
15                
16             B b = (B) ctx.getBean("b");   
17             b.sayHi();   
18         }   
19       
20     }  
21 

嘿, 这里的运行结果不帖了, 大家脑子里闪过即可。

圣诞到了, 小A小B 介绍完自己之后,也应该说句 "Merry Christmas"

Spring 说, 既然你们交给我, 这等 routine 就不用再麻烦了, 直接一并处理掉。

于是:

代码
 1     package com.longthsoft.learn.spring;   
 2       
 3     import org.aspectj.lang.annotation.AfterReturning;   
 4     import org.aspectj.lang.annotation.Aspect;   
 5     import org.aspectj.lang.annotation.Pointcut;   
 6       
 7     @Aspect  
 8     public class SimpleAspect {   
 9       
10         @Pointcut("execution(* com.longthsoft.learn.spring.models.*.say*())")   
11         public void simplePointcut() { }   
12            
13         @AfterReturning(pointcut="simplePointcut()")   
14         public void simpleAdvice() {   
15             System.out.println("Merry Christmas");   
16         }   
17     }   
18 

然后修改一下配置文件

代码
 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"  
 4         xmlns:aop="http://www.springframework.org/schema/aop"  
 5         xsi:schemaLocation="   
 6             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
 7             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
 8            
 9         <aop:aspectj-autoproxy />  
10            
11         <bean id="a" class="com.longthsoft.learn.spring.models.A" />  
12         <bean id="b" class="com.longthsoft.learn.spring.models.B" />  
13            
14         <bean id="simpleAspect" class="com.longthsoft.learn.spring.SimpleAspect" />  
15     </beans>  
16 

OK, 运行一下:

Hello, I'm a
Merry Christmas
Hi, I'm b
Merry Christmas

版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     6       0
作者其它文章: 作者全部文章
评论人:xzcgeorge 发表时间: Mon Sep 22 06:02:33 CST 2008
不错,简明扼要![good]

这个文章共有 1 条评论
主题: 公用Struts分页 上一篇文章
返回文章列表 返回〔开发框架〕
下一篇文章 主题: 谈谈如何保证数据库操作与文件系统操作一致性


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号