jdk方式
package com.kite.dao.jdkproxy; /** * 定義一個person類 * @author admin * */ public class Person { private Long pid; private String pname; public Long getPid() { return pid; } public void setPid(Long pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } }
1 package com.kite.dao.jdkproxy; 2 3 import java.util.List; 4 /** 5 * 定義一個接口 6 * @author admin 7 * 8 */ 9 public interface PersonDao 10 { 11 public void savePerson(); 12 public void updatePerson(); 13 public void deletePerson(); 14 public List<Person> getPerson(); 15 }
package com.kite.dao.jdkproxy; import java.util.ArrayList; import java.util.List; /** * 實現接口 * @author admin * */ public class PersonDaoImpl implements PersonDao { @Override public void savePerson() { System.out.println("save person"); } @Override public void updatePerson() { System.out.println("update person"); } @Override public void deletePerson() { System.out.println("delete person"); } @Override public List<Person> getPerson() { List<Person> list = new ArrayList<Person>(); Person p = new Person(); p.setPname("kite"); p.setPid(1L); list.add(p); for(Person person : list) { System.out.println(person.getPname()); } return null; } }
1 package com.kite.dao.jdkproxy; 2 /** 3 * 模仿事物 4 * @author admin 5 * 6 */ 7 public class Transaction 8 { 9 public void beginTransaction() 10 { 11 System.out.println("begin transaction"); 12 } 13 public void commitTransaction() 14 { 15 System.out.println("commit transaction"); 16 } 17 18 19 }
1 package com.kite.dao.jdkproxy; 2 3 import java.lang.reflect.InvocationHandler; 4 import java.lang.reflect.Method; 5 6 /** 7 * 代理類 8 * @author admin 9 * 10 */ 11 public class PersonDaoInteceptor implements InvocationHandler 12 { 13 14 private Transaction transaction; 15 public Object target; 16 public PersonDaoInteceptor(Object target, Transaction transaction) 17 { 18 this.transaction = transaction; 19 this.target = target; 20 } 21 @Override 22 public Object invoke(Object proxy, Method method, Object[] args) 23 throws Throwable 24 { 25 Object obj; 26 String methodName = method.getName(); 27 if("savePerson".equals(methodName) || "updatePerson".equals(methodName) || "deletePerson".equals(methodName)) 28 { 29 transaction.beginTransaction(); 30 obj = method.invoke(target, args); 31 transaction.commitTransaction(); 32 }else 33 { 34 obj = method.invoke(target, args); 35 } 36 return obj; 37 } 38 39 }
1 package com.kite.dao.jdkproxy; 2 3 import java.lang.reflect.Proxy; 4 5 import org.junit.Test; 6 /** 7 *測試類 8 */ 9 public class PersonDaoTest 10 { 11 @Test 12 public void test() 13 { 14 Transaction transaction = new Transaction(); 15 PersonDao personDao = new PersonDaoImpl(); 16 //攔截器 17 PersonDaoInteceptor interceptor = new PersonDaoInteceptor(personDao, transaction); 18 //得到代理對象 19 PersonDao proxy = (PersonDao) Proxy.newProxyInstance(personDao.getClass().getClassLoader(), personDao.getClass().getInterfaces(), interceptor); 20 //調用目標方法 21 proxy.getPerson(); 22 } 23 }
cglib方式 只須要修改java
1 package com.kite.dao.jdkproxy; 2 3 import java.lang.reflect.InvocationHandler; 4 import java.lang.reflect.Method; 5 6 /** 7 * 代理類 8 * @author admin 9 * 10 */ 11 public class PersonDaoInteceptor implements InvocationHandler 12 { 13 14 private Transaction transaction; 15 public Object target; 16 public PersonDaoInteceptor(Object target, Transaction transaction) 17 { 18 this.transaction = transaction; 19 this.target = target; 20 } 21 @Override 22 public Object invoke(Object proxy, Method method, Object[] args) 23 throws Throwable 24 { 25 Object obj; 26 String methodName = method.getName(); 27 if("savePerson".equals(methodName) || "updatePerson".equals(methodName) || "deletePerson".equals(methodName)) 28 { 29 transaction.beginTransaction(); 30 obj = method.invoke(target, args); 31 transaction.commitTransaction(); 32 }else 33 { 34 obj = method.invoke(target, args); 35 } 36 return obj; 37 } 38 39 }
測試ide
1 package com.kite.dao.cglibproxy; 2 3 import java.lang.reflect.Proxy; 4 5 import org.junit.Test; 6 7 public class PersonDaoTest 8 { 9 @Test 10 public void test() 11 { 12 Transaction transaction = new Transaction(); 13 PersonDao target = new PersonDaoImpl(); 14 //得到代理對象 15 PersonDaoInteceptor inteceptor = new PersonDaoInteceptor(target, transaction); 16 //調用目標方法 17 PersonDaoImpl proxy = (PersonDaoImpl) inteceptor.createProxy(); 18 proxy.getPerson(); 19 } 20 }