• Spring AOP案例:百度网盘密码数据兼容处理 与 SpringAOP总结


    目录

    需求与分析:

    代码实现:

    AOP总结:


    需求与分析:

    需求:对百度网盘分享连接输入密码时尾部多输入的空格进行处理

    分析:

    1.在业务方法执行前对所有的输入参数进行格式处理——trim();

    2.使用处理后的参数调用原始方法——环绕通知(around)中存在对原始方法的调用

    代码实现:

    DataAdvise  AOP 通知类

    1. package com.itheima.aop;
    2. import org.aspectj.lang.ProceedingJoinPoint;
    3. import org.aspectj.lang.annotation.*;
    4. import org.springframework.stereotype.Component;
    5. @Component //让1配置类知道是bean
    6. @Aspect //让配置类知道是造Aop,去识别一下的内容
    7. public class DataAdvise {
    8. //定义切入点
    9. @Pointcut("execution(boolean com.itheima.service.ResourcesService.openURL(*,*))")
    10. private void servicePt(){}
    11. //连接切入点
    12. @Around("servicePt()")
    13. public Object trimStr(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    14. Object[] args = proceedingJoinPoint.getArgs();
    15. for (int i = 0; i < args.length; i++) {
    16. //判断参数是否时String
    17. if (args[i].getClass().equals(String.class)){
    18. args[i]=args[i].toString().trim();
    19. }else{
    20. System.out.println("不是字符串");
    21. }
    22. }
    23. Object proceed = proceedingJoinPoint.proceed(args);
    24. return proceed;
    25. }
    26. }

    SpringConfig  Spring配置类

    1. package com.itheima.config;
    2. import org.springframework.context.annotation.ComponentScan;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.context.annotation.EnableAspectJAutoProxy;
    5. @Configuration //说明此文件为Spring配置类
    6. @ComponentScan("com.itheima") //包扫描,加载bean
    7. @EnableAspectJAutoProxy //启动了MyAdvise内的Aspect注解,
    8. public class SpringConfig {
    9. }

    ResourcesDao  数据接口

    1. package com.itheima.dao;
    2. public interface ResourcesDao {
    3. boolean readResources(String url,String password);
    4. }

    ResourcesDaoImpl 

    1. package com.itheima.dao.impl;
    2. import com.itheima.dao.ResourcesDao;
    3. import org.springframework.stereotype.Repository;
    4. @Repository
    5. public class ResourcesDaoImpl implements ResourcesDao {
    6. @Override
    7. public boolean readResources(String url, String password) {
    8. System.out.println(password.length());
    9. //避免空指针
    10. return "root".equals(password);
    11. }
    12. }

    ResourcesService  数据操作

    1. package com.itheima.service;
    2. public interface ResourcesService {
    3. public boolean openURL(String url,String password);
    4. }

    ResourcesServiceImpl

    1. package com.itheima.service.impl;
    2. import com.itheima.dao.ResourcesDao;
    3. import com.itheima.service.ResourcesService;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Service;
    6. @Service
    7. public class ResourcesServiceImpl implements ResourcesService {
    8. @Autowired
    9. private ResourcesDao resourcesDao;
    10. @Override
    11. public boolean openURL(String url, String password) {
    12. return resourcesDao.readResources(url,password);
    13. }
    14. }

    App  main方法类

    1. package com.itheima;
    2. import com.itheima.config.SpringConfig;
    3. import com.itheima.service.ResourcesService;
    4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    5. public class App {
    6. public static void main(String[] args) {
    7. //获取Java配置类
    8. AnnotationConfigApplicationContext acct = new AnnotationConfigApplicationContext(SpringConfig.class);
    9. //获取bean
    10. ResourcesService bean = acct.getBean(ResourcesService.class);
    11. //获取方法
    12. boolean root = bean.openURL("http://www.baidu.com/haha", "root ");
    13. System.out.println(root);
    14. }
    15. }

    pom.xml文件

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>org.examplegroupId>
    7. <artifactId>Spring-aop-demo1artifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <properties>
    10. <maven.compiler.source>18maven.compiler.source>
    11. <maven.compiler.target>18maven.compiler.target>
    12. properties>
    13. <dependencies>
    14. <dependency>
    15. <groupId>org.springframeworkgroupId>
    16. <artifactId>spring-contextartifactId>
    17. <version>5.3.23version>
    18. dependency>
    19. <dependency>
    20. <groupId>javax.annotationgroupId>
    21. <artifactId>javax.annotation-apiartifactId>
    22. <version>1.3.2version>
    23. dependency>
    24. <dependency>
    25. <groupId>com.alibabagroupId>
    26. <artifactId>druidartifactId>
    27. <version>1.2.11version>
    28. dependency>
    29. <dependency>
    30. <groupId>org.mybatisgroupId>
    31. <artifactId>mybatisartifactId>
    32. <version>3.5.6version>
    33. dependency>
    34. <dependency>
    35. <groupId>mysqlgroupId>
    36. <artifactId>mysql-connector-javaartifactId>
    37. <version>5.1.46version>
    38. dependency>
    39. <dependency>
    40. <groupId>org.springframeworkgroupId>
    41. <artifactId>spring-jdbcartifactId>
    42. <version>5.0.0.RELEASEversion>
    43. dependency>
    44. <dependency>
    45. <groupId>org.mybatisgroupId>
    46. <artifactId>mybatis-springartifactId>
    47. <version>1.3.0version>
    48. dependency>
    49. <dependency>
    50. <groupId>junitgroupId>
    51. <artifactId>junitartifactId>
    52. <version>4.12version>
    53. <scope>testscope>
    54. dependency>
    55. <dependency>
    56. <groupId>org.springframeworkgroupId>
    57. <artifactId>spring-testartifactId>
    58. <version>5.2.10.RELEASEversion>
    59. dependency>
    60. <dependency>
    61. <groupId>org.aspectjgroupId>
    62. <artifactId>aspectjweaverartifactId>
    63. <version>1.8.8version>
    64. dependency>
    65. <dependency>
    66. <groupId>log4jgroupId>
    67. <artifactId>log4jartifactId>
    68. <version>1.2.17version>
    69. dependency>
    70. dependencies>
    71. project>

    AOP总结:

    概念:AOP(Aspect Oriented Programming)面向切面编程,一种编程范式

    作用:在不惊动原始代码的情况下,为代码添加功能

    核心概念:

        代理(Proxy):SpringAOP的核心本质是采用代理模式实现的

        连接点(JoinPoint):在SpringAOP中,理解为任意方法的执行

        切入点(Aspect):匹配连接点的式子,也是具有共性功能的方法描述

    Spring AOP切入点表达式+语法格式+通配符+书写技巧_我的猴子的博客-CSDN博客icon-default.png?t=M85Bhttps://blog.csdn.net/qq_51272114/article/details/127382125?spm=1001.2014.3001.5502

        通知(Advice):若干个方法的共性功能,在切入点执行,最终体现为一个方法

    Spring AOP通知的类型+通知的案例_我的猴子的博客-CSDN博客icon-default.png?t=M85Bhttps://blog.csdn.net/qq_51272114/article/details/127383556?spm=1001.2014.3001.5502

        切面(Aspect):描述通知与切入点的对应关系

        目标对象(Target):被代理的原始对象成为目标对象

  • 相关阅读:
    YbtOJ「基础算法」第1章 递推算法
    java 网络编程 总结篇
    『忘了再学』Shell基础 — 21、变量的测试与内容置换
    第三节:样式、选择器
    Lumiprobe丨Lumizol RNA 提取试剂解决方案
    安卓手机使用Tasker实现应用级功能,屏幕翻译v9,翻译&复制&贴图
    单链表经典OJ题:合并有序链表
    基于边缘网关的智慧工地监测方案
    gpio配置
    金仓数据库KingbaseES客户端编程接口指南-JDBC(5. JDBC 查询结果集处理)
  • 原文地址:https://blog.csdn.net/qq_51272114/article/details/127832266