网站建设与管理专业就业,文章id wordpress,南平建设集团有限公司网站,美乐乐是哪个公司做的网站AOP用于处理系统中分布于各个模块的横切关注点#xff0c;比如事务管理、日志、缓存等。AOP实现的关键#xff0c;在于AOP框架自动创建的AOP代理#xff0c;AOP代理主要分为静态代理和动态代理#xff0c;静态代理的代表为AspectJ#xff1b;而动态代理则以Spring AOP为代…AOP用于处理系统中分布于各个模块的横切关注点比如事务管理、日志、缓存等。AOP实现的关键在于AOP框架自动创建的AOP代理AOP代理主要分为静态代理和动态代理静态代理的代表为AspectJ而动态代理则以Spring AOP为代表。静态代理 AspectJAspectJ是什么Eclipse AspectJ is a seamless aspect-oriented extension to the Java™ programming language. It is Java platform compatible easy to learn and use.AspectJ是Java的扩展用于实现面向切面编程。AspectJ有自己的编辑器ajcAspectJ 官网Eclipse AspectJprojects.eclipse.orgAspectJ 入门www.jianshu.com使用AspectJ的编译时增强实现AOP。之前提到AspectJ是静态代理的增强。所谓的静态代理就是AOP框架会在编译阶段生成AOP代理类因此也称为编译时增强。它会在编译阶段将Aspect织入Java字节码中运行的时候就是经过增强之后的AOP对象。proceed方法就是回调执行被代理类中的方法。动态代理Spring AOPSpring AOP官方文档Core Technologiesdocs.spring.io1.与AspectJ的静态代理不同Spring AOP使用的是动态代理。所谓的动态代理就是说AOP框架不会去修改字节码而是在内存中临时为方法生成一个AOP对象这个AOP对象包含了目标对象的全部方法并且在特定的切点做了增强处理并回调原对象的方法。2.Spring AOP中的动态代理主要有两种方式JDK动态代理和CGLIB动态代理。JDK动态代理通过“反射”来接收被代理的类并且要求被代理的类必须实现一个接口。JDK动态代理的核心是InvocationHandler接口和Proxy类。如果目标类没有实现接口那么Spring AOP会选择使用CGLIB来动态代理目标类。3.CGLIBCode Generation Library是一个代码生成的类库可以在运行时动态地生成某个类的子类。注意CGLIB是通过继承的方式做的动态代理因此如果某个类被标记为final那么它是无法使用CGLIB做动态代理的。使用动态代理实质上就是调用时拦截对象方法对方法进行改造、增强测试CGlib生成的动态代理测试类package com.example.demo.aop;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;SpringBootApplication
RestController
//EnableAspectJAutoProxy
public class SpringBootDemoApplication {// 直接用Chinese类注入Autowiredprivate Landlord landlord;RequestMapping(/test)public void test() {landlord.service();System.out.println(landlord.getClass());}public static void main(String[] args) {SpringApplication.run(SpringBootDemoApplication.class, args);}
}Landlord类package com.example.demo.aop;import org.springframework.stereotype.Component;Component(landlord)
public class Landlord {public void service() {// 仅仅只是实现了核心的业务功能System.out.println(签合同);System.out.println(收房租);}
}
Broker类package com.example.demo.aop.aspect;import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;Component
Aspect
class Broker {Before(execution(* com.example.demo.aop.Landlord.service()))public void before() {System.out.println(带租客看房);System.out.println(谈价格);}After(execution(* com.example.demo.aop.Landlord.service()))public void after() {System.out.println(交钥匙);}
}运行结果带租客看房
谈价格
签合同
收房租
交钥匙
class com.example.demo.aop.Landlord$$EnhancerBySpringCGLIB$$1f1c504aSpring AOP就是这么简单啦juejin.imJava AOP的底层实现原理 - 健人雄 - 博客园www.cnblogs.comSpring(4)——面向切面编程AOP模块www.jianshu.com