Spring AOP(一)获取操作参数request、session、application(转http://liweitsky.iteye.com/blog/850832)

Coordinator
May 30, 2012 at 9:08 AM


springAOP的用途极其广泛,但在struts2之前request等获取是个问题,这里简单介绍一下我的方法:
struts1中两种方法:
1.通过threadlocal
先写java类
Java代码 复制代码 收藏代码
  1. package com.util;    
  2. import java.io.Serializable;    
  3.   
  4. import javax.servlet.http.HttpServletRequest;    
  5.   
  6.   
  7. public class ThreadTest implements Serializable{    
  8.       private static ThreadLocal<Object> threadLocal = new ThreadLocal<Object>();    
  9.   
  10. public HttpServletRequest getContext(){    
  11.       return (HttpServletRequest)threadLocal.get();    
  12. }    
  13. public void setContext(HttpServletRequest request){    
  14.       threadLocal.set(request);    
  15. }    
  16.   
  17. public void cleanContext(){    
  18.       threadLocal.set(null);    
  19. }    
  20. }   
package com.util; 
import java.io.Serializable; 

import javax.servlet.http.HttpServletRequest; 


public class ThreadTest implements Serializable{ 
      private static ThreadLocal<Object> threadLocal = new ThreadLocal<Object>(); 

public HttpServletRequest getContext(){ 
      return (HttpServletRequest)threadLocal.get(); 
} 
public void setContext(HttpServletRequest request){ 
      threadLocal.set(request); 
} 

public void cleanContext(){ 
      threadLocal.set(null); 
} 
} 

配置为filter
Java代码 复制代码 收藏代码
  1. <filter>   
  2.     <filter-name>test</filter-name>   
  3.     <filter-class>com.util.ThreadTest</filter-class>   
  4. </filter>   
  5. <filter-mapping>   
  6.     <filter-name>test</filter-name>   
  7.     <url-pattern>/*</url-pattern>   
  8. </filter-mapping>  
<filter>
	<filter-name>test</filter-name>
	<filter-class>com.util.ThreadTest</filter-class>
</filter>
<filter-mapping>
	<filter-name>test</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

最后在aop方法中只要包含HttpServletRequest request, HttpServletResponse response参数就可以了

方法二:
Java代码 复制代码 收藏代码
  1. public Object saveLog(ProceedingJoinPoint joinPoint) throws Throwable {   
  2.         Object retVal = null;   
  3.         try {   
  4.             if(logDao == null){   
  5.                 String[] locations={"applicationContext-beans.xml","applicationContext-actions.xml""applicationContext-common.xml"};   
  6.                 ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);   
  7.                 logDao = (LogDao) ctx.getBean("logDao");   
  8.             }   
  9.             //logDetail(jointPoint,":startTime:");   
  10.             retVal = joinPoint.proceed();   
  11.   
  12.             Object[] args = joinPoint.getArgs();   
  13.             HttpServletRequest request = null;   
  14.             ActionMapping mapping = null;   
  15.                         //通过分析aop监听参数分析出request等信息   
  16.             for (int i = 0; i < args.length; i++) {   
  17.                 if (args[i] instanceof HttpServletRequest) {   
  18.                     request = (HttpServletRequest) args[i];   
  19.                 }if (args[i] instanceof ActionMapping) {   
  20.                     mapping = (ActionMapping) args[i];   
  21.                 }   
  22.             }   
  23.             Person person = new Person();   
  24.             if (request != null) {   
  25.                 person = (Person) request.getSession().getAttribute("login");   
  26.             }   
  27.             String method = request.getParameter("method");   
  28.             System.out.println("------------------------------------"+SystemContext.getOffset());   
  29.             //登录日志   
  30.             if (method.equals("login")) {   
  31.             }   
  32.             //查看   
  33.             if (method.equals("showAll")) {   
  34.             }   
  35.             }   
  36.   
  37.             //logDetail(joinPoint,":endTime:");   
  38.         } catch (Exception ee) {   
  39.             System.out.println("出错:" + ee.toString());   
  40.         }   
  41.         return retVal;   
  42.     }  
public Object saveLog(ProceedingJoinPoint joinPoint) throws Throwable {
		Object retVal = null;
		try {
			if(logDao == null){
				String[] locations={"applicationContext-beans.xml","applicationContext-actions.xml", "applicationContext-common.xml"};
				ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
				logDao = (LogDao) ctx.getBean("logDao");
			}
			//logDetail(jointPoint,":startTime:");
			retVal = joinPoint.proceed();

			Object[] args = joinPoint.getArgs();
			HttpServletRequest request = null;
			ActionMapping mapping = null;
                        //通过分析aop监听参数分析出request等信息
			for (int i = 0; i < args.length; i++) {
				if (args[i] instanceof HttpServletRequest) {
					request = (HttpServletRequest) args[i];
				}if (args[i] instanceof ActionMapping) {
					mapping = (ActionMapping) args[i];
				}
			}
			Person person = new Person();
			if (request != null) {
				person = (Person) request.getSession().getAttribute("login");
			}
			String method = request.getParameter("method");
			System.out.println("------------------------------------"+SystemContext.getOffset());
			//登录日志
			if (method.equals("login")) {
			}
			//查看
			if (method.equals("showAll")) {
			}
			}

			//logDetail(joinPoint,":endTime:");
		} catch (Exception ee) {
			System.out.println("出错:" + ee.toString());
		}
		return retVal;
	}


在struts2中request等作为系统参数,已经可以直接调用了