当前位置:   article > 正文

初学Spring之 AOP 面向切面编程

初学Spring之 AOP 面向切面编程

AOP(Aspect Oriented Programming)面向切面编程

通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术

是面向对象(OOP)的延续

AOP 在 Spring 中的作用:

1.提供声明式事务

2.允许用户自定义切面

导入 jar 包,搜索 AspectJ Weaver

(复制过来把 <scope>runtime</scope> 这行删去,不然注解报错)

  1. <dependency>
  2. <groupId>org.aspectj</groupId>
  3. <artifactId>aspectjweaver</artifactId>
  4. <version>1.9.22.1</version>
  5. </dependency>

方法一:使用原生 Spring API 接口实现

先写个 UserService 接口:

  1. package com.demo.service;
  2. public interface UserService {
  3. public void add();
  4. public void delete();
  5. public void update();
  6. public void select();
  7. }

再写个 UserServiceImpl 类来实现接口,重写方法

  1. package com.demo.service;
  2. public class UserServiceImpl implements UserService{
  3. @Override
  4. public void add() {
  5. System.out.println("增");
  6. }
  7. @Override
  8. public void delete() {
  9. System.out.println("删");
  10. }
  11. @Override
  12. public void update() {
  13. System.out.println("改");
  14. }
  15. @Override
  16. public void select() {
  17. System.out.println("查");
  18. }
  19. }

写个 Log 类来实现 MethodBeforeAdvice 接口(前置日志)

method:要执行的目标对象的方法

args:参数

target:目标对象

  1. package com.demo.log;
  2. import org.springframework.aop.MethodBeforeAdvice;
  3. import java.lang.reflect.Method;
  4. public class Log implements MethodBeforeAdvice {
  5. /*
  6. method:要执行的目标对象的方法
  7. args:参数
  8. target:目标对象
  9. */
  10. @Override
  11. public void before(Method method, Object[] args, Object target) throws Throwable {
  12. System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了");
  13. }
  14. }

再写个 AfterLog 类来实现 AfterReturningAdvice 接口(后置日志)

returnValue 返回值

  1. package com.demo.log;
  2. import org.springframework.aop.AfterReturningAdvice;
  3. import java.lang.reflect.Method;
  4. public class AfterLog implements AfterReturningAdvice {
  5. //returnValue返回值
  6. @Override
  7. public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
  8. System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
  9. }
  10. }

配置 applicationContext.xml 文件

注册 bean

配置 aop:需要导入 aop 的约束

xmlns:aop="http://www.springframework.org/schema/aop"

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

使用原生 Spring API 接口

<aop:config>

 切入点:expression:表达式,execution(要执行的位置:修饰词 返回值 类名 方法名 参数)

<aop:pointcut id="xx" expression="execution(* com.demo.service.UserServiceImpl.*(..))"/>

执行环绕增加

<aop:advisor advice-ref="xx" pointcut-ref="xx"/>

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:c="http://www.springframework.org/schema/c"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xmlns:util="http://www.springframework.org/schema/util"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  11. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/util
  13. http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  14. <!-- 注册bean -->
  15. <bean id="user" class="com.demo.service.UserServiceImpl"/>
  16. <bean id="log" class="com.demo.log.Log"/>
  17. <bean id="afterLog" class="com.demo.log.AfterLog"/>
  18. <!-- 使用原生Spring API接口 -->
  19. <!-- 配置aop:需要导入aop的约束 -->
  20. <aop:config>
  21. <!-- 切入点:expression:表达式,execution(要执行的位置:修饰词 返回值 类名 方法名 参数) -->
  22. <aop:pointcut id="pointcut" expression="execution(* com.demo.service.UserServiceImpl.*(..))"/>
  23. <!-- 执行环绕增加 -->
  24. <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
  25. <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
  26. </aop:config>
  27. </beans>

写个测试类

动态代理,代理的是接口

  1. import com.demo.service.UserService;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MyTest {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  7. //动态代理,代理的是接口
  8. UserService userService = (UserService) context.getBean("user");
  9. userService.add();
  10. }
  11. }

执行结果如下:

方法二:自定义类来实现 AOP(切面定义)

增加一个自定义的类

  1. package com.demo.pointcut;
  2. public class PointCut {
  3. public void before(){
  4. System.out.println("方法执行前");
  5. }
  6. public void after(){
  7. System.out.println("方法执行后");
  8. }
  9. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:c="http://www.springframework.org/schema/c"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xmlns:util="http://www.springframework.org/schema/util"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  11. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/util
  13. http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  14. <!-- 注册bean -->
  15. <bean id="user" class="com.demo.service.UserServiceImpl"/>
  16. <bean id="log" class="com.demo.log.Log"/>
  17. <bean id="afterLog" class="com.demo.log.AfterLog"/>
  18. <!-- 自定义类 -->
  19. <bean id="diy" class="com.demo.pointcut.PointCut"/>
  20. <aop:config>
  21. <!-- 自定义切面,ref:要引用的类 -->
  22. <aop:aspect ref="diy">
  23. <!-- 切入点 -->
  24. <aop:pointcut id="pointcut" expression="execution(* com.demo.service.UserServiceImpl.*(..))"/>
  25. <!-- 通知 -->
  26. <aop:before method="before" pointcut-ref="pointcut"/>
  27. <aop:after method="after" pointcut-ref="pointcut"/>
  28. </aop:aspect>
  29. </aop:config>
  30. </beans>

 自定义切面,ref:要引用的类

<aop:aspect ref="xx">

切入点:

<aop:pointcut id="pointcut" expression="execution(* com.demo.service.UserServiceImpl.*(..))"/>

通知:

<aop:before method="xx" pointcut-ref="xx"/>

测试类不变,执行结果如下:

方法三:使用注解实现 AOP

注意导入包不要导错

  1. package com.demo.pointcut;
  2. //使用注解方式实现AOP
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.aspectj.lang.Signature;
  5. import org.aspectj.lang.annotation.After;
  6. import org.aspectj.lang.annotation.Around;
  7. import org.aspectj.lang.annotation.Aspect;
  8. import org.aspectj.lang.annotation.Before;
  9. @Aspect //标注这个类是一个切面
  10. public class AnnotationPointCut {
  11. @Before("execution(* com.demo.service.UserServiceImpl.*(..))")
  12. public void before(){
  13. System.out.println("方法执行前");
  14. }
  15. @After("execution(* com.demo.service.UserServiceImpl.*(..))")
  16. public void after(){
  17. System.out.println("方法执行后");
  18. }
  19. //在环绕增强中,可以给定一个参数,代表要获取处理切入的点
  20. @Around("execution(* com.demo.service.UserServiceImpl.*(..))")
  21. public void around(ProceedingJoinPoint joinPoint) throws Throwable {
  22. System.out.println("环绕前");
  23. Signature signature = joinPoint.getSignature(); //获得签名
  24. System.out.println(signature);
  25. //执行方法
  26. Object proceed = joinPoint.proceed();
  27. System.out.println("环绕后");
  28. System.out.println(proceed);
  29. }
  30. }

开启注解支持:<aop:aspectj-autoproxy/>

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:c="http://www.springframework.org/schema/c"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xmlns:util="http://www.springframework.org/schema/util"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  11. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/util
  13. http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  14. <!-- 注册bean -->
  15. <bean id="user" class="com.demo.service.UserServiceImpl"/>
  16. <bean id="log" class="com.demo.log.Log"/>
  17. <bean id="afterLog" class="com.demo.log.AfterLog"/>
  18. <bean id="anno" class="com.demo.pointcut.AnnotationPointCut"/>
  19. <!-- 开启注解支持 -->
  20. <aop:aspectj-autoproxy/>
  21. </beans>

测试类不变,执行结果如下:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/876604
推荐阅读
相关标签
  

闽ICP备14008679号