如何在controller中或得一个ApplicationContext 的对象,通过ApplicationContext 得到某一个bean 的值

1.  (1) 或得到request 的对象的值:(request 的值的或得必须要先有请求,比如:在main方法里面直接或得拿不到这值) 

      HttpServletRequest request =         ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            ServletContext servletContext = request.getSession().getServletContext();

     (2) 得到ApplicationContext 对象的实例:

            ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext ); 

    (3) 取出容器中的某一个bean 的值
            XzUserRechargeDao dao = (XzUserRechargeDao)ctx.getBean("xzUserRechargeDao");

    

猜你喜欢

转载自blog.csdn.net/qq_37469931/article/details/88169591