需求: 对百度网盘分享链接输入密码时尾部多输入的空格做兼容处理。
问题描述:
答案是可以的,我们只需要在业务方法执行之前对所有的输入参数进行格式处理——trim()
也没有必要,一般只需要针对字符串处理即可。
可以考虑使用AOP来统一处理。
我们的需求是将原始方法的参数处理后在参与原始方法的调用,能做这件事的就只有环绕通知。
综上所述,我们需要考虑两件事: ①:在业务方法执行之前对所有的输入参数进行格式处理——trim() ②:使用处理后的参数调用原始方法——环绕通知中存在对原始方法的调用
org.springframework spring-context 5.2.10.RELEASE org.aspectj aspectjweaver 1.9.4
public interface ResourcesDao { boolean readResources(String url, String password);}@Repositorypublic class ResourcesDaoImpl implements ResourcesDao { public boolean readResources(String url, String password) { //模拟校验 return password.equals("root"); }}public interface ResourcesService { public boolean openURL(String url ,String password);}@Servicepublic class ResourcesServiceImpl implements ResourcesService { @Autowired private ResourcesDao resourcesDao; public boolean openURL(String url, String password) { return resourcesDao.readResources(url,password); }}
@Configuration@ComponentScan("com.itheima")public class SpringConfig {}
public class App { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); ResourcesService resourcesService = ctx.getBean(ResourcesService.class); boolean flag = resourcesService.openURL("http://pan.baidu.com/haha", "root"); System.out.println(flag); }}
最终创建好的项目结构如下:
现在项目的效果是,当输入密码为"root"控制台打印为true,如果密码改为"root "控制台打印的是false
需求是使用AOP将参数进行统一处理,不管输入的密码 root 前后包含多少个空格,最终控制台打印的都是true。
三、具体实现
步骤1:开启SpringAOP的注解功能
@Configuration@ComponentScan("com.itheima")@EnableAspectJAutoProxypublic class SpringConfig {}
步骤2:编写通知类
@Component@Aspectpublic class DataAdvice { @Pointcut("execution(boolean com.itheima.service.*Service.*(*,*))") private void servicePt(){} }
步骤3:添加环绕通知
@Component@Aspectpublic class DataAdvice { @Pointcut("execution(boolean com.itheima.service.*Service.*(*,*))") private void servicePt(){} @Around("DataAdvice.servicePt()") // @Around("servicePt()")这两种写法都对 public Object trimStr(ProceedingJoinPoint pjp) throws Throwable { Object ret = pjp.proceed(); return ret; } }
步骤4:完成核心业务,处理参数中的空格
@Component@Aspectpublic class DataAdvice { @Pointcut("execution(boolean com.itheima.service.*Service.*(*,*))") private void servicePt(){} @Around("DataAdvice.servicePt()") // @Around("servicePt()")这两种写法都对 public Object trimStr(ProceedingJoinPoint pjp) throws Throwable { //获取原始方法的参数 Object[] args = pjp.getArgs(); for (int i = 0; i < args.length; i++) { //判断参数是不是字符串 if(args[i].getClass().equals(String.class)){ args[i] = args[i].toString().trim(); } } //将修改后的参数传入到原始方法的执行中 Object ret = pjp.proceed(args); return ret; } }
步骤5:运行程序
不管密码 root 前后是否加空格,最终控制台打印的都是true
步骤6:优化测试
为了能更好的看出AOP已经生效,我们可以修改ResourcesImpl类,在方法中将密码的长度进行打印
@Repositorypublic class ResourcesDaoImpl implements ResourcesDao { public boolean readResources(String url, String password) { System.out.println(password.length()); //模拟校验 return password.equals("root"); }}
再次运行成功,就可以根据最终打印的长度来看看,字符串的空格有没有被去除掉。
注意:
原文链接:https://www.cnblogs.com/xiaoyh/p/16412341.html
留言与评论(共有 0 条评论) “” |