`
lichaozhangobj
  • 浏览: 99021 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java 动态代理

阅读更多

PersonService 接口

public interface PersonService {

	@SuppressWarnings("unchecked")
	public abstract List getAllPerson();

}

 PersonSercieImpl

public class PersonServiceImpl implements PersonService {

	@SuppressWarnings("unchecked")
	public List getAllPerson(){
		System.out.println("查询全部");
		return null;
	}
}

 JdkProxyFactory

public class JdkProxyFactory implements InvocationHandler {

	private Object targetObject;

	public Object createProxyInstance(Object obj) {
		this.targetObject = obj;
		return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),
				this.targetObject.getClass().getInterfaces(), this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Object result = null;
		System.err.println("---操作1---");
		result = method.invoke(targetObject, args);
		System.err.println("---操作2---");
		return result;
	}
}

 MainTest

public class MainTest {
	
	public static void main(String[] args) {
		
		PersonService personService = new PersonServiceImpl();
		
		JdkProxyFactory jdkproxy = new JdkProxyFactory();
		PersonService personServiceProxy = (PersonService)jdkproxy.createProxyInstance(personService);
		personServiceProxy.getAllPerson();
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics