JAVA动态代理模式
这是我要实现的效果
?
package org.aop;import java.lang.reflect.Proxy;import org.dao.UserDao;import org.dao.impl.UserDaoImpl;import org.pojo.User;public class BeanProxyFactory {public static Object getProxyObject(String className) {Object proxyObject = null;try {ClassLoader classLoader = ClassLoader.getSystemClassLoader();Class claszz = classLoader.loadClass(className);MethodInvocationHandler handler = new MethodInvocationHandler(claszz.newInstance());proxyObject = Proxy.newProxyInstance(classLoader, claszz.getInterfaces(),handler);} catch (Exception e) {}return proxyObject;}public static void main(String[] args) {UserDao proxy = (UserDao) BeanProxyFactory.getProxyObject(UserDaoImpl.class.getName());proxy.save(new User());//我想实现保存的时候,自动调用一些动作}}?
?