通过Java代理的方式:
汽车的代理,需要实现
java.lang.reflect.InvocationHandler接口
public class CarJavaProxy implements InvocationHandler {
private Object car;
public void setCar(Object car) {
this.car = car;
}
public Object createProxy() {
Object proxy = Proxy.newProxyInstance(car.getClass().getClassLoader(), car.getClass().getInterfaces(), this);
return proxy;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(car, args);
if ("info".equalsIgnoreCase(method.getName())) {
System.out.println("Give 200 maintenance coupon via Java proxy");
}
return result;
}
}
Spring配置文件beans-java-proxy.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"
default-autowire="byName">
<bean id="car" class="com.john.spring.aop.Car">
<property name="name" value="Superb" />
<property name="price" value="220000" />
</bean>
<bean id="carJavaProxy" class="com.john.spring.aop.CarJavaProxy">
<property name="car" ref="car" />
</bean>
</beans>
测试:
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "beans-java-proxy.xml" });
CarJavaProxy carProxy = (CarJavaProxy) ctx.getBean("carJavaProxy");
Vehicle vehicle = (Vehicle) carProxy.createProxy();
vehicle.info();
}
输出:
Car name: Superb, price: 220000
Give 200 maintenance coupon via Java proxy
分享到:
相关推荐
Spring AOP(面向切面编程)是Java高级编程中的重要组成部分,它允许程序员以声明的方式处理关注点(例如日志、事务管理等),而不是通过硬编码。本文深入分析了Spring AOP的实现机制,让读者能够更好地理解和应用这...
package com.gc.aop下为:aop方式ProxyFactoryBean代理...package com.gc.javaproxy下为:java代理机制实现 package com.gc.proxy下为:自定义代理模式(面向接口编程) package com.gc.normal下为:通用日志处理方式
SpringAOP动态代理 Spring AOP 使用的动态代理主要有两种方式:JDK 动态代理和 CGLIB 代理。 JDK 动态代理:用于代理实现了接口的类。Spring 会使用 java.lang.reflect.Proxy 类来创建代理对象。 CGLIB 代理:用于...
描述一下Spring AOP? 在Spring AOP中关注点(concern)和横切关注点(cross-cutting ...Spring AOP 代理是什么? 引介(Introduction)是什么? 连接点(Joint Point)和切入点(Point Cut)是什么? 织入(Weaving)是什么?
Spring AOP(面向切面编程)作为Spring框架的一个重要部分,为Java开发者提供了一个强大而灵活的工具来切入代码执行流程,实现关注点的分离。通过详细解析Spring AOP的源码,本文揭示了其背后的核心原理和实现机制。...
通过动态代理模拟Spring AOP,通过动态代理模拟Spring AOP
NULL 博文链接:https://jadeluo.iteye.com/blog/1940019
写了SpringAop的两种代理的实现,jdk动态代理和cglib代理的代码,及测试。
Spring-AOP-利用java中的动态代理和Spring的拦截器做到AOP
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,AOP的原理就是java的动态代理机制
为了说明Spring的AOP原理,本人使用代理模式中的动态代理完成演示AOP编程的原理的演示。相信,如果你耐心看完整个程序(几乎一行注释一行代码),那么你对Spring这个东西就不是觉得有什么神秘了! 阅读对象:凡是喜爱...
Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,AOP的原理就是java的动态代理机制
springAOP指的是在spring中的AOP,什么是AOP,相对于java中的面向对象(oop),在面向对象中一些公共的行为,像日志记录,权限验证等如果都使用面向对象来做,会在每个业务方法中都写上重复的代码,造成代码的冗余。...
java中动态代理,springAop.pdf
NULL 博文链接:https://arne3166.iteye.com/blog/1046340
简介 Spring AOP 大家在使用Spring 框架 ...就是Spring IOC 容器中获取Bean–getBean()的是时候IOC容器返回的时候 代理类的实例 而非真实类的实例,这个代理类 我们没有写任何的代码去实现,是通过java的动态代理或者CG