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

目录

需求与分析:

代码实现:

AOP总结:


需求与分析:

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

分析:

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

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

代码实现:

DataAdvise  AOP 通知类

package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;


@Component    //让1配置类知道是bean
@Aspect       //让配置类知道是造Aop,去识别一下的内容
public class DataAdvise {

    //定义切入点
    @Pointcut("execution(boolean com.itheima.service.ResourcesService.openURL(*,*))")
    private void servicePt(){}

    //连接切入点
    @Around("servicePt()")
    public Object trimStr(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            //判断参数是否时String
            if (args[i].getClass().equals(String.class)){
                args[i]=args[i].toString().trim();
            }else{
                System.out.println("不是字符串");
            }
        }
        Object proceed = proceedingJoinPoint.proceed(args);
        return proceed;
    }

}

SpringConfig  Spring配置类

package com.itheima.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration                   //说明此文件为Spring配置类
@ComponentScan("com.itheima")    //包扫描,加载bean
@EnableAspectJAutoProxy          //启动了MyAdvise内的Aspect注解,
public class SpringConfig {
}

ResourcesDao  数据接口

package com.itheima.dao;

public interface ResourcesDao {

    boolean readResources(String url,String password);
}

ResourcesDaoImpl 

package com.itheima.dao.impl;

import com.itheima.dao.ResourcesDao;
import org.springframework.stereotype.Repository;

@Repository
public class ResourcesDaoImpl implements ResourcesDao {


    @Override
    public boolean readResources(String url, String password) {
        System.out.println(password.length());

        //避免空指针
        return "root".equals(password);
    }
}

ResourcesService  数据操作

package com.itheima.service;

public interface ResourcesService {

    public boolean openURL(String url,String password);

}

ResourcesServiceImpl

package com.itheima.service.impl;

import com.itheima.dao.ResourcesDao;
import com.itheima.service.ResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ResourcesServiceImpl implements ResourcesService {

    @Autowired
    private  ResourcesDao resourcesDao;

    @Override
    public boolean openURL(String url, String password) {
        return resourcesDao.readResources(url,password);
    }
}

App  main方法类

package com.itheima;

import com.itheima.config.SpringConfig;
import com.itheima.service.ResourcesService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class App {
    public static void main(String[] args) {

        //获取Java配置类
        AnnotationConfigApplicationContext acct = new AnnotationConfigApplicationContext(SpringConfig.class);

        //获取bean
        ResourcesService bean = acct.getBean(ResourcesService.class);

        //获取方法
        boolean root = bean.openURL("http://www.baidu.com/haha", "root   ");

        System.out.println(root);

    }
}

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Spring-aop-demo1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
    </properties>


    <dependencies>
        <!--springFramework依赖,,,,由于AOP包与Context有依赖关系,所以不需要在进行导入aop的包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.23</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.11</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!--        Spring操作数据库有关的都要导这个包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
        <!--        Spring 整合mybtis的包,这个包是和mybatis的包版本是相对应的-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--        junit包-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--        Spring 操作test的 包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
<!--        导入Aspect(切面)的包-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.8</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

    </dependencies>
</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):被代理的原始对象成为目标对象

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想给世界留下 1bite 的印象

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值