Chinaunix首页 | 论坛 | 博客
  • 博客访问: 640547
  • 博文数量: 632
  • 博客积分: 39960
  • 博客等级: 大将
  • 技术积分: 4975
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-16 18:20
文章分类

全部博文(632)

文章存档

2011年(1)

2008年(631)

我的朋友

分类:

2008-10-16 18:22:38

首先建一个普通项目: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   
 2  3  xmlns:xsi=""  
 4         xmlns:aop=""  
 5         xsi:schemaLocation="   
 6              /spring-beans-2.0.xsd   
 7              /spring-aop-2.0.xsd">  
 8            
 9   
10   
11   
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       
 2      3         xmlns:xsi=""  
 4         xmlns:aop=""  
 5         xsi:schemaLocation="   
 6              /spring-beans-2.0.xsd   
 7              /spring-aop-2.0.xsd">  
 8            
 9           
10            
11           
12           
13            
14           
15       
16 

OK, 运行一下:

Hello, I'm a
Merry Christmas
Hi, I'm b
Merry Christmas
【责编:Chuan】

--------------------next---------------------

阅读(236) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~