二者区别:
静态代理模式:
灵活性不强;如果有1000个dao;就有1000个代理类;没有做到通用性
静态代理模式:
package StaticProxy;
public interface PersonDao {
public void sava();
public void update();
}
package StaticProxy; public class PersonDaoimpl implements PersonDao { public String user = null; PersonDaoimpl(){} /** * @return the user */ public String getUser() { return user; } /** * @param user the user to set */ public void setUser(String user) { this.user = user; } PersonDaoimpl(String user){ this.user = user; } @Override public void sava() { // TODO Auto-generated method stub System.out.println("进入方法"); } @Override public void update() { // TODO Auto-generated method stub } }
package StaticProxy; import JDKProxy.impl.PersonDaoImpl; //静态代理类 public class personproxy implements PersonDao{ public PersonDaoimpl persondaoimpl; public personproxy(PersonDaoimpl persondaoimpl){ this.persondaoimpl = persondaoimpl; } @Override public void sava() { // TODO Auto-generated method stub if(persondaoimpl.getUser() != null){ persondaoimpl.sava(); } } @Override public void update() { // TODO Auto-generated method stub } }
测试类:
package StaticProxy; public class Test { public static void main(String[] args) { //创建代理类 personproxy pro = new personproxy(new PersonDaoimpl()); pro.sava(); } }
动态代理模式:
JDK : proxy
要求:目标类要有实现接口
package JDKProxy; //接口 public interface PersonDao { public void sava(String name); public void update(String name); public String getUsername(String name); }
package JDKProxy.impl; import JDKProxy.PersonDao; public class PersonDaoImpl implements PersonDao { private String user = null; /** * @return the user */ public String getUser() { return user; } /** * @param user the user to set */ public void setUser(String user) { this.user = user; } public PersonDaoImpl(){} public PersonDaoImpl(String user){ this.user = user; } @Override public String getUsername(String name) { System.out.println("这是getusername()方法"); return "oooo"; } @Override public void sava(String name) { System.out.println("这是save()方法"); } @Override public void update(String name) { System.out.println("这是update()方法"); } }
package JDKProxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import JDKProxy.impl.PersonDaoImpl; /** * 代理工厂 * * @author Administrator * */ public class JdkProxyFactory implements InvocationHandler { private Object targetObject; /** * 生成代理对象 * * @param targetObject * @return */ public Object createProxyObject(Object targetObject) { this.targetObject = targetObject; // 生成代理对象 // loader:目标类的加载器 // interfaces:目标类的所有接口 // 回调接口类 return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(), targetObject.getClass().getInterfaces(), this); } /** * 代理类回调方法 在调用目标类方法的时候进行回调 * 调用目标类方法时进行拦截 */ @Override//环绕通知 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { PersonDaoImpl persondaoimpl = (PersonDaoImpl) this.targetObject; Object invoke = null; try {
package JDKProxy; import JDKProxy.impl.PersonDaoImpl; public class JdkProxyTest { public static void main(String[] args) { //创建动态代理工厂 JdkProxyFactory jdkproxy = new JdkProxyFactory(); //为目标类创建代理对象 PersonDao persondao = (PersonDao) jdkproxy .createProxyObject(new PersonDaoImpl("")); persondao.update("xxx"); } }
//....begin():前置通知 if (persondaoimpl.getUser() != null) { // 委派目标对象调用方法:调用的目标方法称为连接点 invoke = method.invoke(targetObject, args); } //.....commit():后置通知 } catch (Exception e) { //callback():异常通知 }finally{ //close():最终通知 } return invoke; } }
测试: