AOP面向切面编程
什么是AOP
AOP (Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
OOP (Object Oriented Programming) 面向对象编程
AOP (Aspect Oritented Programming) 面向切面编程
OOP 到AOP 不是替换的关系,而是一种扩展,使用了AOP后,OOP还是会继续使用
Aop在Spring中的作用
提供声明式事务;允许用户自定义切面
- 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等.....
- 切面(ASPECT)︰横切关注点被模块化的特殊对象。即,它是一个类。
- 通知(Advice) :切面必须要完成的工作。即,它是类中的一个方法。·
- 目标(Target):被通知对象。
- 代理(Proxy) ︰向目标对象应用通知之后创建的对象。
- 切入点(PointCut) :切面通知执行的“地点""的定义。
- 连接点(JointPoint) : 与切入点匹配的执行点。
AOP 主要就是在不改变原本代码的前提下,新增功能上去不影响原本的功能。
AOP在Spring中是非常重要的一个功能,可以理解为一个业务就是一条线,当使用一把刀在这条线的指定位置砍下去,添加新的功能到断开处,最后在进行织入,最后连起来成为了一条新的线,新的功能就可以实现。
使用Spring实现Aop
添加依赖- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>5.3.18</version>
- </dependency>
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjrt</artifactId>
- <version>1.9.6</version>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>5.3.18</version>
- </dependency>
复制代码 注解实现
修改配置文件- <?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"
- xmlns:context="http://www.springframework.org/schema/context"
- 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xs">
-
- <context:component-scan base-package="com.bing"/>
-
- <aop:aspectj-autoproxy />
- </beans>
复制代码 添加一个切面类- package com.bing.aspect;
- import org.apache.log4j.Logger;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.*;
- import org.springframework.stereotype.Component;
- /**
- * @Author IBing
- * @Date 2022/8/26 22:01
- * @Version 1.0
- */
- @Aspect //表示这是一个 切面类
- @Component //将对象放入spring容器
- public class UserAspect {
- //开启日志
- private Logger logger = Logger.getLogger(UserAspect.class);
- /**
- * 目标方法执行之前执行
- * @param joinPoint
- */
- @Before("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
- public void before(JoinPoint joinPoint){
- logger.debug("before");
- logger.debug("拦截的目标对象"+joinPoint.getTarget());
- logger.debug("拦截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
- logger.debug("拦截的参数"+joinPoint.getArgs());
- logger.debug("拦截的位置"+joinPoint.getStaticPart());
- logger.debug("拦截的代理对象"+joinPoint.getStaticPart());
- }
- /**
- * 无论是否抛出异常都会执行,相当于finally
- * @param joinPoint
- */
- @After("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
- public void after(JoinPoint joinPoint){
- logger.debug("after");
- logger.debug("拦截的目标对象"+joinPoint.getTarget());
- logger.debug("拦截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
- logger.debug("拦截的参数"+joinPoint.getArgs());
- logger.debug("拦截的位置"+joinPoint.getStaticPart());
- logger.debug("拦截的代理对象"+joinPoint.getStaticPart());
- }
- /**
- * 相当于try
- * @param joinPoint
- */
- @AfterReturning("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
- public void afterReturning(JoinPoint joinPoint){
- logger.debug("afterReturning");
- logger.debug("拦截的目标对象"+joinPoint.getTarget());
- logger.debug("拦截的方法"+joinPoint.getSignature().getDeclaringTypeName()+joinPoint.getSignature().getName());
- logger.debug("拦截的参数"+joinPoint.getArgs());
- logger.debug("拦截的位置"+joinPoint.getStaticPart());
- logger.debug("拦截的代理对象"+joinPoint.getStaticPart());
- }
- /**
- * 相当于catch
- * @param joinPoint
- * @param e
- */
- @AfterThrowing(value="execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))",throwing ="e" )
- public void afterThrowing(JoinPoint joinPoint,RuntimeException e){
- logger.debug("afterThrowing");
- logger.debug("拦截的目标对象"+joinPoint.getTarget());
- logger.debug("抛出的异常为",e);
- }
- /**
- * 环绕执行
- * @param pjd
- * @return
- * @throws Throwable
- */
- @Around("execution(public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))")
- public Object around(ProceedingJoinPoint pjd) throws Throwable {
- logger.debug("around之前");
- Object proceed= pjd.proceed();
- logger.debug("around之后");
- return proceed;
- }
- }
复制代码 测试- @Test
- public void test( ){
- //创建spring的容器并初始化
- ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
- String[] beanNameForType = classPathXmlApplicationContext.getBeanNamesForType(UserController.class);
- System.out.println(Arrays.toString(beanNameForType));
- UserController bean = (UserController) classPathXmlApplicationContext.getBean("userController");
- bean.login("aa","123"); //这是 UserController 里写好的login方法
- classPathXmlApplicationContext.close();
- }
复制代码 输出结果

因为没有异常,所以 **afterThrowing **方法没有执行
下面是一些切入点语法
- 直接精准到一个方法上面去
- execution( public com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
- 任意权限修饰符
- execution( com.bing.entity.User com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
- 无返回类型
- execution( void com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
- 有返回类型
- execution( !void com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
- 任意返回类型
- execution( * com.bing.service.impl.UserServiceImpl.login(java.lang.String,java.lang.String))
- 任意参数
- execution( * com.bing.service.impl.UserServiceImpl.login(..))
- 类中的任意方法
- execution( * com.bing.service.impl.UserServiceImpl.*(..))
- 类中以指定内容开头的方法
- execution( * com.bing.service.impl.UserServiceImpl.select*(..))
- 包中的任意类的任意方法不包含子包下面的类
- execution( * com.bing.service.impl.*.*(..))
- 包中及其下的任意类的任意方法
- execution( * com.bing.service..*.*(..))
复制代码 AOP还可以用XML方法实现,这里就不演示了,如果大家有兴趣,可以自行百度实现
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |