静态代理:
一个对象使用多个代理,实现代理的类为xxxProxy,proxy也实现被代理对象的接口,就可以实现嵌套代理。静态代理的代理类时事先写好的,前提是我们通过实现指定接口,知道被代理对象需要代理的方法。
动态代理:
通过反射,由JDK动态创建代理对象。这个方法完全依赖于接口,被代理对象必须实现某一接口,否则jdk不知道要写入什么方法。这也是jdk动态代理的局限之处。jdk动态代理底层是用asm类库实现的,asm类库是一个可以操纵二进制码的类库,asm直接修改二进制文件进行方法的代理。
以下是关于Java的Proxy类的详解。
newProxyInstance(ClassLoader loader, 类<?>[] interfaces, InvocationHandler h) ClassLoader loader:被代理对象的类加载器 类<?>[] interfaces:被代理对象所实现的接口数组
InvocationHandler h:代理方法的处理过程
代码实现
public static void main(String[] args) {
Tank tank = new Tank();
Moveble proxy1 = (Moveble)Proxy.newProxyInstance(Tank.class.getClassLoader(), new Class[]{Moveble.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("proxy1");
Object o = method.invoke(tank, args);
return o;
}
});
proxy1.move();
}
cglib
和jdk动态代理比起来适用范围更广,底层同样使用的是asm。cglib通过生成一个被代理对象的子类,重写父类方法来实现代理。缺陷是如果被代理对象被final修饰则不能生成子类也就无法使用cglib进行代理。
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Tank.class);
enhancer.setCallback(new MethodInterceptor() {
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("before");
Object invoke = methodProxy.invokeSuper(o, objects);
System.out.println("after");
return invoke;
}
});
Tank tank = (Tank)enhancer.create();
tank.move();
}
spring aop代理
首先加入spring所需依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
编写appContext.xml配置文件
这里有一个注意事项:aop:config中的 proxy-target-class设置为true否则会报一个Exception in thread “main“ java.lang.ClassCastException: com.sun.proxy.$Proxy8 cannot be cast to 的异常,如果被代理的类本身是一个接口,那默认就是true。
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.0.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="Tank" class="StaticProxy.Tank"/>
<bean id="TankProxy" class="spring.aop.TankProxy"/>
<aop:config proxy-target-class="true">
<aop:aspect id="proxy" ref="TankProxy" >
<!-- 切面:插入的功能-->
<aop:pointcut id="move" expression="execution(void StaticProxy.Tank.move())"/>
<!-- 切点:被切入的方法-->
<aop:after method="after" pointcut-ref="move"/>
<!-- 在方法之后执行-->
<aop:before method="before" pointcut-ref="move"/>
<!-- 在方法之前执行-->
</aop:aspect>
</aop:config>
</beans>
测试
package spring.aop;
import StaticProxy.Tank;
import javafx.application.Application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");
Tank tank = (Tank)context.getBean("Tank");
tank.move();
}
}
还有一种方法是通过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"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.0.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy/>
<bean id="Tank" class="StaticProxy.Tank"/>
<bean id="TankProxy" class="spring.aop.TankProxy"/>
</beans>
代理类
package spring.aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class TankProxy {
@Before("execution(void StaticProxy.Moveble.move())")
void before(){
System.out.println("before");
}
@After("execution(void StaticProxy.Moveble.move())")
void after(){
System.out.println("after");
}
}
还记得我们刚才的报错吗?嘿嘿!使用这种方法也是会出现相同的报错的,只需要把cast的类型改成一个接口就没有问题啦!例如Tank实现了Moveable接口,我就把他cast成Moveable而不是直接cast成Tank。因为如果是接口的话proxy-target-class默认为true。