zoukankan      html  css  js  c++  java
  • 使用spring AOP获得session的思路

    由于Spring 的AOP面向切面编程,与Servlet容器没有任何关联,所以想要获得Session会话比较麻烦。
    当然Struts2同样不依赖Servlet容器,可以在Spring AOP中可以使用com.opensymphony.xwork2.ActionContext,就可以获得Session。
    但是在Servlet中或struts1中,可以通过ThreadLocal方式将session保存,Spring AOP中获得Session对象。
    具体思路和代码:
     
    这个是保存request和session的类
    /**
     * 
     * @author 株洲健坤北大青鸟 周钢
     *
     */
    public class SysContent {  
         private static ThreadLocal<HttpServletRequest> requestLocal= new ThreadLocal<HttpServletRequest>();  
         private static ThreadLocal<HttpServletResponse> responseLocal= new ThreadLocal<HttpServletResponse>();  
           
        public static HttpServletRequest getRequest() {  
            return (HttpServletRequest)requestLocal.get();  
        }  
        public static void setRequest(HttpServletRequest request) {  
            requestLocal.set(request);  
        }  
        public static HttpServletResponse getResponse() {  
            return (HttpServletResponse)responseLocal.get();  
        }  
        public static void setResponse(HttpServletResponse response) {  
            responseLocal.set(response);  
        }  
        public static HttpSession getSession() {  
            return (HttpSession)((HttpServletRequest)requestLocal.get()).getSession();  
        }  
    }
     
     
    这个是配置的过滤器
     
    /**
     * 
     * @author 株洲健坤北大青鸟 周钢
     *
     */
    public class GetContent implements Filter {  
        @Override  
        public void destroy() {  
            // TODO Auto-generated method stub  
        }  
        @Override  
        public void doFilter(ServletRequest arg0, ServletResponse arg1,  
                FilterChain arg2) throws IOException, ServletException {  
            SysContent.setRequest((HttpServletRequest) arg0);  
            SysContent.setResponse((HttpServletResponse) arg1);  
            arg2.doFilter(arg0, arg1);  
        }  
        @Override  
        public void init(FilterConfig arg0) throws ServletException {  
            // TODO Auto-generated method stub  
        }  
    }  
     
     
     
    使用AOP进行环绕通知切入所有com.aptech.service包下的所有类的方法
     
    /**
     * @author 株洲健坤北大青鸟 周钢
     */
    @Aspect
    public Class AopTest{
    @Around(value="execution(* com.aptech.service.*.*(..))")
    public void aroundTest(ProceedingJoinPoint pj) throws Exception {  
              HttpServletRequest request = SysContent.getRequest();  
              HttpServletResponse response = SysContent.getResponse();  
              HttpSession session = SysContent.getSession();  
              //其他操作
              if(true){
               pj.proceed();
              }
              throw new Exception("您没有该权限");
        } 
    }
  • 相关阅读:
    递归运用实例
    动手动脑(&课后实验):生成随机数,函数的重载
    求一个数字的每位上的数字并求和
    要求根据RandomStr.java:使用类型转换生成六位验证字符串,示例程序每次运 行时,都会生成不同的字符串。
    Java语言基础问题
    从命令行接收多个数字,求和输出结果
    《大道至简》第一章读后感Java伪代码
    《大道至简》读后感
    怎样关闭WIN7系统的自动更新
    Mongodb集群搭建的三种方式
  • 原文地址:https://www.cnblogs.com/netcorner/p/4346190.html
Copyright © 2011-2022 走看看