package com.xyl.aop;
/**
* Author:谢英亮
* Date:2018/3/13 15:32
* Description:公共的接口
*/
public interface UserService {
void create();
void delete();
}
package com.xyl.aop;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
* Author:谢英亮
* Date:2018/3/13 15:33
* Description:目标对象
*/
public class UserServiceImpl implements UserService {
private final static Logger log= LogManager.getLogger(UserServiceImpl.class);
@Override
public void create() {
log.info("create a user");
}
@Override
public void delete() {
log.info("delete a user");
}
}
package com.xyl.aop;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
* Author:谢英亮
* Date:2018/3/13 16:33
* Description:静态代理
*/
public class UserServiceImplStaticProxy implements UserService {
private final static Logger log = LogManager.getLogger(UserServiceImplStaticProxy.class);
private UserService userService;
public UserServiceImplStaticProxy(UserService userService) {
this.userService = userService;
}
@Override
public void create() {
log.info("create方法即将执行。。");
userService.create();
log.info("create方法执行结束。。");
}
@Override
public void delete() {
log.info("delete方法即将执行。。");
userService.delete();
log.info("delete方法执行结束。。");
}
}
package com.xyl.aop;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* Author:谢英亮
* Date:2018/3/13 16:08
* Description:jdk代理
*/
public class UserServiceJdkProxy implements InvocationHandler {
private final static Logger log = LogManager.getLogger(UserServiceJdkProxy.class);
public UserServiceJdkProxy(Object target) {
this.target = target;
}
private Object target;
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
log.info(method.getName() + "方法即将执行。。");
Object result = method.invoke(target, args);
log.info(method.getName() + "方法执行结束。。");
return result;
}
}
package com.xyl.aop;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.lang.reflect.Method;
/**
* Author:谢英亮
* Date:2018/3/13 15:35
* Description:采用cglib动态代理
*/
public class UserServiceImplCglibProxy implements MethodInterceptor {
private final static Logger log = LogManager.getLogger(UserServiceImplCglibProxy.class);
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
log.info(method.getName() + "方法即将执行。。。");
Object result = methodProxy.invokeSuper(o, objects);
log.info(method.getName() + "方法执行结束。。。");
return result;
}
}
package com.xyl.aop;
import net.sf.cglib.proxy.Enhancer;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.lang.reflect.Proxy;
/**
* Author:谢英亮
* Date:2018/3/13 15:44
* Description:测试代理模式
*/
public class TestProxy {
private final static Logger log = LogManager.getLogger(UserServiceImplCglibProxy.class);
public static void main(String[] args) {
log.info("测试静态代理。。。");
testStatic();
log.info("测试Cglib代理。。。");
testCglib();
log.info("测试JDK代理。。。");
testJDk();
}
/**
* 测试静态代理
*/
private static void testStatic() {
UserService userService = new UserServiceImpl();
UserService proxy = new UserServiceImplStaticProxy(userService);
proxy.create();
proxy.delete();
}
/**
* 测试cglib代理模式
*/
private static void testCglib() {
UserServiceImpl userService = (UserServiceImpl) Enhancer.create(UserServiceImpl.class, new UserServiceImplCglibProxy());
userService.create();
userService.delete();
}
/**
* 测试Jdk代理模式
*/
private static void testJDk() {
UserService userService = (UserService) Proxy.newProxyInstance(UserServiceImpl.class.getClassLoader(), UserServiceImpl.class.getInterfaces(), new UserServiceJdkProxy(new UserServiceImpl()));
userService.create();
userService.delete();
}
}