com.itheima.aspect包:
MyAspect.java
package com.itheima.aspect;
public class MyAspect {
public void before() {
System.out.println("已进行权限验证");
}
public void after() {
System.out.println("已操作完成");
}
}
一、JDK代理:
com.itheima.jdk包
JdkProxy.Java
package com.itheima.jdk;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import com.itheima.aspect.MyAspect;
public class JdkProxy implements InvocationHandler{
private UserDao userDao;
public Object createProxy(UserDao userDao) {
this.userDao = userDao;
ClassLoader classLoader = JdkProxy.class.getClassLoader();
Class[] clazz = userDao.getClass().getInterfaces();
return Proxy.newProxyInstance(classLoader,clazz,this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
MyAspect myAspect = new MyAspect();
myAspect.before();
Object obj = method.invoke(userDao,args);
myAspect.after();
return obj;
}
}
JdkTest.java
package com.itheima.jdk;
public class JdkTest {
public static void main(String[] args) {
JdkProxy jdkproxy = new JdkProxy();
UserDao userDao= new UserDaoimp();
UserDao userDao1 = (UserDao)jdkproxy.createProxy(userDao);
userDao1.queryUser();
userDao1.deleteUser();
userDao1.changeUser();
}
}
UserDao.java
package com.itheima.jdk;
public interface UserDao {
public void queryUser();
public void deleteUser();
public void changeUser();
}
Userdaoimp.java
package com.itheima.jdk;
public class UserDaoimp implements UserDao{
public void queryUser() {
System.out.println("查询用户信息");
}
public void deleteUser() {
System.out.println("删除用户信息");
}
public void changeUser() {
System.out.println("修改用户信息");
}
}
二、CGLIB代理
com.itheima.cglib包
CglibProxy.java
package com.itheima.cglib;
import java.lang.reflect.Method;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import com.itheima.aspect.MyAspect;
public class CglibProxy implements MethodInterceptor {
public Object createProxy(Object target) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(target.getClass());
enhancer.setCallback(this);
return enhancer.create();
}
public Object intercept(Object proxy, Method method,Object[] args,
MethodProxy methodProxy) throws Throwable {
MyAspect myAspect = new MyAspect();
myAspect.before();
Object obj = methodProxy.invokeSuper(proxy, args);
myAspect.after();
return obj;
}
}
CglibTest.java
package com.itheima.cglib;
public class CglibTest {
public static void main(String[] args) {
CglibProxy cglibproxy = new CglibProxy();
UserDao userDao = new UserDao();
UserDao userDao1 = (UserDao)cglibproxy.createProxy(userDao);
userDao1.queryUser();
userDao1.deleteUser();
userDao1.changeUser();
}
}
UserDao.java
package com.itheima.cglib;
public class UserDao {
public void queryUser() {
System.out.println("查询用户信息");
}
public void deleteUser() {
System.out.println("删除用户信息");
}
public void changeUser() {
System.out.println("修改用户信息");
}
}