|
真爱的事业和真正的爱情一生只有一次,都值得我们温柔地相待,因为那种感觉是永远都无法复制的, 这世界真正属于你的东西其实并不多,你不好好珍惜,它便会离你而去,包括机遇,包括爱情,包括生命。 不要找任何理由, 当幸福在你身边的时候就抓住它,你就一定会很幸福! |
时 间 记 忆 |
« | October 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | |
|
blog名称:玻璃杯中的花生壳 日志总数:162 评论数量:249 留言数量:1 访问次数:829318 建立时间:2004年11月4日 |
 | | |
|
|
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! |
|
运行的过程中发生错误,没有找到applicationContext.xml文件,查找了一下原因,发生将applicationContext.xml文件放错了位置,放在packge下,正确的应该是放在src目录下 |
| » 1 »
| | |
|