spring开发_AOP_代理模式
2012-03-09 15:07
260 查看
项目结构:
http://www.cnblogs.com/hongten/gallery/image/112445.html
执行过程:
用户 -------------- >> 代理对象 -------------->> 目标对象
/AOP/src/com/b510/service/PersonService.java
/AOP/src/com/b510/service/impl/JDKProxyFactory.java
/AOP/src/com/b510/service/impl/PersonServiceBean.java
/AOP/src/com/b510/test/AOPTest.java
运行结果;
我们看到的是:new PersonServiceBean("hello")的时候,传递了参数后,即PersonServiceBean中的user属性的值不为空,那么那些方法save(),update(),getPersonName()
就会被调用,而new PersonServiceBean()的时候,没有传递任何参数,结果是不会调用任何方法的。
http://www.cnblogs.com/hongten/gallery/image/112445.html
执行过程:
用户 -------------- >> 代理对象 -------------->> 目标对象
/AOP/src/com/b510/service/PersonService.java
package com.b510.service; /** * * @author Hongten * */ public interface PersonService { /** * 保存方法 * * @param name * 姓名 */ public void save(String name); /** * 得到person的姓名 * * @param personid * id号 * @return 返回person姓名 */ public String getPersonName(Integer personid); /** * 更新方法 * * @param name * person的姓名 * @param personid * person的id号 */ public void update(String name, Integer personid); }
/AOP/src/com/b510/service/impl/JDKProxyFactory.java
package com.b510.service.impl; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * 当用户要调用目标对象的时候,首先调用的是JDKProxyFactory代理类,代理类会执行invoke方法,在这个调用的过程中, * 我们在把方法的调用委派给目标对象 * * * @author Hongten * */ public class JDKProxyFactory implements InvocationHandler { private Object targetObject; public Object createProxyInstance(Object targetObject) { this.targetObject = targetObject; // 参数一:类装载器 // 参数二:代理对象要实现的所有接口 // 参数三:回调JDKProxyFactory本身实现的接口方法invoke()方法 return Proxy.newProxyInstance(this.targetObject.getClass() .getClassLoader(), this.targetObject.getClass().getInterfaces(), this); } /** * 实现接口中的方法invoke() 参数一:代理对象 参数二:被拦截到的方法 参数三:方法的输入参数 * * 当执行Proxy.newProxyInstance()的时候,会被newProxyInstance()方法中的第三个参数拦截到, * 进入回调invoke()方法 * */ @Override public Object invoke(Object arg0, Method method, Object[] arg) throws Throwable { PersonServiceBean bean = (PersonServiceBean) this.targetObject; Object result = null; //判断bean对象的user属性是否为空,如果为空,则不用把方法委派给目标对象 if (bean.getUser() != null) { // 把方法的调用委派给的创建的目标对象 result = method.invoke(targetObject, arg); } return result; } }
/AOP/src/com/b510/service/impl/PersonServiceBean.java
package com.b510.service.impl; import com.b510.service.PersonService; public class PersonServiceBean implements PersonService { private String user = null; public PersonServiceBean() { } public PersonServiceBean(String user) { this.user = user; } public String getUser() { return this.user; } @Override public String getPersonName(Integer personid) { System.out.println("this is the getPersonName() function"); return "hongten"; } @Override public void save(String name) { System.out.println("this is the save() function"); } @Override public void update(String name, Integer personid) { System.out.println("this is the update() function"); } }
/AOP/src/com/b510/test/AOPTest.java
package com.b510.test; import com.b510.service.PersonService; import com.b510.service.impl.JDKProxyFactory; import com.b510.service.impl.PersonServiceBean; public class AOPTest { public static void main(String[] args) { JDKProxyFactory factory = new JDKProxyFactory(); PersonService service = (PersonService) factory .createProxyInstance(new PersonServiceBean("hello")); service.save("hongten"); service.getPersonName(1); service.update("hello", 2); System.out.println("*************************"); PersonService service1 = (PersonService) factory .createProxyInstance(new PersonServiceBean()); service1.save("hongten"); service1.getPersonName(1); service1.update("hello", 2); System.out.println("end"); } }
运行结果;
this is the save() function this is the getPersonName() function this is the update() function ************************* end
我们看到的是:new PersonServiceBean("hello")的时候,传递了参数后,即PersonServiceBean中的user属性的值不为空,那么那些方法save(),update(),getPersonName()
就会被调用,而new PersonServiceBean()的时候,没有传递任何参数,结果是不会调用任何方法的。
相关文章推荐
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理) --转
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
- 代理模式及spring AOP
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
- Spring-AOP的代理模式
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
- 反射实现AOP动态代理模式(Spring AOP实现原理)
- 反射实现 AOP 动态代理模式实例说明(Spring AOP 的实现 原理)
- Java设计模式之静态代理模式 (Spring Aop)
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
- Spring代理模式及AOP基本术语
- 反射实现AOP动态代理模式(SpringAOP的实现原理)
- 反射实现 AOP 动态代理模式(Spring AOP实现原理)
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
- Spring AOP 学习(一) 代理模式
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
- Spring AOP代理模式
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)