菜鳥學習Spring——利用JoinPoint獲取參數的值和方法名稱

    

一、概述

      AOP的實現方法在上兩篇博客中已經用了兩種方法來實現現在的問題來了雖然我們利用AOP,那麼客户端如何信息傳遞?利用JoinPoint接口來實現客户端給具體實現類的傳遞參數。

二、代碼演示。

目錄結構:

JoinPoint能否獲取到request_spring


SecurityHandler.java




[java] view plain copy  



1. package com.tgb.spring;  
2.   
3. import org.aspectj.lang.JoinPoint;  
4.   
5.   
6. public class SecurityHandler{  
7.   
8.   
9.     private void checkSecurity(JoinPoint joinPoint){  
10.         for (int i = 0; i < joinPoint.getArgs().length; i++) {  
11.             System.out.println(joinPoint.getArgs()[i]);  
12.         }  
13.         System.out.println(joinPoint.getSignature().getName());  
14.           
15.         System.out.println("=====checkSecurity====");  
16.   
17.     }  
18.   
19.   
20. }




Client.java




[java] view plain copy  



1. package com.tgb.spring;  
2.   
3. import org.springframework.beans.factory.BeanFactory;  
4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
5.   
6. import com.tgb.spring.UserManager;  
7.   
8.   
9.   
10. public class Client {  
11.   
12.     public static void main(String[] args) {  
13.     BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");  
14.         UserManager userManager=(UserManager) factory.getBean("userManager");  
15.         userManager.addUser("張三", "123");  
16.         //userManager.delUser(1);  
17.           
18.     }  
19. }


UserManager.java




[java] view plain copy  


1. package com.tgb.spring;  
2.   
3. public interface UserManager {  
4.       
5.     public void addUser(String username,String password);  
6.       
7.     public void delUser(int userId);  
8.       
9.     public String findUserById(int userId);  
10.       
11.     public void modifyUser(int userId,String username,String password);  
12.       
13. }




UserManagerImpl.java




[java] view plain copy  



1. package com.tgb.spring;  
2.   
3. public class UserManagerImpl implements UserManager {  
4.   
5.     public void addUser(String username, String password) {  
6.         //checkSecurity();  
7.             System.out.println("===UserManager.addUser===");  
8.   
9.     }  
10.   
11.     public void delUser(int userId) {  
12.         //checkSecurity();  
13.         System.out.println("===UserManager.delUser===");  
14.   
15.     }  
16.   
17.     public String findUserById(int userId) {  
18.         //checkSecurity();  
19.         System.out.println("===UserManager.findUserById===");  
20.     return  "張三";  
21.     }  
22.   
23.     public void modifyUser(int userId, String username, String password) {  
24.         //checkSecurity();  
25.         System.out.println("===UserManager.modifyUser===");  
26.   
27.     }  
28.       
29. //  private void checkSecurity(){  
30. //      System.out.println("checkSecurity");  
31. //  
32. //  }  
33.   
34. }

applicationContext.xml

[html] view plain copy  

1. <?xml version="1.0" encoding="UTF-8"?>  
2.   
3.   
4. <beans xmlns="http://www.springframework.org/schema/beans"  
5.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
6.          xmlns:aop="http://www.springframework.org/schema/aop"  
7.          xmlns:tx="http://www.springframework.org/schema/tx"  
8.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
9.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
10.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
11.   
12.   
13. <bean id="userManager" class="com.tgb.spring.UserManagerImpl" />  
14. <bean id="securityHandler" class="com.tgb.spring.SecurityHandler"/>  
15. <aop:config>  
16.     <aop:aspect id="securityAspect" ref="securityHandler">  
17.   
18.          <aop:pointcut id="addAddMethod" expression="execution(* com.tgb.spring.*.*(..))" />  
19.         <aop:before method="checkSecurity" pointcut-ref="addAddMethod" />  
20.     </aop:aspect>  
21. </aop:config>  
22. </beans>



效果圖:

JoinPoint能否獲取到request_反射_02

三、總結。

我們可以通過Advice中添加一個JoinPoint參數,這個值會由spring自動傳入,從JoinPoint中可以取得。