Spring Boot HttpServletRequest Request Scope Bean? - spring

It would seem that HttpServletRequest is a request scope bean, but I have not yet been able to find any documentation on this.
Does anyone know where this is documented or where in the code base it is created?

The HttpServletRequest is normally created and managed by servlet container (e.g. Tomcat) but not Spring. You normally do not need to define it as a spring bean unless you are doing something special. So technically from the spring 's point of view, the HttpServletRequest does not have any scope as it is not a spring bean.
But even if you need to define a HttpServletRequest bean for some reason, by default it will be in the singleton scope. (see the scope table in this).
The relationship between HttpServletRequest and a request scope bean is that Spring will make sure whenever the servlet container process a new HttpServletRequest , it will create a new request scope bean instance in case you need to access it during processing this HttpServletRequest. And this request scope bean will be destroyed after the servlet container finish process that HttpServletRequest. Such behaviour is also mentioned in the above link as :
Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext

Related

Need for Bean scopes in Spring frameowrk

I have read that there are several types of bean scopes in Spring framework when declaring them. However, I do not understand why we need several types. Can anyone explain what is the need of having Bean scopes in a Spring application with some examples?
Thank you in Advance!
Spring Inversion of Control Container (Ioc container) creates and manages the beans in a Spring Application.With Each declared Spring Bean, we can provide metadata which specifies that number of instances of particular bean should get created and how long they should live i.e. life time of the bean.
Basically with plain Java we our-self creates object. Position where we create object decides it's lifetime for example object created within method is destroyed as soon as method is returned. But in case of Spring, v creates bean for us and to manage the life cycle of the bean Spring uses scope of the bean. It also provide flexibility to the developer to override default scope which is "Singleton". 
Now why do we need several types ?
Simply because each bean could have its own lifetime. Depending upon the lifetime it has scope defined as below
singleton (Default) Scopes a single bean definition to a single object instance per Spring IoC container.
prototype Scopes a single bean definition to any number of object instances.
request Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
session Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
application Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocket Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
For example :
Singleton : objects like controller, service, repository required only one instance to be existed in the application.
Prototype : objects like Student, Product which might need to be created everytime you wanted to create new resource.
Remaining are WebAware scopes and self explanatory.

No FacesContext found in JoinFaces ViewScope

We are currently migrating a rather big project from JavaEE (Wildfly) to Spring Boot 2.0.5 using JoinFaces 3.2.5 for JSF support. Unfortunately when starting the server we always get the following message:
Scope 'view' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No FacesContext found.
The problematic UI bean is a Spring Component additionally annotated with javax.faces.view.ViewScoped (like class StarterMBean in the joinfaces-maven-jar-example).
Is there anything special we have to be careful about, e.g. forbidden dependencies, special configurations etc?
We are thankful for every hint!
You have an singleton/application scoped bean which has a direct or indirect dependency on a view scoped bean. This forces the BeanFactory to construct the view scoped bean when the application starts, but view scoped beans can only be used in threads which are currently processing a JSF request.
There are multiple ways to solve this problem:
Try to model your beans to only have dependencies to beans with the same or a higher scope. (So application scoped beans can only use application scoped beans, view scoped beans can use view, session or application scoped ones and so on)
When you are 100% sure your application scoped bean will only use the view scoped one during the processing of a JSF request you can automatically or manually wrap the bean in a scoped proxy.
To get a scoped proxy automcatically, change #ViewScoped to #Scope(scopeName = "view", proxyMode = ScopedProxyMode.TARGET_CLASS)
If you have no access to the view scoped bean, you can declare the injection point as ObjectProvider<> in order to get a scoped proxy.
More information about this problem can be found here: https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-scopes-other-injection

Does an Object in Spring created every time we write getBean()?

I have read that whenever we do getBean() in spring, it returns desired object.
So does it mean, if i write call getBean() 1000 times, thousand object will be created ??
If yes, Than how Spring manages these objects ?
if No, Please explain how Spring works with respect to object creation ?
Is there something Object pool kind of concept ?
Please clarify my doubts. I am new to spring and is very confused whether spring framework is created to make our task easy or to make things more complicated.
Spring seems to be a web of XMLs :(
From the Spring Framework documentation on singleton bean factory scope:
The singleton scope
Only one shared instance of a singleton bean is managed, and all
requests for beans with an id or ids matching that bean definition
result in that one specific bean instance being returned by the Spring
container.
To put it another way, when you define a bean definition and it is
scoped as a singleton, the Spring IoC container creates exactly one
instance of the object defined by that bean definition. This single
instance is stored in a cache of such singleton beans, and all
subsequent requests and references for that named bean return the
cached object.
To sum it up, no, Spring will create only a single instance of each bean in a bean factory unless you change the default scope of singleton to some other bean scope.
Other bean scopes include:
Bean scopes
singleton (Default) Scopes a single bean definition to a
single object instance per Spring IoC
container.
prototype Scopes a single bean
definition to any number of object instances.
request Scopes a single bean definition to the
lifecycle of a single HTTP request; that is, each HTTP request has its
own instance of a bean created off the back of a single bean
definition. Only valid in the context of a web-aware Spring
ApplicationContext.
session Scopes a single
bean definition to the lifecycle of an HTTP Session. Only valid in the
context of a web-aware Spring ApplicationContext.
global session Scopes a single bean definition to the
lifecycle of a global HTTP Session. Typically only valid when used in a portlet
context. Only valid in the context of a web-aware Spring
ApplicationContext.

Spring - what's a bean and what's not?

I'm new to Spring and I'm confused about something basic. Are the classes that are stereotyped (Service, Controller, Repository) treated as beans? I'm confused as to when you actually need to annotate/configure something as a bean and when you don't. Is it for the classes that aren't stereotyped?
Thanks!
From spring documentation:
In Spring, the objects that form the backbone of your application and
that are managed by the Spring IoC container are called beans. A bean
is an object that is instantiated, assembled, and otherwise managed by
a Spring IoC container. Otherwise, a bean is simply one of many
objects in your application. Beans, and the dependencies among them,
are reflected in the configuration metadata used by a container.
Service, Controller, Repository are managed by the Spring IoC container, so they are called beans. You annotate a class as #Serivice, #Controller, #Repository, or more in general #Component when you want spring to manage it: spring will manage the instance of annotated class in regard of the scope you select (not all these scope are always available):
singleton – Return a single bean instance per Spring IoC container
prototype – Return a new bean instance each time when requested
request – Return a single bean instance per HTTP request
session – Return a single bean instance per HTTP session
globalSession – Return a single bean instance per global HTTP
session

How does #SessionScoped work with EJB? Is CDI only for web-tier?

How is the session defined in #SessionScoped CDI bean?
Is this annotation valid only when called from Servlet container, where the session is well defined in form of HttpSession?
If not, than how an EJB with #Inject #SessionScoped MyBean myBean can know what the session really is? I mean, methods of this EJB could have been invoked by a standalone client, RESTful WS or by some other view.
What should happen in such case? Should the annotation have no meaning, should it inject fresh MyBean instance for each request or maybe it should retain the same instance across all requests?
Taken from the #SessionScoped specification
The session scope is active:
during the service() method of any servlet in the web application,
during the doFilter() method of any servlet filter and when the
container calls any HttpSessionListener, AsyncListener or
ServletRequestListener.
So in short, yes. It is bound to the HttpSession. Also:
The session context is shared between all servlet requests that occur
in the same HTTP session. The session context is destroyed when the
HTTPSession times out, after all HttpSessionListeners have been
called, and at the very end of any request in which invalidate() was
called, after all filters and ServletRequestListeners have been
called.

Resources