使用xml配置實現,可根據此方法轉換爲Annotation:java
DynamicProxy.javaspring
package com.me.proxy; import java.lang.reflect.Method; import org.apache.log4j.Logger; import org.springframework.cglib.proxy.InvocationHandler; import org.springframework.cglib.proxy.Proxy; public class DynamicProxy implements InvocationHandler { private Logger logger = Logger.getLogger(DynamicProxy.class); private DynamicProxy() { } private Object target; /** * 建立代理對象 * @param obj * @return */ public static Object getInstance(Object obj) { DynamicProxy dynamicProxy = new DynamicProxy(); dynamicProxy.target = obj; return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj .getClass().getInterfaces(), dynamicProxy); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { logger.info("新加入的方法"); System.out.println("新加入的方法"); return method.invoke(target, args); } }
IPeopleAction.javaapache
package com.me.control; import java.util.List; import com.me.dao.PeopleDao; import com.me.model.People; public class IPeopleAction implements PeopleAction { private PeopleDao peopleDao; private People people; private List<String> list; public PeopleDao getPeopleDao() { return peopleDao; } public void setPeopleDao(PeopleDao peopleDao) { this.peopleDao = peopleDao; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public People getPeople() { return people; } public void setPeople(People people) { this.people = people; } @Override public String add() { peopleDao.add(people); return "success"; } @Override public String update() { peopleDao.update(people); return "success"; } @Override public String delete() { peopleDao.delete(1); return "success"; } @Override public String query() { peopleDao.select(); return "success"; } }
IPeopleDao.javaide
package com.me.dao; import com.me.model.People; public class IPeopleDao implements PeopleDao{ @Override public boolean add(People people) { System.out.println("添加成功"); return false; } @Override public boolean delete(int pid) { System.out.println("刪除成功"); return false; } @Override public boolean update(People people) { System.out.println("更新成功"); return false; } @Override public People select() { System.out.println("查詢成功"); return null; } }
beans.xmlthis
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="iPeopleDao" class="com.me.dao.IPeopleDao" /> <bean id="dynamicProxy" class="com.me.proxy.DynamicProxy" factory-method="getInstance"> <constructor-arg ref="iPeopleDao" /> </bean> <bean id="iPeopleAction" class="com.me.control.IPeopleAction"> <property name="peopleDao" ref="dynamicProxy" /> </bean> </beans>
TestSpring.javaspa
package com.me.testspring; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.me.control.IPeopleAction; public class TestSpring { private ApplicationContext factory = new ClassPathXmlApplicationContext( "beans.xml"); @Test public void test() { IPeopleAction action = factory.getBean("iPeopleAction", IPeopleAction.class); action.delete(); } }
使用Annotation簡化代碼:代理
ProxyMethod.javacode
package com.me.annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface ProxyMethod { public String value() default ""; }
改造後的 DynamicProxy.javaxml
package com.me.proxy; import java.lang.reflect.Method; import org.apache.log4j.Logger; import org.springframework.cglib.proxy.InvocationHandler; import org.springframework.cglib.proxy.Proxy; import com.me.annotation.ProxyMethod; public class DynamicProxy implements InvocationHandler { private Logger logger = Logger.getLogger(DynamicProxy.class); private DynamicProxy() { } private Object target; /** * 建立代理對象 * * @param obj * @return */ public static Object getInstance(Object obj) { DynamicProxy dynamicProxy = new DynamicProxy(); dynamicProxy.target = obj; return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj .getClass().getInterfaces(), dynamicProxy); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if(method.isAnnotationPresent(ProxyMethod.class)){ ProxyMethod proxyMethod = method.getAnnotation(ProxyMethod.class); //logger.info(proxyMethod.value()); System.out.println(proxyMethod.value()); } return method.invoke(target, args); } }
使用自定義的Annotation對象
PeopleDao.java
package com.me.dao; import com.me.annotation.ProxyMethod; import com.me.model.People; public interface PeopleDao { public boolean add(People people); @ProxyMethod("這個信息爲代理刪除信息") public boolean delete(int pid); public boolean update(People people); public People select(); }