1

If I create a bean annotated with @RequestScoped I expect that it will instantiate a new proxy instance with each new request.

From the other hand each request is associated with its own thread.

My question is: will CDI reuse previously created proxy object of my bean/service if the new request reuses previously created thread from a pool?

Andremoniy
  • 34,031
  • 20
  • 135
  • 241

1 Answers1

5

First of all, it's important to understand that client proxy object != bean instance (or contextual instance if we want to follow the spec wording). For @RequestScoped a new bean instance is always created for each request. However, @RequestScoped is a normal scope, which means that a client proxy is injected (and this proxy delegates to the bean instance).

Now back to your question to client proxy object - the strategy is implementation-specific. The spec states that a container might instantiate one client proxy object per bean and share it between multiple injection points. But it's not required (see also this documentation). Reference implementation (Weld, WildFly, GlassFish, etc.) DOES share client proxy objects. But again, we're speaking about client proxies, not bean instances.

WRT thread association - request context is associated with one thread but when the request ends the context is destroyed (incl. all bean instances) and the thread is dissociated.

Siliarus
  • 6,393
  • 1
  • 14
  • 30
Martin Kouba
  • 1,121
  • 5
  • 8