0

I don't know what I am missing, checked all the links which tells about request-scoped bean creation but I am missing something that's why request-scoped bean is not creating.

Here is request scoped bean class:

package com.spring.beans;

public class RequestScopedBean 
{
    public RequestScopedBean()
    {
        System.out.println("RequestScopedBean constructor");
    }   
    @Autowired
    HttpServletRequest request;

    public void getSessionId()
    {
        if(request != null)
        {
            System.out.println(request.getSession().getId());
        }
    }
}

Web.xml

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>com.spring.systemaudit.OESLog4jListener</listener-class>
    </listener>
    <listener>
     <listener-class>se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor</listener-class>
    </listener>  
    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    ...
</web-app>

and root-context.xml

...
<bean id="requestScopedBean" class="com.spring.beans.RequestScopedBean" scope="request">

</bean>
...

If I make scope="singleton" then bean is created because I can see sysout on console while starting server then why not with scope="request". What I am missing?

Amogh
  • 4,453
  • 11
  • 45
  • 106

1 Answers1

2

I guess it's successfully created... but on request. The bean must be created on call a controller where the bean is used. E.g. when you type localhost:8080/someRequestMapping your controller is triggered and if the Controller has a reference to your bean it is created.

By default the reference is a Proxy which creates Request scoped beans on demand.

On startup no request exists so the bean is not created (only proxy is created)

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • But what if I am browsing the home page (HomeController) using `http://localhost:2020/SpringWeb/`then also I can't see any sysout on console. – Amogh Apr 11 '17 at 12:36
  • Ohh I got it I have to use it on HomeController something like `context.getBean("requestScopedBean");`. I got It :) – Amogh Apr 11 '17 at 12:40
  • Can I get your one more help in this regarding? I have modified class `RequestScopedBean` with `@Autowired HttpServletRequest request;` with a method to get session id but when I call to this method using `context.getBean(RequestScopedBean.class).getSessionId();` from a controller the `request` object is `null` always. bean class is modified in question – Amogh Apr 11 '17 at 14:05
  • Because HttpServletRequest is not a bean. Check this http://stackoverflow.com/questions/592123/is-there-a-static-way-to-get-the-httpservletrequest-of-the-current-request but request still could not be null. E.g when you try to access request from a Job – StanislavL Apr 11 '17 at 14:09
  • Oh! actually what I am trying to achieve here is we have a spring mvc project live from 2 years but due some business requirement we want to query on different table rather than current one depending upon a value in session.So Idea is injecting request object (req. bean) into that service class and check the session value lets say if session has a value 1 then query on `Table1` otherwise `Table2`. So that only query part will get modified rather service logic. – Amogh Apr 11 '17 at 14:27