Spring案例(百度网盘提取码数据兼容处理)

百度网盘分享连接输入密码数据错误兼容处理

🚸🚸
需求:对百度网盘分享链接输入提取码时尾部多输入的空格做兼容处理

在这里插入图片描述
🎊🎊️️
分析
:在业务方法执行之前对所有的输入参数进行格式处理—— trim()
【trim是String类型数据的一个方法,Java中的<font color=red> trim() </font>⽅法是⽤于删除字符串的头尾空⽩符(只有头和尾),返回值是删除头尾空⽩符的字符串】

:使用处理后的参数调用原始方法——环绕通知中存在对原始方法的调用

🍭🍭
实现

在这里插入图片描述
【读取链接与提取码并返回提取码的布尔值】

package com.GY.dao;

public interface dataDao {
     boolean readdata(String url,String password) ;

}
package com.GY.dao;

import org.springframework.stereotype.Repository;

@Repository
public class dataDaoImpl implements dataDao {
    @Override
    public boolean readdata(String url, String password) {
        return password.equals("123456");
    }
}

【判断链接与提取码是否正确】

package com.GY.service;

public interface dataService {

    boolean URL(String url,String password);
}
package com.GY.service;

import com.GY.dao.dataDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class dataServiceImpl implements dataService{

    @Autowired
    private dataDao  data;

    public boolean URL(String url, String password) {

        return data.readdata(url,password);
    }
}

【Spring配置类】

package com.GY.config;

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


@Configuration
@ComponentScan("com.*")
@EnableAspectJAutoProxy
public class SpringConfig {

}

【Spring切面】

import org.springframework.stereotype.Component;

@Component
@Aspect
public class Advice {
    @Pointcut("execution( boolean com.GY.service.dataService.URL(*,*))")
    private void G(){}

    @Around("G()")
    public Object method(ProceedingJoinPoint P) throws Throwable {
        //取出参数(若有空格则去掉)
        Object[]arg= P.getArgs();
        for (int i = 0; i <arg.length ; i++) {
            //判断参数是否为字符串
           if( arg[i].getClass().equals(String.class)){
               arg[i]=arg[i].toString().trim();
        }
        }
      //将修改完的参数放回
      Object O= P.proceed(arg);
      return O;
    }

}
import com.GY.config.SpringConfig;
import com.GY.service.dataService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class GY {
    public static void main(String[] args) {
        ApplicationContext A=new AnnotationConfigApplicationContext(SpringConfig.class);
        dataService dataService=A.getBean(com.GY.service.dataService.class);
        Boolean GY=dataService.URL("http://www.GY.com","123456 ");
        System.out.println(GY);
    }
}

🛳️🛳️
结果:(提取码首尾存在空格时也可正常进行提取)
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

new一个对象_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值