随笔-10  评论-66  文章-1  trackbacks-0

      经过这段日子的学习和使用Spring,慢慢地体会到Spring的优妙之处,正在深入地吸收Spring的精华,呵呵。现在写的这个只是个简单AOP例子,包括前置通知,后置通知,环绕通知,和目标对象。写这个例子的主要目标只是想让想学AOP的能更快地入门,了解一下如何去配置AOP里面的东东。
目标对象的接口:IStudent.java

 1 /**
 2  * 
 3   */

 4 package  com.dragon.study;
 5
 6 /**
 7  *  @author  dragon
 8  *
 9   */

10 public   interface  IStudent  {
11     
12      public   void  addStudent(String name);
13 }

14


目标类:StudentImpl.java

 1 /**
 2  * 
 3   */

 4 package  com.dragon.study.Impl;
 5
 6 import  com.dragon.study.IStudent;
 7
 8 /**
 9  *  @author  dragon
10  *
11   */

12 public   class  StudentImpl  implements  IStudent {
13
14       public   void  addStudent(String name) {
15          System.out.println( " 欢迎  " + name + "  你加入Spring家庭! " );
16      }

17 }

18



前置通知:BeforeAdvice.java

 1 /**
 2  * 
 3   */

 4 package  com.dragon.Advice;
 5
 6 import  java.lang.reflect.Method;
 7
 8 import  org.springframework.aop.MethodBeforeAdvice;
 9
10 /**
11  *  @author  dragon
12  *
13   */

14 public   class  BeforeAdvice  implements  MethodBeforeAdvice {
15
16        public   void  before(Method method,Object[] args, Object target)
17                 throws  Throwable {
18           
19           System.out.println( " 这是BeforeAdvice类的before方法. " );
20           
21       }

22 }

23

后置通知:AfterAdvice.java
 1/**
 2 * 
 3 */

 4package com.dragon.Advice;
 5
 6import java.lang.reflect.Method;
 7
 8import org.springframework.aop.AfterReturningAdvice;
 9
10/**
11 * @author dragon
12 *
13 */

14public class AfterAdvice implements AfterReturningAdvice{
15    
16    public void afterReturning(Object returnValue ,Method method,
17                   Object[] args,Object target) throws Throwable{
18        System.out.println("这是AfterAdvice类的afterReturning方法.");
19    }

20      
21
22}

23


环绕通知:CompareInterceptor.java

 1/**
 2 * 
 3 */

 4package com.dragon.Advice;
 5
 6import org.aopalliance.intercept.MethodInterceptor;
 7import org.aopalliance.intercept.MethodInvocation;
 8
 9
10/**
11 * @author dragon
12 *
13 */

14public class CompareInterceptor implements MethodInterceptor{
15
16      public Object invoke(MethodInvocation invocation) throws Throwable{
17          Object result = null;
18         String stu_name = invocation.getArguments()[0].toString();
19         if ( stu_name.equals("dragon")){
20             //如果学生是dragon时,执行目标方法,
21              result= invocation.proceed();
22              
23         }
 else{
24             System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");
25         }

26        
27          return result;
28      }

29}

30

配置文件applicationContext.xml
 1<?xml version="1.0" encoding="UTF-8"?>
 2<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 3
 4<beans>
 5
 6<bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
 7<bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
 8<bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor"></bean>
 9<bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>
10
11<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
12  <property name="proxyInterfaces">
13    <value>com.dragon.study.IStudent</value>
14  </property>
15  <property name="interceptorNames">
16    <list>
17     <value>beforeAdvice</value>
18     <value>afterAdvice</value>
19    <value>compareInterceptor</value>  
20    </list>
21  </property>
22  <property name="target">
23    <ref bean="studenttarget"/>
24  </property>
25
26</bean>
27
28
29
30
31</beans>


  现在开始写测试类,Test.java
 1/**
 2 * 
 3 */

 4package com;
 5
 6import org.springframework.context.ApplicationContext;
 7import org.springframework.context.support.FileSystemXmlApplicationContext;
 8
 9import com.dragon.study.IStudent;
10
11/**
12 * @author dragon
13 *
14 */

15public class Test {
16
17    /**
18     * @param args
19     */

20    public static void main(String[] args) {
21        // TODO Auto-generated method stub
22      ApplicationContext ctx = 
23          new FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");
24      
25      IStudent person = (IStudent)ctx.getBean("student");
26      person.addStudent("dragon");
27      
28//      person.addStudent("javadragon");
29    }

30
31}

32
posted on 2006-12-03 03:29 javadragon 阅读(76681) 评论(59)  编辑  收藏

评论:
# re: 一个简单的Spring的AOP例子 2007-02-22 11:41 | freesky_zh
这个类public class BeforeAdvice implements MethodBeforeAdvice
好像有点问题,在Eclipse中会报错。  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2007-02-28 20:34 | javadragon
我又试了次,没有错呀,如果你的还出现错误,
请你把整个工程发给我试下
邮箱:newlong@126.com  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2007-03-10 10:54 | weichenggao
不错,好例子,不过运行该程序,还需要加入commons-logging.jar
请大家注意!  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2007-03-24 00:31 | 鸟不生蛋蛋的地方
Nice,u've done a good job, keep practice, keep thinking, then move forward. God's watching u ,ahahaha~  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2007-03-26 20:25 | javadragon
Thank you! if you don't guided i to how to program,my program capability can't improve. you give me a importnat thing--thinking.thanks again ! i will become stronger. Sorry for my english.haha  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2007-10-11 13:38 | pcz
AfterAdvice

不能在方法执行后 在执行啊!  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2007-10-12 22:06 | javadragon
有什么问题?  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2007-11-12 18:16 | landon
good job  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2008-04-16 20:42 | 00?
好不错,可以运行,,太需要了,谢谢了,,  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2008-04-20 23:01 | huangzongbai
我按照你的方法去做了,可是Advice不能调用~`,很是郁闷.  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2008-07-14 16:59 | zuoshaobiao
不错 不错 终于弄出来了。谢谢了、  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2008-07-30 20:36 | Rain
虽然将Aop这个例子写的不错!能很好的将要经常使用的代码变成一个“方面”
但是注释要加强!  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2008-07-30 20:52 | Rain
在Spring Appactiocontext.xml配置文件;你定义的前置,后置;环绕等通知在配置文件中实现了代理(org.springframework.aop.framework.ProxyFactoryBean)
以此将通知放入到了原Bean中;这样才能使原Bean中方法调用时自动执行通知
这是其一》
<property name="proxyInterfaces">
<property name="interceptorNames">
<property name="target">
这三个属性是一定要配置的
第一是被代理的接口(IStudent)
第二是通知列表(前置,后置;环绕)上面定义的三个类
第三是被代理的原Bean(StudentImpl )

  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2008-08-01 15:08 |
好不错,可以运行,,太需要了,谢谢了,请你把整个工程发给我试下
邮箱:2008-sina@163.com   回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2008-08-27 14:14 | zackey
@freesky_zh
因为没有导入spring.jar  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2008-09-11 10:49 | 啊正
不错啊~~~谢谢LZ分享!!!  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2008-09-24 16:13 | wtf110
我动手做了下,真的好用啊!我感觉明白了点aop!谢谢楼主!!  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2009-02-17 23:59 | anna
good  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2009-02-18 00:12 | wen
@Rain
great, 你的注释是画龙点睛一笔阿  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2009-09-30 11:51 | 淡定
No setter found for property 'target' in class 'org.springframework.aop.framework.ProxyFactoryBean'

在第22行中出现。我加入了aop包啊。为什么找不到
22 <property name="target">
23 <ref bean="studenttarget"/>
24 </property>

  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2009-11-05 16:16 |
运行以后会报异常啊
Error creating bean with name 'student' defined in file [E:\workspace\aop2\src\applicationContext.xml]: Cannot resolve reference to bean 'studenttarger' while setting bean property 'targer'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'studenttarger' is defined  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2009-11-07 19:49 | dragon
@涛
bean id有没有写对?
targer =>target  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2010-02-02 17:19 | Erwin
博主你好!
虽然你的博文已经发表很久了,但是经过一番查找比较,我觉得是最好的!
同时希望能将运行的测试结果贴出来,这样文章才算完整而且方便其他人的阅读。
另:请问能否引用?  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2010-02-10 11:22 | javadragon
@Erwin
可以引用。
最近几个项目都没有用spring,过完年如果有空再整理下,主要还是自己懒,呵呵  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2010-03-10 10:42 | 成亟亟
您好!dragon。
我刚刚接触spring,想跑些小例子熟悉熟悉,由于本人比较愚钝,网上实在是找不到那种step by step的文章,感觉您的文字简洁明了,可是实在是不清楚该怎样把这个例子跑在我的eclipse里,可否把您的工程发给我嘞?谢谢!
scaramouchben@tom.com  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2010-05-03 15:13 | 123
顶,  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2010-07-20 11:56 | 微风
我遇到了和一楼一样的问题,spring.jar包引入了,可还是不行。为什么???  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2010-07-20 16:02 | 微风
搞点了,少了个这个aopalliance-1.0.jar包  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2010-07-27 12:50 | wangsan
3q
  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2010-10-06 20:30 | 查正滨
代码没有任何问题,这个问题我看了好几天,今天终于解决了,谢谢楼主。  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2011-04-12 15:35 | 啊啊啊
代码没有任何问题,也很简单明了,谢谢楼主  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2011-04-13 21:37 | 地痞张三
这个例子很有帮助,谢谢楼主的幸苦劳动  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2011-07-18 14:36 | 产自海南岛的驴肉火烧
小例子很好用,谢谢。  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2011-08-27 09:06 | zhong
代码运行,aop的通知代码没运行出来呢,也没报错  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2011-10-13 16:42 | tesoqop
可以运行,但是aop的通知代码没有运行出来,不知道 为什么。。  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2011-10-13 17:03 | tesoqop
刚才仔细的又看了一遍,发现是自己搞错了。
现在可以啦。afteradvice还没有运行出来。  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2011-10-17 20:12 | ee
如果这就算aop 的话 真的是不难  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2011-11-16 16:44 | 尘埃
不知楼主是否遗忘了这个例子,我看了这个例子后,也运行了,但是我想知道这个程序的运行顺序是什么,就是说在哪配置了它的运行顺序吗(我也没看到啊),为什么就先走before,然后走compareInterceptor,而且这里面怎么去调用impl里的方法的,最后走after?求楼主解答,不胜感激~~  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2011-11-20 19:41 | javadragon
@尘埃
执行顺序是通过继承spring的接口before,afterReturning等来实现的,有兴趣的话,可以去看下spring aop的源码  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2012-04-23 15:07 |
我觉得你这个例子是调用一个方法并打印,如果参数不满足条件,则不调用方法,和不如直接if,else就可以搞定呢?  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2012-11-16 16:54 | 44
4444  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2013-03-25 11:12 | haha
good  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2013-06-03 14:34 | wayne
最好吧jar包列一下 免得有些人不清楚错在哪里  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2013-08-27 10:29 | 对方
@Rain
<property name="proxyInterfaces"> 这个去掉也可以执行目标方法  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2013-08-28 00:35 | GHF
GH   回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2013-11-11 15:49 | jki
@freesky_zh应该还差aopalliance.jar这个jar包  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2014-03-27 18:03 | 凨不止
我是初学者,可以发工程到我邮箱么835060947@qq.com , /thx感谢  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2014-07-06 21:59 | 飞花一夜
很赞的入门例子,一次成功!
ps:这里的编辑器没有复制功能,太讨厌了!  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2014-08-13 15:00 | 你爸
@javadragon
煞笔  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2014-08-19 10:26 | France.
其实我是来学习思想 而不是实现,,呵呵 刚刚学 感觉对aop了解了一点,谢谢楼主  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2014-10-30 14:56 | 游客
不错,简单明了,我个人觉得:如果再注明应该导入的包,会更好。  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2014-12-18 16:39 | king
spring aop 这样我尝试也可以拦截到,不过目前我的项目不是ApplicationContext ctx =
new FileSystemXmlApplicationContext("etc/applicationContext.xml");

ServerController p= (ServerController)ctx.getBean("serverController");
p.test();
这样调用,是直接在controller类中@Autowired注入service,通过调用controller的方法,就是没有拦截到,这是什么原因?  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2015-01-23 10:31 | 1
中国人说什么洋文 草泥马@javadragon
  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2015-04-02 16:58 | Hill
@king
貌似不能用注解,不用注解,用楼主的调用方式就可以了  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2015-06-09 16:52 | 雷锋
继承MethodBeforeAdvice出错, 我的原因是缺少org.aopalliance-1.0.0.jar的包,我是在网上搜的,放进项目后错误消失  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2015-07-29 11:54 |
你这个applicationContext.xml放在哪里 我运行下找不到xml文件   回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2015-11-16 17:08 | 哆啦雷
@杰
不要用FileSystemXmlApplicationContext这个包,

你试试import org.springframework.context.support.ClassPathXmlApplicationContext,

相应的方法也换成ClassPathXmlApplicationContext("applicationContext.xml")  回复  更多评论
  
# re: 一个简单的Spring的AOP例子 2015-11-16 17:14 | 哆啦雷
这是我lib下的几个,都是极易引发缺包错误的包.
也许有多余的,不过多多益善咯

commons-logging-1.0.4.jar
jstl.jar
spring-webmvc.jar
spring.jar
standard.jar  回复  更多评论
  
# re: 一个简单的Spring的AOP例子[未登录] 2016-04-12 21:07 | 111
@鸟不生蛋蛋的地方
wo qu,u english is very good.  回复  更多评论
  

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


网站导航: