您好,欢迎访问一九零五行业门户网

什么是Java AOP动态代理?

1.ioc与aop概念ioc:控制反转,把对象创建和对象之间的调用过程,交给spring进行管理。使用ioc的目的是为了降低耦合度。
aop:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。aop是oop的延续,是软件开发中的一个热点,也是spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用aop可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。aop的底层实现是基于动态代理(实现方式是当切入接口时,使用jdk原生动态代理;当切入普通方法时,使用cglib动态代理)。
2.为何使用动态代理随着业务的不断扩展:
(1)日志功能:如果日志代码修改,需要修改多处。
(2)校验功能:如果多处需要校验,需要修改多处。
这时就需要使用动态代理来解决问题,动态代理的实现方式有两种:
[1]jdk原生动态代理:缺点是必须基于接口完成
[2]cglib动态代理:他可以不用基于接口完成
2.1 jdk原生动态代理
2.1.1 mathservice接口类public interface mathservice { //+ public double add(double a,double b); //- public double sub(double a,double b); //* public double mul(double a,double b); /// public double div(double a,double b);}
2.1.2 mathserviceimpl实现接口类public class mathserviceimpl implements mathservice{ @override public double add(double a, double b) { double result=a+b; return result; } @override public double sub(double a, double b) { double result=a-b; return result; } @override public double mul(double a, double b) { double result=a*b; return result; } @override public double div(double a, double b) { double result=a/b; return result; }}
2.1.3 proxyfactory动态代理工厂public class proxyfactory { //被代理对象 private object target; public proxyfactory(object target) { this.target = target; } //获取代理对象 public object getproxy(){ /** * classloader loader, 被代理对象的类加载器 * class<?>[] interfaces, 被代理对象实现的接口 * invocationhandler h: 当代理对象执行被代理的方法时,会触发该对象中的invoke功能 */ classloader loader=target.getclass().getclassloader(); class<?>[] interfaces=target.getclass().getinterfaces(); invocationhandler h=new invocationhandler() { @override public object invoke(object proxy, method method, object[] args) throws throwable { //可以加上需要的非业务代码 //method.getname()获取方法名 // arrays.aslist(args)获取输入值 system.out.println("this is "+method.getname()+" method begin with"+ arrays.aslist(args)); //method:表示代理对象要代理的方法 //invoke:回调该函数 //args:方法需要的参数 object result = method.invoke(target, args);//代理对象回调该方法 return result; } }; //先写此处方法,才可找到上述三个方法填写方式 object o = proxy.newproxyinstance(loader, interfaces, h); return o; }}
2.1.4 测试类public class test01 { public static void main(string[] args) { mathserviceimpl target=new mathserviceimpl(); proxyfactory proxyfactory=new proxyfactory(target); mathservice proxy = (mathservice) proxyfactory.getproxy(); double add = proxy.add(15.0, 5.0); system.out.println(add); }}
2.2 cglib动态代理
2.2.1 mathserviceimpl类public class mathserviceimpl{ public double add(double a, double b) { double result=a+b; return result; } public double sub(double a, double b) { double result=a-b; return result; } public double mul(double a, double b) { double result=a*b; return result; } public double div(double a, double b) { double result=a/b; return result; }}
2.2.2 proxyfactory动态代理工厂注意:
(1)引入cglib的jar包.
<dependency>
<groupid>cglib</groupid>
<artifactid>cglib</artifactid>
<version>3.2.5</version>
</dependency>
(2)创建一个代理类工厂并实现接口methodinterceptor
public class proxyfactory implements methodinterceptor { private object target; public proxyfactory(object target) { this.target = target; } //获取代理对象 public object getproxy(){ enhancer enhancer=new enhancer(); //指定被代理对象的父类 enhancer.setsuperclass(target.getclass()); //指定回调类 enhancer.setcallback(this); //创建代理对象 return enhancer.create(); } //当代理对象执行代理方法时触发的方法 public object intercept(object obj, method method, object[] args, methodproxy proxy) throws throwable {// system.out.println("before++++++++++++++++++++");// object result = method.invoke(target, args); //可以加上需要的非业务代码 //method.getname()获取方法名 // arrays.aslist(args)获取输入值 system.out.println("this is "+method.getname()+" method begin with"+ arrays.aslist(args)); //method:表示代理对象要代理的方法 //invoke:回调该函数 //args:方法需要的参数 object result = method.invoke(target, args);//代理对象回调该方法 return result; }}
2.2.3 测试类public class test01 { public static void main(string[] args) { mathserviceimpl target=new mathserviceimpl(); proxyfactory proxyfactory=new proxyfactory(target); mathserviceimpl proxy = (mathserviceimpl) proxyfactory.getproxy(); double add = proxy.add(1, 2); system.out.println(add); }}
3.aop动态代理
3.1 添加对应依赖<dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>5.2.15.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aspects</artifactid> <version>5.2.15.release</version> </dependency>
3.2 配置spring.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" 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--包扫描--> <context:component-scan base-package="com.qy151wd.proxy.proxy.aop"/> <!--开启aop注解--> <aop:aspectj-autoproxy/></beans>
3.3 mathservice接口类public interface mathservice { public double add(double a, double b); public double sub(double a, double b); public double mul(double a, double b); public double div(double a, double b);}
3.4 mathserviceimpl实现接口类@servicepublic class mathserviceimpl implements mathservice { @override public double add(double a, double b) { double result=a+b; return result; } @override public double sub(double a, double b) { double result=a-b; return result; } @override public double mul(double a, double b) { double result=a*b; return result; } @override public double div(double a, double b) { double result=a/b; return result; }}
3.5 logaspect类@service //若是使用@component也可以@aspect //表示该类为切面类public class logaspect { //任意返回类型 aop包下的所有类都有切面日志 使用通配符 //第一个*:修饰符和返回值类型 //第二个*:所有类 //第三个*:所有方法 @before("execution(* com.qy151wd.proxy.proxy.aop.*.*(..))") public void before(){ system.out.println("方法执行前的日志"); } @after("execution(* com.qy151wd.proxy.proxy.aop.*.*(..))") //总会被执行,不管有没有异常 public void after(){ system.out.println("方法执行后的日志"); } @afterreturning("execution(* com.qy151wd.proxy.proxy.aop.*.*(..))")//只有碰到return后才会执行 public void afterreturning(){ system.out.println("碰到return后执行"); } @afterthrowing("execution(* com.qy151wd.proxy.proxy.aop.*.*(..))")//异常通知 public void afterthrowing(){ system.out.println("出现异常了"); }}
3.6 测试类public class test01 { public static void main(string[] args) { //从spring容器中获取 applicationcontext app=new classpathxmlapplicationcontext("spring.xml"); mathservice mathservice = (mathservice) app.getbean("mathserviceimpl"); double add = mathservice.add(10, 5); system.out.println(add); }}
以上就是什么是java aop动态代理?的详细内容。
其它类似信息

推荐信息