如何使用ApplicationContextAware调用声明的构造函数并传递HttpServletRequest和HttpServletResponse

时间:2015-01-12 15:13:35

标签: spring servlets constructor dependency-injection applicationcontext

我正在使用ApplicationContextAware从遗留代码中获取bean。我需要将两个参数传递给构造函数。当我使用WebApplicationContextUtils时很容易,但我没有找到如何使用ApplicationContextAware做同样的事情。基本上,我需要将HttpServletRequest请求和HttpServletResponse响应传递给从ApplicationContextAware检索的bean的构造函数。

//使用WebApplicationContextUtils

public class ClassA extends HttpServlet{
       private void methodA(HttpServletRequest request,HttpServletResponse response){
          Lo_DisplayHandler display = null;

          ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

          display = context.getBean(Lo_DisplayHandler.class, request, response);
       }
}

//使用ApplicationContextAware

/*
Although it doesn’t extend HttpServlet this class receives HttpServletRequest request and HttpServletResponse response in its constructor and I want to pass them to a bean retrieved from ApplicationContextAware 
*/
public class ClassB { 

       private HttpServletRequest request;
       private HttpServletResponse response;

       public ClassB (HttpServletRequest request,HttpServletResponse response) {

              this.request  = request;
              this.response = response;
       }

       public void methodB(){

       Lo_DisplayHandler handler = null;

       //the doubt is here: How can I pass request,response to the constructor
       handler = (Lo_DisplayHandler) SpringApplicationContext.getBean("lo_DisplayHandler");
}

//调用的bean

@Lazy(value=true)
public class Lo_DisplayHandler {
       //how call this constructor from SpringApplicationContext.getBean?
       public Lo_DisplayHandler(HttpServletRequest request,HttpServletResponse response) {

//使用ApplicationContextAware

public class SpringApplicationContext implements ApplicationContextAware {
  private static ApplicationContext CONTEXT;
  public void setApplicationContext(ApplicationContext context) throws BeansException {
    CONTEXT = context;
  }

  public static Object getBean(String beanName) {
    return CONTEXT.getBean(beanName);
  }
}

// aplicationContext.xml

      <bean id="springApplicationContext" class="com.myCompany.mhe.util.context.SpringApplicationContext"/>
      <bean id="lo_DisplayHandler" class="com.myCompany.mhe.log.handler.Lo_DisplayHandler" scope="prototype" lazy-init="true"/>

// web.xml中

<servlet>
              <servlet-name>mvc-dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
       </servlet>
       <servlet-mapping>
              <servlet-name>mvc-dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
       </servlet-mapping>
       <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/applicationContext.xml</param-value>
       </context-param>
       <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>

//找到解决方案

public static Object getBean(String beanName, HttpServletRequest request,HttpServletResponse response) {
      //OPTION 1
      //return CONTEXT.getBean( beanName, request, response);

       //OPTION 2
       BeanFactory factory = (BeanFactory) CONTEXT;
       return (Lo_DisplayHandler)factory.getBean("lo_DisplayHandler", request,response);

  }

0 个答案:

没有答案