What Is Contextloaderlistener Inward Leap Mvc - 10 Things Coffee Programmer Should Know

The ContextLoaderListner is ane of the of import components of the Spring MVC framework, belike the most of import later on DispatcherServlet itself. It is used to create root context too responsible for loading beans which are shared yesteryear multiple DispatcherServlet similar beans related to the service layer too information access layer. In general, When y'all prepare Spring MVC based spider web application too too using Spring inwards the services layer, y'all demand to supply ii application-contexts. The outset ane is configured using ContextLoaderListener too the other is configured using DispatcherServlet. The DispatcherServlet is responsible for loading spider web factor specific beans similar controllers, view resolvers too handler mappings spell equally I said before, ContextLoaderListener is responsible for loading middle-tier too data-tier beans which forms the dorsum terminate of Spring applications.

The ContextLoaderListener is similar whatever other Servlet listener too it has to hold upwardly declared inwards the deployment descriptor to hear to events. It listens for startup too shutdown of the server yesteryear implementing ServletContextListener too accordingly creates too destroy Spring-managed beans.

Though, web.xml is only ane means to configure ContextLoaderListener inwards Spring MVC application. From Spring 3.1 too Servlet 3.0, y'all tin give the sack too configure ContextLoaderListener without deployment descriptor too only using Java Configurations.

Spring 3.2 provides an abstract shape called AbstractAnnotationConfigDispatcherServletInitializer which receive got methods to render @Configuration classes for both root context too the spider web context specific to the DispatcherServlet.

The getRootConfigClasses() method of this shape render all @Configuration shape which should hold upwardly purpose of root context too @getServletConfigClasses() method returns all @Configuration classes which should hold upwardly specific to the DispatcherServlet. Though, if y'all are non familiar amongst Java Configuration, Spring Framework 5: Beginner to Guru course on Udemy is a proficient resources to start with.

Though, ane matter y'all must retrieve that this facility is solely available from Servlet 3.0 too y'all demand a Servlet 3.0 electrical charge spider web server similar Tomcat seven or higher to programmatically create root context. If y'all are running inwards Tomcat half-dozen or lower version thus y'all receive got to utilisation the web.xml to declare ContextLoaderListener.

One to a greater extent than matter to overstep along inwards hear is that ContextLoaderListener is optional. You tin give the sack even thus create Spring MVC based spider web application without using this listener because all web-specific beans are loaded by DispatcherServlet itself.





10 points nearly ContextLoaderListener inwards Spring MVC

Here are to a greater extent than or less of the of import things nearly org.springframework.web.context.ContextLoaderListener shape a Java too Spring developer should know. It contains to a greater extent than or less of the primal details equally good equally to a greater extent than or less lesser-known features of ContextLoaderListener, which is useful for both Spring 5.0 Developer Certification too Java Interviews which requires candidates from Spring background.

1) The ContextLoaderListener is similar whatever Servlet listener too implements both EventListener too ServletContextListener. Hence it listens for server startup too shutdown events.

2) Its labor is to receive got the Spring configuration files equally input too creates the jump managed beans equally per configuration too arrive create during server startup too destroys them during server shutdown. Though it didn't produce that itself, instead only delegate to ContextLoader equally good equally to ContextCleanupListener.

If y'all are non familiar amongst Spring IOC container, It's amend to bring together a comprehensive Spring Framework course of report like Spring Master Class - Beginner to Expert yesteryear Ranga Karnan, a swain blogger too experienced Java developer amongst Spring.

 is ane of the of import components of the Spring MVC framework What is ContextLoaderListener inwards Spring MVC - 10 Things Java Programmer Should Know


3) You tin give the sack declare ContextLoaderListener inwards deployment descriptor or web.xml equally follows:

<web-app>   <listener>       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>   <context-param>     <param-name>contextConfigLocation</param-name>     <param-value>           /WEB-INF/config/applicationContext-service.xml          /WEB-INF/config/applicationContext-dao.xml      </param-value> </context-param> <servlet>         <servlet-name>dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>          <param-name>contextConfigLocation</param-name>          <param-value>             /WEB-INF/config/servlet-context.xml            ***/WEB-INF/config/applicationContext.xml***          </param-value> </init-param>     <load-on-startup>1</load-on-startup>  </servlet>   </web-app>

4) The actual initialization run for the root application context is performed yesteryear ContextLoader shape which is called yesteryear ContextLoaderListener. This shape looks for a "contextClass" parameter at the web.xml context-param degree to notice the type of the context shape too if no contextClass is defined than yesteryear default it uses XmlWebApplicationContext.

It too processes a "contextConfigLocation" context-param too passes its value to the context instance. This parameter specifies the path for the jump configuration file. It may comprise multiple file paths which tin give the sack hold upwardly separated yesteryear whatever release of commas too spaces, similar "WEB-INF/applicationContext1.xml, WEB-INF/applicationContext2.xml".

Even, Ant-style path patterns are supported equally well, similar "WEB-INF/*Context.xml,WEB-INF/spring*.xml" or "WEB-INF/**/*Context.xml". If non explicitly specified, the context implementation is supposed to utilisation a default place (with XmlWebApplicationContext: "/WEB-INF/applicationContext.xml").


5) Along amongst loading the root application context, this shape tin give the sack optionally charge or obtain too claw upwardly a shared bring upwardly context to the root application context. You tin give the sack encounter the loadParentContext(ServletContext) method for to a greater extent than information. You tin give the sack too read Spring inwards Action fifth Edition for to a greater extent than details, ane of my favorite books to larn Spring too every Java developing using Spring should read it.

 is ane of the of import components of the Spring MVC framework What is ContextLoaderListener inwards Spring MVC - 10 Things Java Programmer Should Know


6) When ContextLoaderListener is used along amongst DispatcherServlet, a root web-application-context is created outset equally said before too a child-context is too created yesteryear dispatcher servlet which is attached to the root application-context. Refer documentation here.

7) In Servlet 3.0 compliant container, y'all tin give the sack create root context without defining ContextLoaderListener inwards the deployment descriptor file. From Servlet 3.0 specification, announced inwards 2009, at that topographic point is a ServletContainerInitializer interface, whatever shape which implements this interface is automatically discovered yesteryear Servlet 3.0 compliant servlet container similar Tomcat seven too higher.

8) Spring provides an implementation of ServletContainerInitializer interface equally SpringServletContainerInitializer too from Spring 3.2 y'all tin give the sack too extend from AbstractAnnotationConfigDispatcherServletInitializer class. This tin give the sack create both root context too web-context specific to the DispatcherServlet.

The getServletCofnigClasses() method of this shape render all @Configuration classes which tin give the sack hold upwardly loaded into spider web context too getRootConfigClasses() method returns all @Configuration classes which tin give the sack hold upwardly purpose of root context.

9) In Spring MVC, the DispatcherServlet is expected to charge the edible bean configuration, spider web components such equally controllers, sentiment resolve too handler mappings spell ContextLoaderListener is expected to charge beans from service-tier too data-tier which forms the dorsum terminate of the application.

10) Another produce goodness of the ContextLoaderListener is that it creates a WebApplicationContext too WebApplicationContext provides access to the ServletContext via ServletContextAware beans too the getServletContext() method. It too ties the lifecycle of the ApplicationContext to the lifecycle of the ServletContext equally explained inwards the classic Data Access object.

You tin give the sack configure ContextLoaderListener inwards the deployment descriptor too y'all tin give the sack fifty-fifty supply it the config place for the jump configuration file. The ContextLoader listener listens for server startup too shutdown too accordingly create too destroyed Spring beans.

From Servlet 3.0 onwards, y'all tin give the sack fifty-fifty configure ContextLoaderListener too root context without using deployment descriptor or web.xml file. The Servlet 3.0 specification provides an interface called ServletContainerInitializer too whatever shape which implements this interface volition automatically hold upwardly scanned too loaded yesteryear whatever Servlet 3.0 compliant container similar Tomcat seven or Tomcat 8.

From Spring 3.2 version Spring provides a convenient shape called AbstractAnnotationConfigDispatcherServlet whose getRootConfig() method tin give the sack hold upwardly used to instantiate edible bean which lives inwards the root context.

The final matter y'all should retrieve nearly ContextLoaderListener is that it's optional inwards Spring MVC. You tin give the sack even thus create Spring MVC based spider web application without declaring ConextLoaderListener too only declaring DispatcherServlet because all web-specific components similar controllers, sentiment resolvers too handler mappings are defined inwards the WebApplicationContext created yesteryear Dispatcher servlet.

Further Learning
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
courses)
  • Difference betwixt @Compoent, @Service, too @Controller inwards Spring? (answer)
  • 5 Free Spring Framework too Spring Boot Courses (courses)
  • Difference betwixt @Autowired too @Inject inwards Spring? (answer)
  • 15 Spring Boot Interview Questions amongst Answers (questions)
  • Difference betwixt @RequestParam too @PathVariable inwards Spring (answer)
  • 5 Spring Framework Books For Java Developers (books)
  • 20 REST amongst Spring Interview Questions for Web developers (questions)
  • 5 Spring Boot Features Java developer should larn (features)
  • 3 ways to larn Core Spring or Spring MVC amend (article)
  • 6 Resources to larn Spring Framework inwards Depth (resources)
  • Top 10 Spring Framework Interview Questions (questions)
  • 10 Spring MVC Annotations Every Java Dev should know (annotations)
  • Thanks for reading this article thus far. If y'all notice these Spring MVC annotations useful thus delight part amongst your friends too colleagues on Facebook too Twitter. If y'all receive got whatever questions or feedback thus delight drib a note.

    P. S. - If y'all are novel to Spring Framework too looking for a hands-on course of report then Learn Spring: The Certification Class yesteryear Eugen Paraschive of Bealdug is belike the best course of report to larn Spring v too Spring Boot 2 from scratch, inwards a guided, code-focused way.

    0 Response to "What Is Contextloaderlistener Inward Leap Mvc - 10 Things Coffee Programmer Should Know"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel