使用JDK中的Proxy技术实现AOP功能
2012-08-05 16:12
721 查看
@Test public void proxyTest() { JDKProxyFactory factory = new JDKProxyFactory(); PeopleService peopleService = (PeopleService) factory.createProxyInstance(new PeopleServiceBean("张三")); peopleService.save("hello"); }
package com.tiger.aop; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import com.tiger.service.impl.PeopleServiceBean; public class JDKProxyFactory implements InvocationHandler { private Object targetObject; public Object createProxyInstance(Object targetObject) { this.targetObject = targetObject; return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(), this.targetObject.getClass().getInterfaces(), this); } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { PeopleServiceBean bean = (PeopleServiceBean) this.targetObject; Object result = null; if(bean.getUser() != null) { result = method.invoke(targetObject, args); } return result; } }
package com.tiger.service; public interface PeopleService { public void save(String name); public void update(String name,Integer peopleid); public String getPeopleName(Integer peopleid); }
package com.tiger.service.impl; import com.tiger.service.PeopleService; public class PeopleServiceBean implements PeopleService { private String user = null; public PeopleServiceBean(){} public PeopleServiceBean(String user) { this.user = user; } public void save(String name) { System.out.println("我是save()方法"); } public void update(String name, Integer peopleid) { System.out.println("我是update()方法"); } public String getPeopleName(Integer peopleid) { System.out.println("我是getPeopleName()方法"); return "xxx"; } public String getUser() { return user; } }
结果:
我是save()方法
相关文章推荐
- 使用JDK中的Proxy技术实现AOP功能
- 15_传智播客Spring2.5视频教程_使用JDK中的Proxy技术实现AOP功能
- 使用JDK中的proxy技术实现AOP功能
- 使用JDK中的Proxy技术实现AOP功能
- 15_传智播客Spring2.5视频教程_使用JDK中的Proxy技术实现AOP功能 2
- 15_传智播客Spring2.5视频教程_使用JDK中的Proxy技术实现AOP功能 3
- 使用JDK中的Proxy技术实现AOP功能[动态代理]
- (8) 使用JDK中的Proxy技术实现AOP功能 以及 CGLIB生成代理
- (转)使用JDK中的Proxy技术实现AOP功能
- 使用JDK中的Proxy技术实现AOP功能
- ITCAST视频-Spring学习笔记(使用JDK中的Proxy技术实现AOP功能)
- (9) 使用JDK中的Proxy技术实现AOP功能 以及 CGLIB生成代理
- JDK中的Proxy技术实现AOP功能
- JDK中的Proxy技术实现AOP功能
- AOP_1.入门及使用JDK中的Proxy技术实现
- JDK中的Proxy技术实现AOP功能
- JDK核心API:使用动态代理实现AOP功能
- 编码实现用JDK中的Proxy实现springAOP功能
- 使用jdk和cglib实现spring的aop功能
- 使用jdk和cglib实现spring的aop功能