Java開發學習(十九)—-AOP環繞通知案例之密碼數據兼容處理
一、需求分析
需求: 對百度網盤分享鏈接輸入密碼時尾部多輸入的空格做兼容處理。
問題描述:
-
點擊鏈接,會提示,請輸入提取碼,如下圖所示
-
當我們從別人發給我們的內容中複製提取碼的時候,有時候會多複製到一些空格,直接粘貼到百度的提取碼輸入框
-
但是百度那邊記錄的提取碼是沒有空格的
-
這個時候如果不做處理,直接對比的話,就會引發提取碼不一致,導致無法訪問百度盤上的內容
-
所以多輸入一個空格可能會導致項目的功能無法正常使用。
-
此時我們就想能不能將輸入的參數先幫用戶去掉空格再操作呢?
答案是可以的,我們只需要在業務方法執行之前對所有的輸入參數進行格式處理——trim()
-
是對所有的參數都需要去除空格么?
也沒有必要,一般只需要針對字符串處理即可。
-
以後涉及到需要去除前後空格的業務可能會有很多,這個去空格的代碼是每個業務都寫么?
可以考慮使用AOP來統一處理。
-
AOP有五種通知類型,該使用哪種呢?
我們的需求是將原始方法的參數處理後在參與原始方法的調用,能做這件事的就只有環繞通知。
綜上所述,我們需要考慮兩件事: ①:在業務方法執行之前對所有的輸入參數進行格式處理——trim() ②:使用處理後的參數調用原始方法——環繞通知中存在對原始方法的調用
二、環境準備
-
創建一個Maven項目
-
pom.xml添加Spring依賴
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.10.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> </dependencies>
-
添加ResourcesService,ResourcesServiceImpl,ResourcesDao和ResourcesDaoImpl類
public interface ResourcesDao { boolean readResources(String url, String password); } @Repository public 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); } @Service public class ResourcesServiceImpl implements ResourcesService { @Autowired private ResourcesDao resourcesDao; public boolean openURL(String url, String password) { return resourcesDao.readResources(url,password); } }
-
創建Spring的配置類
@Configuration @ComponentScan("com.itheima") public class SpringConfig { }
-
編寫App運行類
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("//pan.baidu.com/haha", "root"); System.out.println(flag); } }
最終創建好的項目結構如下:
現在項目的效果是,當輸入密碼為”root”控制台打印為true,如果密碼改為”root “控制台打印的是false
需求是使用AOP將參數進行統一處理,不管輸入的密碼root
前後包含多少個空格,最終控制台打印的都是true。
三、具體實現
步驟1:開啟SpringAOP的註解功能
@Configuration
@ComponentScan("com.itheima")
@EnableAspectJAutoProxy
public class SpringConfig {
}
步驟2:編寫通知類
@Component
@Aspect
public class DataAdvice {
@Pointcut("execution(boolean com.itheima.service.*Service.*(*,*))")
private void servicePt(){}
}
步驟3:添加環繞通知
@Component
@Aspect
public 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
@Aspect
public 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類,在方法中將密碼的長度進行打印
@Repository
public class ResourcesDaoImpl implements ResourcesDao {
public boolean readResources(String url, String password) {
System.out.println(password.length());
//模擬校驗
return password.equals("root");
}
}
再次運行成功,就可以根據最終打印的長度來看看,字符串的空格有沒有被去除掉。
注意: