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();
}
}