1. 程式人生 > >spring aop 獲取攔截類中的屬性的值

spring aop 獲取攔截類中的屬性的值

最近搭建一個ssh的通用架子時,想做個通用日誌的攔截方法,可以利用反射獲取攔截類中的屬性

不多說,上程式碼

日誌類

public class MyLog {
	public void doBefore(JoinPoint jp) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
	Field f = jp.getTarget().getClass().getDeclaredField("username");//獲取屬性-->Field
 	f.setAccessible(true);//如果是私有的 先要設定可訪問
	String user = (String) f.get(jp.getTarget());//獲取值,這個get()方法重點是引數,引數是你要獲取的類
	logStr = user + " 進行了" + jp.getTarget().getClass().getName() + " 類的 "+ jp.getSignature().getName()+" 方法開始執行 ";logger.info(logStr);}
	}
}



攔截類

 com.xxx.xxx.controller包下的ServerManagerController.java

public class ServerManagerController extends BaseAction{
	
	private String username = "";
	@Autowired

	/**
	 * 管理頁面  
	 * @return
	 */
	@RequestMapping(value = "/toServerManager")
    public String toServerManager(){ 
		UserLogInfo user = (UserLogInfo)session.getAttribute("userLogInfo");
		this.username = user.getUsername();
        return "/jsp/examine/serverManager";
    }
}
spring-log.xml
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    					http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    					http://www.springframework.org/schema/context  
           				http://www.springframework.org/schema/context/spring-context.xsd
           				http://www.springframework.org/schema/tx  
           				http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
           				http://www.springframework.org/schema/aop  
           				http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           				http://www.springframework.org/schema/mvc  
    					http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    					http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">  
    
    
    <!--   
    	配置日誌 使用Aop 記錄日誌    
    	注意:  
    	1). 此處proxy-target-class="true" 否則Action內的引數會被攔截掉,導致action內無法獲得前臺傳遞的引數   
    	2). Action需交給spring容器去管理  
	-->  
<aop:config expose-proxy="true" proxy-target-class="true">  
    <aop:aspect  id="aopRiZhiAspect" ref="myRiZhi">   
    <!-- 對哪些方法進行日誌記錄, 此處遮蔽action內的set get方法 -->  
           <aop:pointcut id="aopRiZhi" expression="(execution(* com.xxx.*.controller.*.*(..)) )" />   <!--  and (!execution(* com.hylanda.*.action.*.set*(..)) ) and (!execution(* com.hylanda.*.action.*.get*(..)) ) --> 
           <aop:before pointcut-ref="aopRiZhi" method="doBefore"/>    
           <aop:after pointcut-ref="aopRiZhi" method="doAfter"/>    
           <aop:around pointcut-ref="aopRiZhi" method="doAround"/>    
       </aop:aspect>  
</aop:config>  
  
  
<beans>  
    <bean id="myRiZhi" class="com.xxx.util.MyLog"></bean>   <!-- Aop日誌類 -->  
</beans> 
    
    
</beans>