首页 > 编程知识 正文

aop的底层实现原理,spring aop原理面试题

时间:2023-05-03 07:36:18 阅读:32278 作者:1751

AOP又称AOP面向切片编程,介绍通过预编译方式和运行中的动态代理实现程序功能统一维护的技术。 AOP是OOP的延续,是软件开发中的热点,是Spring框架中的重要内容,是函数式编程的派生类型。 可以利用AOP隔离业务逻辑的各个部分,从而降低业务逻辑各个部分之间的耦合度,提高程序的复用性,同时提高开发效率。

简而言之,AOP是指在不更改现有代码的情况下,横向切入代码来补充业务。 (本质上采用了动态代理模型。 )

AOP实现1,首先是jar包(Maven项目) dependenciesdependencygroupidorg.AspectJ/groupidartifactidaspectjweaver/artifactidvedverg 有三种方式使用spring接口【springAPI接口实现】,请求添加日志业务接口和实现类

package com.service; 公共接口用户服务{公共语音添加(; 公共语音删除(; 公共语音更新(; 公共语音搜索(; } package com.service; publicclassuserserviceimpimplementsuserservice { public void add (} {系统. out.println (add ) ); } public void delete () ) system.out.println ) ' delete ); } public void query () ) system.out.println ) ' query ); } public void update () (system.out.println ) (update ); }然后去写我们的扩展班。 我们写两个。 一个是前置扩展,另一个是后置扩展

package com.log; importorg.spring framework.AOP.methodbeforeadvice; import java.lang.reflect.Method; publicclasslogimplementsmethodbeforeadvice (/method )要运行的目标对象的方法//args参数//target )目标对象公共语音基础) )方法方法对象目标(throws throwable (system.out.println ) target.getclable publicclassafterlogimplementsafterreturningadvice//return vaule :返回值publicvoidafterreturning (对象返回值,方法已运行对象目标(throws throwable { system.out.println ('是' target.getClass ).getName ) '中的' mmontln

? XML版本=' 1.0 '编码=' utf-8 '? beans xmlns=' http://www.spring框架. org/schema/beans ' xmlns 3360 xsi=' http://www.w3.org/2001/XML方案-实例' xmlns : AOP=' http://www.spring framework.org/schema/AOP ' xsi :方案位置=' 3358 www bbw beans/spring-beans.xsd 3358 www.spring framework.org/schema/AOP 358

gframework.org/schema/aop/spring-aop.xsd"> <!--注册bean--> <bean id="userservice" class="com.service.UserServiceImp"></bean> <bean id="log" class="com.log.Log"/> <bean id="afterlog" class="com.log.AfterLog"/> <!--配置aop--> <aop:config> <!--切入点:expression:表达式,execution(要执行的位置)--> <aop:pointcut id="point" expression="execution(* com.service.UserServiceImp.*(..))"/> <!--执行环绕--> <aop:advisor advice-ref="log" pointcut-ref="point"/> <aop:advisor advice-ref="afterlog" pointcut-ref="point"/> </aop:config></beans>

测试

public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applcationContext.xml"); //动态代理代理的是接口 UserService userService = (UserService) context.getBean("userservice"); userService.add(); }} 方式二:自定义实现AOP【主要是切面定义】

自定义一个切入类

package com.diy;public class DiyPointcut { public void before(){ System.out.println("before"); } public void after(){ System.out.println("after"); }}

设置配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--注册bean--> <bean id="userservice" class="com.service.UserServiceImp"></bean> <bean id="log" class="com.log.Log"/> <bean id="afterlog" class="com.log.AfterLog"/> <bean id="diy" class="com.diy.DiyPointcut"> </bean> <aop:config> <!--自定义切面--> <aop:aspect ref="diy"> <!--切入点--> <aop:pointcut id="point" expression="execution(* com.service.UserServiceImp.*(..))"/> <aop:before method="before" pointcut-ref="point"/> <aop:after method="after" pointcut-ref="point"/> </aop:aspect> </aop:config></beans>

测试

public class Test { @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) context.getBean("userService"); userService.add(); }} 方式三:采用注解方式

自定义切入类

package com.service;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspect //标注这个类是一个切面public class AnnotationPointcut { @Before("execution(* com.service.UserServiceImp.*(..))") public void before(){ System.out.println("before"); } @After("execution(* com.service.UserServiceImp.*(..))") public void after(){ System.out.println("after"); } //在环绕增强中,我们可以给around方法一个参数,代表我们要获取切入的点 @Around("execution(* com.service.UserServiceImp.*(..))") public void around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("around"); Object proceed = joinPoint.proceed(); System.out.println("after around"); }}

设置配置文件信息(注意要开启注解支持)

<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--支持注解--> <aop:aspectj-autoproxy/> <!--注册bean--> <bean id="userservice" class="com.service.UserServiceImp"></bean> <bean id="annotationPointcut" class="com.diy.AnnotationPointcut"></bean></beans>

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。