代理模式主要有两种:静态代理和动态代理
代理模式 代理模式是常用的 Java 设计模式,它的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息、过滤消息、把消息转发给委托类,以及事后处理消息等。代理类与委托类之间通常会存在关联关系,一个代理类的对象与一个委托类的对象关联, 代理 类的对象本身并不真正实现服务, 而是通过调用委托类的对象的相关方法, 来提供特定的服务. 按照代理类的创建时期,代理类可分为两种。
1.静态代理类: 由程序员创建或由特定工具自动生成源代码,再对其编译。在程序运行前,代理类 的.class 文件就已经存在了。
静态代理类 如下:
StudentImpl 类是代理类,
public class StudentImpl implements IStudentServiceDAO {
StudentServiceImpl service;
StudentImpl(StudentServiceImpl service) {
this.service = service;
}
@Override
public void save(Student stu) {
System.out.println("开启事务");
service.save(stu);
System.out.println("提交事务");
}
}
StudentServiceImpl 类是委托类,
public class StudentServiceImpl implements IStudentServiceDAO {
@Override
public void save(Student stu) {
System.out.println("调用dao层保存方法");
}
}
这两个类都实现了 HelloService 接口.
其中 StudentServiceImpl 类是 IStudentServiceDAO 接口的真正实现者,
而 StudentImpl 类是通过调用 StudentServiceImpl 类的相关方法来提供特定服务的.
StudentImpl 类的 save()方法会分别调用被代理的 StudentServiceImpl 对象的 save()方法,并且在方法调用前后都会执行一些简单的打印操作。 由此可见,代理类可以为委托类预处理消息、把消息转发给委托类和事后处理消息等。
测试类代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class StaticProxyTest {
@Autowired
@Qualifier("servicetran")
IStudentServiceDAO service;
@Test
public void testStaticProxy() {
service.save(new Student("ZhangSan", 18));
}
}
xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<--id="service"的类为委托类--->
<bean id="service" class="com.gxxy.spring_05proxy.static1.StudentServiceImpl"></bean>
<--id="servicetran"的类为代理类-->
<bean id="servicetran" class="com.gxxy.spring_05proxy.static1.StudentImpl">
<constructor-arg name="service" ref="service"></constructor-arg>
</bean>
</beans>
类的源代码是由程序员编写的,在程序运行前,它的.class 文件就已经存在了,这种代理类称为静态代理类。
2.动态代理类:在程序运行时,运用反射机制动态创建而成。
动态代理类的字节码在程序运行时由Java 反射机制动态生成,无需程序员手工编写它的源代码.动态代理类不仅简化了编程工作,而且提高了 软件系统的可扩展性,因为Java反射机制可以生成任意类型的动态代理类.
java.lang.reflect 包中的 Proxy 类和 InvocationHandler 接口提供了生成动态代理类的能力。
Proxy 类提供了创建动态代理类及其实例的静态方法。
①:接口(interface)/InvocationHandler
代理类:实现 InvocationHandler 接口 注意: InvocationHandler 是 import java.lang.reflect.InvocationHandler包中的;
package com.gxxy.spring_05proxy.dynamic.interfaces;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class ProxyDynamicInvocationHandler implements InvocationHandler {
private IStudentServiceDAO service;
ProxyDynamicInvocationHandler(IStudentServiceDAO service) {
this.service = service;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Begin");
System.out.println("save");
System.out.println("commit");
System.out.println("-------"+method.getName());
return method.invoke(service, args);
}
}
委托类:(实现IStudentServiceDAO 接口,接口中只有save()方法)
public class StudentServiceImpl implements IStudentServiceDAO {
@Override
public void save(Student stu) {
System.out.println("调用dao层保存方法");
}
}
测试类:
package com.gxxy.spring_05proxy.dynamic.interfaces;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class DynamicProxyTest {
@Autowired
IStudentServiceDAO service;
@Test
public void testStaticProxy() {
ClassLoader loader = IStudentServiceDAO.class.getClassLoader();
Class<?>[] interfaces = new Class<?>[] { IStudentServiceDAO.class };
InvocationHandler h = new ProxyDynamicInvocationHandler(service);
IStudentServiceDAO newProxyInstance = (IStudentServiceDAO) Proxy.newProxyInstance(loader, interfaces, h);
newProxyInstance.save(new Student("ZhangSan",18));
}
}
XML配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="service" class="com.gxxy.spring_05proxy.dynamic.StudentServiceImpl"></bean>
</beans>
②:继承(inherit)/Enhancer
代理类:实现 InvocationHandler 接口 注意: InvocationHandler 是 import org.springframework.cglib.proxy.InvocationHandler 包中的;
package com.gxxy.spring_05proxy.dynamic.inherit;
import java.lang.reflect.Method;
import org.springframework.cglib.proxy.InvocationHandler;
public class ProxyDynamicInvocationHandler implements InvocationHandler {
private Object service;
ProxyDynamicInvocationHandler(StudentServiceImpl service) {
this.service = service;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Begin");
System.out.println("save");
System.out.println("commit");
System.out.println("-------" + method.getName());
return method.invoke(service, args);
}
}
委托类:StudentServiceImpl
package com.gxxy.spring_05proxy.dynamic.inherit;
public class StudentServiceImpl {
public void save(Student stu) {
System.out.println("调用dao层保存方法");
}
}
测试类:
package com.gxxy.spring_05proxy.dynamic.inherit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class DynamicinheritProxyTest {
@Autowired
StudentServiceImpl service;
@Test
public void testStaticProxy() {
ClassLoader loader = StudentServiceImpl.class.getClassLoader();
InvocationHandler handle = new ProxyDynamicInvocationHandler(service);
Enhancer hancer = new Enhancer();
hancer.setClassLoader(loader);
hancer.setSuperclass(StudentServiceImpl.class);
hancer.setCallback(handle);
StudentServiceImpl create = (StudentServiceImpl) hancer.create();
create.save(new Student("Jack", 18));
}
}
XML配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="service" class="com.gxxy.spring_05proxy.dynamic.inherit.StudentServiceImpl"></bean>
</beans>