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

玻璃杯中的花生壳

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

时 间 记 忆
«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031

最 新 评 论
回复: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
访问次数:829318
建立时间:2004年11月4日
 
 
 
[java]spring 中的拦截器
[ 2006/8/4 16:11:53 | By: 玻璃杯中的花生壳 ]
 
1.自定义被拦截的接口,且实现接口。代码: 500)this.width=500'>package demo;500)this.width=500'>public interface UserService //被拦截的接口500)this.width=500'>500)this.width=500'>500)this.width=500'>{500)this.width=500'> public void printUser(String user);500)this.width=500'>}500)this.width=500'>500)this.width=500'>500)this.width=500'>package demo;500)this.width=500'>public class UserServiceImp extends UserService //实现UserService接口500)this.width=500'>500)this.width=500'>500)this.width=500'>{500)this.width=500'>  public void printUser(String user)500)this.width=500'>500)this.width=500'>  500)this.width=500'>{500)this.width=500'>   System.out.println("printUser user:"+user);//显示user 500)this.width=500'>  }500)this.width=500'>}2 实现AOP的方法级拦截器。它可以在目标操作前后执行,拦截自定义接口的参数,或拦截接口返回的值。代码: 500)this.width=500'>package demo;500)this.width=500'>import org.aopalliance.intercept.MethodInterceptor;500)this.width=500'>import org.aopalliance.intercept.MethodInvocation;500)this.width=500'>500)this.width=500'>public class UserInterceptor implements MethodInterceptor500)this.width=500'>       //AOP方法拦截器500)this.width=500'>500)this.width=500'>       500)this.width=500'>{500)this.width=500'>500)this.width=500'>500)this.width=500'>    public Object invoke(MethodInvocation arg0) throws Throwable 500)this.width=500'>{500)this.width=500'>    500)this.width=500'>500)this.width=500'>        try 500)this.width=500'>{500)this.width=500'>            500)this.width=500'>            if (arg0.getMethod().getName().equals("printUser"))500)this.width=500'>              //拦截方法是否是UserService接口的printUser方法 500)this.width=500'>500)this.width=500'>            500)this.width=500'>{500)this.width=500'>                 Object[] args = arg0.getArguments();//被拦截的参数500)this.width=500'>                 System.out.println("user:"+args[0]);500)this.width=500'>                 arg0.getArguments()[0]="hello!" //修改被拦截的参数500)this.width=500'>                500)this.width=500'>                500)this.width=500'>            }500)this.width=500'>            500)this.width=500'>            System.out.println(arg0.getMethod().getName() + "---!");500)this.width=500'>                        return arg0.proceed();//运行UserService接口的printUser方法500)this.width=500'>500)this.width=500'>500)this.width=500'>        } catch (Exception e) 500)this.width=500'>{500)this.width=500'>            throw e;500)this.width=500'>        }500)this.width=500'>    }3. ApplicationContext.xml的设置  500)this.width=500'><?xml version="1.0" encoding="UTF-8"?>500)this.width=500'>   <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">500)this.width=500'>   500)this.width=500'>   <beans>500)this.width=500'>    <bean id="userServiceImp" class="demo.UserServiceImp”>500)this.width=500'>   </bean>500)this.width=500'>   500)this.width=500'> 500)this.width=500'>  500)this.width=500'>  <bean id="userInterceptor" class="demo.UserInterceptor">500)this.width=500'>  </bean>500)this.width=500'>  500)this.width=500'> 500)this.width=500'>  500)this.width=500'>  <bean id="userService" 500)this.width=500'>      class="org.springframework.aop.framework.ProxyFactoryBean">500)this.width=500'>      <property name="proxyInterfaces"><value>demo.UserService</value></property>500)this.width=500'>  500)this.width=500'>      <property name="target"><ref local="userServiceImp"/></property>500)this.width=500'>      <property name="interceptorNames">500)this.width=500'>          <list>500)this.width=500'>              <value>userInterceptor</value>500)this.width=500'>          </list>500)this.width=500'>      </property>500)this.width=500'>  </bean>500)this.width=500'>  500)this.width=500'> </beans>500)this.width=500'>4.运行 500)this.width=500'>package demo;500)this.width=500'> import org.springframework.context.ApplicationContext;500)this.width=500'> import org.springframework.context.support.FileSystemXmlApplicationContext;500)this.width=500'> 500)this.width=500'> public class DemoApp500)this.width=500'>500)this.width=500'> 500)this.width=500'>{500)this.width=500'>  public static void main(String[] args)500)this.width=500'>500)this.width=500'>  500)this.width=500'>{500)this.width=500'>         ApplicationContext ac = new FileSystemXmlApplicationContext(500)this.width=500'>                "classpath:ApplicationContext.xml");500)this.width=500'>    UserService us = (UserService) ac.getBean("userService");500)this.width=500'>     String user="zhao";500)this.width=500'>500)this.width=500'>     us.printUser("zhao");500)this.width=500'>500)this.width=500'>  }500)this.width=500'> } 运行结果: [java]  user:zhao [java]  printUser user:hello!
 

阅读全文(2612) | 回复(1) | 编辑 | 精华
 
 
回复:spring 中的拦截器
[ 2006/8/4 16:53:22 | By: 玻璃杯中的花生壳 ]
 
运行的过程中发生错误,没有找到applicationContext.xml文件,查找了一下原因,发生将applicationContext.xml文件放错了位置,放在packge下,正确的应该是放在src目录下
 

个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 
» 1 »

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

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