0

I am trying to use a request scoped been inside my servlet 3.0 application.

I am not using the web.xml but an implementation of WebApplicationInitializer. The onStartup method looks like this:

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext applicationContext =
            new AnnotationConfigWebApplicationContext();

    applicationContext.setServletContext(servletContext);
    applicationContext.scan("package containing proxy scoped bean and other stuff");

    applicationContext.refresh();        

    servletContext.addListener(new ContextLoaderListener(applicationContext));
    servletContext.addListener(new RequestContextListener());
}

and the request scoped bean looks like this:

@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) 
public class CallerInformation {

    private String clientIp;

    public String getClientIp() {
        return clientIp;
    }

    public void setClientIp(String clientIp) {
        this.clientIp = clientIp;
    }
}

Now the injected "CallerInformation" is not a CGLIB-proxy but behaves like prototype scoped, it is a different instance in every class and it does not hold any information through the request...

Any ideas what I am doing wrong?

EDIT:

I have tried the same scenario with servlet 2.5 and web.xml config and it worked like hell ;)

woezelmann
  • 1,355
  • 2
  • 19
  • 39
  • I don't know what you mean. – woezelmann Apr 25 '13 at 08:58
  • Have you tried it with servlet 3.0 and web.xml? try it also... – rahul maindargi Apr 25 '13 at 11:56
  • Also try to change the order as `servletContext.addListener(new RequestContextListener()); servletContext.addListener(new ContextLoaderListener(applicationContext));` Listerner orders can also do some problems . Not 100% sure thats the cause here.. ` – rahul maindargi Apr 25 '13 at 12:07
  • Why using proxyMode = ScopedProxyMode.TARGET_CLASS ? if you use proxyMode=ScopedProxyMode.INTERFACES it will fix your problem, it fixed the problem to me in this question. http://stackoverflow.com/questions/23724716/problems-with-bean-declared-as-scope-request-in-servlet-3-0-configuration – alexzm1 May 19 '14 at 03:47

1 Answers1

0

the behavior is right. the instance only lives the one request. see this site for further information.

try to inject the spring-component with the @Autowired annotation

Philipp Sander
  • 10,139
  • 6
  • 45
  • 78