ContextLoaderListener in spring boot - spring-boot

I am new in spring boot
I am trying to configure ContextLoaderListener and regiter a Bean in the listener, so that I can use it in two different DispatcherServlet.
I have done this successfully in spring mvc with JavaConfig but In spring-boot I am unable to configure ContextLoaderListener
Please Help

add #ServletComponentScan to the main method

Related

How can I inject dependencies into a javax.faces.context.ExceptionHandler using Spring?

I have a JSF application using PrimeFaces 6.2 and Spring 5.1.4. I read, that an exception handler can be defined in the faces-config.xml like this:
<factory>
<exception-handler-factory>my.package.MyExceptionHandlerFactory</exception-handler-factory>
</factory>
I wondered how I could get the dependencies injected into an ExceptionHandlerFactory and ExceptionHandler?
You can't do this out of the box.
It would be possible by creating an "spring aware" ExceptionHandlerFactory and create the ExceptionHandler instance via Spring but i would just get the beans manually in your ExceptionHandler like: Best way to manually pull a spring bean?

Is there any bean created for spring #propertySource annotation

I have used spring #propertysource annotation to load properties file in my spring boot project . I just need to know is there any bean created for this annotation when application gets started
#PropertySource annotation is used to let spring-boot know the location of the properties required by your application. It will not create any bean.

Purpose of using #Configuration annotation

I have created a spring mvc based application but I didn't use this #Configuration annotation. What is the purpose of using #Configuration annotation? By using this, what are we communicating to springMVC container?
Assuming your application is using xml configuration rather than AnnotationConfig so it is not loaded to ApplicationContext at all.
#Configuration is used when ApplicationContext has been initialized and bean registration.
#Configuration annotation is a core Spring annotation, and not Spring MVC. It is a core entry point to configuring Spring-based application using Java config instead of XML config.
Please, use Spring Documentation more often because it is a place where you will find answers to most of your questions. Like this one:
Indicates that a class declares one or more Bean #Bean methods and may
be processed by the Spring container to generate bean definitions and
service requests for those beans at runtime

Servlet/Filters/Listeners in Spring Boot Application

I've added Servlet/Filters/Listeners in Spring Boot application using ServletRegistrationBean , FilterRegistrationBean etc.. for that we have to declare servlets, filters as spring bean ..which will get added in Spring Application Context..which is working absolutely fine
My Question is ..whenever i will call my application, will the request FIRST be handled by Dispatcher Servlet and then to Filter?
Before spring boot, we use to register it directly in web.xml and then i think Filters used to handler the request first, then dispatcher servlet and so on and so forth..
Has the flow changed in Spring Boot Application?

what is the difference between xml files Spring and Spring MVC framework

I start to learn spring recently.
My goal is to use spring MVC to do restful api
I know spring MVC is web framework in spring
I know that in spring,there is beans.xml
And in spring MVC , there is servletname-servlet.xml
I want to know where is difference??
Is it means if I use spring MVC,I don't need to use beans.xml??
Please give me some way or give me example project link with spring and spring MVC together
The servletname-servlet.xml defines the beans for one servlet's app context. There can be number of servlets in a webapp and for every servlet we have servletname-servlet.xml (e.g. spring1-servlet.xml for servlet1, spring2-servlet.xml for servlet2).
Beans defined in servletname-servlet.xml can reference beans in beans.xml, but not vice versa.
All Spring MVC controllers must go in the servletname-servlet.xml context.
Beans.xml contain beans that are shared between all servlets in a webapp.Usually the beans.xml context is not necessary if you have only one servlet in your webapp.
You could define all your beans in servletname-servlet.xml but it's not a good practice.
Usually if you create a web application in 'pure' spring (ie. without spring MVC) then you will add ContextLoaderListener as a filter to your web.xml. Then spring will look for applicationContext.xml when you will usually import beans.xml.
In servletname-servlet.xml you define servlets. Servlets can refer other beans. So it's good practice to separate front (servlets) from backend (beans.xml).
Also remember that beans declared in servletname-servlet.xml are overriding the definitions of any beans defined with the same name in the global scope.
See also better answer at: ContextLoaderListener or not?

Resources