首页(162) xml(5) spring(1) 生活(8) java(70) 代码(10) 英语(4) 数据库(7) c#(14) 成长(10) 软件工程(27)  写新日志
 
 

玻璃杯中的花生壳

  真爱的事业和真正的爱情一生只有一次,都值得我们温柔地相待,因为那种感觉是永远都无法复制的, 这世界真正属于你的东西其实并不多,你不好好珍惜,它便会离你而去,包括机遇,包括爱情,包括生命。
   不要找任何理由,  当幸福在你身边的时候就抓住它,你就一定会很幸福! 
   

时 间 记 忆
«September 2025»
123456
78910111213
14151617181920
21222324252627
282930

最 新 评 论
回复:xml的Jdom解析过程详解
回复:突然想到的几句话!
 Boyle came out of n
回复:xml的Jdom解析过程详解
回复:配置Spring数据源
回复:使用SAX解析XML
回复:java中写文件操作时FileOu
回复:关联和依赖关系的区分
回复:HttpSessionListen
回复:Spring AOP四种创建通知(

最 新 日 志
Java开发者的十大戒律
配置Spring数据源
java多线程设计模式
java中switch的使用
性格,编码,测试
突然想到的几句话!
理解Spring AOP中的关键概念
Spring AOP四种创建通知(拦截器
xml的四种解析方法 比较 sax,do
xml的Jdom解析过程详解

最 新 留 言
签写新留言

我渴望知识
很好的东东
帖子不错,道声谢
想拜师学艺
我的呼喊

搜 索


用 户 登 录
用户名称:
登陆密码:
密码保存:

友 情 连 接

模板设计:部落窝模板世界

blog名称:玻璃杯中的花生壳
日志总数:162
评论数量:249
留言数量:1
访问次数:826995
建立时间:2004年11月4日
 
 
 
[java]Spring AOP之Hello World
[ 2006/9/11 14:32:04 | By: 玻璃杯中的花生壳 ]
 
  1.首先我们来创建一个自己的interceptor   这个类必须继承org.aopalliance.intercept. MethodInterceptor接口。Spring的AOP框架就是参照aopalliance这个标准实现的,所以我们的MyInterceptor要继承这个标准中的接口。  这个接口只有一个要求实现的方法:  public Object invoke(MethodInvocation methodInvocation) throws Throwable;  下面是我们的MyIntercptor:    public class MyInterceptor implements MethodInterceptor {  private final Log logger = LogFactory.getLog(getClass());     public Object invoke(MethodInvocation methodInvocation) throws Throwable {  logger.info("Beginning method (1): " +   methodInvocation.getMethod().getDeclaringClass() + "." +   methodInvocation.getMethod().getName() + "()");  long startTime = System.currentTimeMillis();  try{  Object result = methodInvocation.proceed();  return result;  }finally{  logger.info("Ending method (1): " +   methodInvocation.getMethod().getDeclaringClass() + "." +  methodInvocation.getMethod().getName() + "()");  logger.info("Method invocation time (1): " +   (System.currentTimeMillis() - startTime) + " ms.");  }  }  }    对于上面的代码需要说明的是下面两行代码:  Object result = methodInvocation.proceed();  return result;  整个程序的流程是这样的:  1,先是执行在Object result = methodInvocation.proceed();前面的代码;  2,接着执行Object result = methodInvocation.proceed();,它把执行控制权交给了interceptor stack(拦截器栈)内的下一个interceptor,如果没有了就交给真正的业务方法;  3,然后执行return result;之前的代码;  4,最后执行return result;,它把控制权交回它之上的interceptor,如果没有了就退出interceptor stack。    2.写出我们的业务对象及其接口  为了方便我们的业务接口只有一个hello方法:    public interface BusinessInterface {  public void hello();  }     业务对象的代码如下:    public class BusinessInterfaceImpl implements BusinessInterface{  public void hello() {  System.out.println("hello Spring AOP.");  }  }     3.接下来,我们来看看如何使用我们的写的interceptor  我们把业务对象作为AOP的target:  <bean id="businessTarget" class="com.rst.spring.testaop.BusinessInterfaceImpl"/>  接着在bean定义中声明interceptor:  <bean id="myInterceptor" class="com.rst.spring.testaop.MyInterceptor"/>  最后,我们来声明真正的业务对象,通过使用它的接口以及Spring的ProxyFactoryBean:    <bean id="businessBean"    class="org.springframework.aop.framework.ProxyFactoryBean">  <property name="proxyInterfaces">  <value>com.rst.spring.testaop.BusinessInterface</value>  </property>  <property name="interceptorNames">  <list>  <value>myInterceptor</value>  <value>businessTarget</value>  </list>  </property>  </bean>     这里需要说明两点:  proxyInterfaces:就是我们的业务对象的实际接口;  interceptorNames:定义了所有interceptors的执行顺序,其中业务对象的target作为list的最后一个。记着一定要把业务对象的target放到list中,否则你的业务对象就不会工作。    4.最后,写我们的测试类  ClassPathResource resource =   new ClassPathResource("com/rst/spring/testaop/aop_bean.xml");  XmlBeanFactory beanFactory = new XmlBeanFactory(resource);  BusinessInterface businessBean =   (BusinessInterface) beanFactory.getBean("businessBean");  businessBean.hello();     一切正常就可以在log上看到相应的信息了。  以下是附件源代码的执行效果:  2004-09-08 16:04:51,210 INFO - Beginning method (1): interface com.rst.spring.testaop.BusinessInterface.hello()  2004-09-08 16:04:51,210 INFO - Beginning method (2): interface com.rst.spring.testaop.BusinessInterface.hello()  hello Spring AOP.  2004-09-08 16:04:51,210 INFO - Ending method (2): interface com.rst.spring.testaop.BusinessInterface.hello()  2004-09-08 16:04:51,210 INFO - Ending method (1): interface com.rst.spring.testaop.BusinessInterface.hello()  2004-09-08 16:04:51,210 INFO - Method invocation time (1): 0 ms.  源代码需要spring.jar, aopallience.jar, commons-logging.jar。
 

阅读全文(3345) | 回复(0) | 编辑 | 精华
 

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
 
部落窝Blog模板世界部落窝Blog模板世界
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.031 second(s), page refreshed 144784044 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号