一、JDK动态代理
1.导入jar包。需要导入6个jar包,spring框架包中的5个,还有一个依赖包
2.Spring配置文件中约束
<?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">
<context:component-scan base-package="com.wx"></context:component-scan>
</beans>
3.书写代码
package com.wx.dao;
public interface UserDao {
public void say();
}
package com.wx.dao.impl;
import org.springframework.stereotype.Repository;
import com.wx.dao.UserDao;
@Repository("userDao")
public class UserDaoImpl implements UserDao{
@Override
public void say() {
// TODO Auto-generated method stub
System.out.println("UserDao Method!");
}
}
package com.wx.aspect;
import org.springframework.stereotype.Component;
@Component("myAspect")
public class MyAspect {
public void check(){
System.out.println("检查数据");
}
public void log(){
System.out.println("打印日志");
}
}
package com.wx.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.wx.aspect.MyAspect;
import com.wx.dao.UserDao;
@Component("jdkProxy")
public class JDKProxy implements InvocationHandler {
private Object obj;
@Resource(name="myAspect")
private MyAspect myAspect;
public Object createProxy(Object obj){
this.obj = obj;
ClassLoader classLoader = JDKProxy.class.getClassLoader();
Class[] clazz = obj.getClass().getInterfaces();
return Proxy.newProxyInstance(classLoader, clazz, this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
myAspect.check();
Object object = method.invoke(obj, args);
myAspect.log();
return object;
}
}
5.测试类
package com.wx.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wx.dao.UserDao;
import com.wx.proxy.JDKProxy;
public class MyTest {
public static void main(String[] args){
ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
JDKProxy jdkProxy = (JDKProxy)application.getBean("jdkProxy");
UserDao userDao = (UserDao)application.getBean("userDao");
UserDao userDaoProxy = (UserDao)jdkProxy.createProxy(userDao);
userDaoProxy.say();
}
}
6.结果
二、CGLIB代理
在使用JDK动态代理时,使用的动态代理的对象必须实现一个或者多个接口。而没有实现接口的类,可以使用CGLIB代理。
1.导入jar包。需要导入6个jar包,spring框架包中的5个,还有一个依赖包
2.Spring配置文件中约束
<?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">
<context:component-scan base-package="com.wx"></context:component-scan>
</beans>
3. 书写代码
package com.wx.service;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserService {
public void say(){
System.out.println("UserService Method!");
}
}
package com.wx.proxy;
import java.lang.reflect.Method;
import javax.annotation.Resource;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.stereotype.Component;
import com.wx.aspect.MyAspect;
@Component("cglibProsy")
public class CGLIBProxy implements MethodInterceptor{
@Resource(name="myAspect")
private MyAspect myAspect;
public Object createProxy(Object obj){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(obj.getClass());
enhancer.setCallback(this);
return enhancer.create();
}
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2,MethodProxy arg3) throws Throwable {
// TODO Auto-generated method stub
myAspect.check();
Object obj = arg3.invokeSuper(arg0, arg2);
myAspect.log();
return obj;
}
}
4.测试类
package com.wx.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wx.dao.UserDao;
import com.wx.proxy.CGLIBProxy;
import com.wx.proxy.JDKProxy;
import com.wx.service.UserService;
public class MyTest {
public static void main(String[] args){
ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
CGLIBProxy cglibProxy = (CGLIBProxy)application.getBean("cglibProsy");
UserService userService = (UserService)application.getBean("userService");
UserService userServiceProxy = (UserService)cglibProxy.createProxy(userService);
userServiceProxy.say();
}
}
5.结果