簡單動態代理例項(使用jdk的proxy)
阿新 • • 發佈:2019-01-25
在持久層DAO層中,對資料庫操作有影響的增、刪、改都需要開啟事物,並在執行完增刪改之後要提交事物。但如果每個方法都這樣寫的話,那麼程式碼變顯得冗餘,採用動態代理的方式來實現這種需求的話,可以使程式碼更清晰簡潔,並且將事物和資料操作解耦。下面是簡單的模擬一下此案例,沒有真正的資料庫連線和事物,只是簡單的模擬。
2 . Transaction.java
4. PersonDaoImpl.java
6. 測試類PersonDaoTest.java
7.結果
1.Person.java
package cn.it.spring.proxy.dao;
public class Person {
private Integer pid;
private String pnameString;
//省略set和get方法,還有建構函式
}
2 . Transaction.java
package cn.it.spring.proxy.dao; /** * 模擬事物類 * @author Administrator * */ public class Transaction { /** * 開啟事物 */ public void beginTransaction(){ System.out.println("beginTransaction"); } /** * 提交事物 */ public void commit(){ System.out.println("commit"); } }
3. PersonDao.java
package cn.it.spring.proxy.dao;
import java.util.List;
public interface PersonDao {
public void savePerson();
public void deletePerson();
public void updatePerson();
public List<Person> getPersons();
}
4. PersonDaoImpl.java
package cn.it.spring.proxy.dao; import java.util.ArrayList; import java.util.List; public class PersonDaoImpl implements PersonDao { @Override public void savePerson() { System.out.println("savePerson"); } @Override public void deletePerson() { System.out.println("deletePerson"); } @Override public void updatePerson() { System.out.println("updatePerson"); } @Override public List<Person> getPersons() { List<Person> persons = new ArrayList<>(); Person person1 = new Person(1, "person1"); Person person2 = new Person(2, "person2"); persons.add(person1); persons.add(person2); return persons; } }
5.MyInterceptor.java
package cn.it.spring.proxy.dao; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class MyInterceptor implements InvocationHandler { private Object target; private Transaction transaction; public MyInterceptor(Object target, Transaction transaction) { super(); this.target = target; this.transaction = transaction; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().equals("savePerson") || method.getName().equals("deletePerson") || method.getName().equals("updatePerson")) { //開啟事物 transaction.beginTransaction(); method.invoke(target, args);//執行目標物件的目標方法 //提交事物 transaction.commit(); return null; }else { return method.invoke(target, args); } } }
6. 測試類PersonDaoTest.java
package cn.it.spring.proxy.dao;
import java.lang.reflect.Proxy;
import org.junit.Test;
public class PersonDaoTest {
@Test
public void test(){
Transaction transaction = new Transaction();
PersonDao target = new PersonDaoImpl();
MyInterceptor interceptor = new MyInterceptor(target, transaction);
PersonDao personDao = (PersonDao) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), interceptor);
personDao.deletePerson();
System.out.println("=============================");
personDao.savePerson();
System.out.println("=============================");
personDao.updatePerson();
System.out.println("=============================");
for(Person person : personDao.getPersons()){
System.out.println(person.getPnameString());
}
}
}
7.結果