Friday, November 1, 2019

What Is Contextloaderlistener Inwards Trammel Mvc - 10 Things Coffee Programmer Should Know

The ContextLoaderListner is i of the of import components of the Spring MVC framework, in all likelihood the most of import later DispatcherServlet itself. It is used to exercise root context too responsible for loading beans which are shared past times multiple DispatcherServlet similar beans related to the service layer too information access layer. In general, When yous prepare Spring MVC based spider web application too too using Spring inwards the services layer, yous demand to render 2 application-contexts. The outset i is configured using ContextLoaderListener too the other is configured using DispatcherServlet. The DispatcherServlet is responsible for loading spider web ingredient specific beans similar controllers, view resolvers too handler mappings piece 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 whatsoever other Servlet listener too it has to hold out declared inwards the deployment descriptor to hear to events. It listens for startup too shutdown of the server past times implementing ServletContextListener too accordingly creates too destroy Spring-managed beans.

Though, web.xml is only i agency to configure ContextLoaderListener inwards Spring MVC application. From Spring 3.1 too Servlet 3.0, yous tin 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 out move of root context too @getServletConfigClasses() method returns all @Configuration classes which should hold out specific to the DispatcherServlet. Though, if yous are non familiar alongside Java Configuration, Spring Framework 5: Beginner to Guru course on Udemy is a skillful resources to start with.

Though, i affair yous must recollect that this facility is exclusively available from Servlet 3.0 too yous demand a Servlet 3.0 electrical charge spider web server similar Tomcat seven or higher to programmatically exercise root context. If yous are running inwards Tomcat six or lower version too therefore yous receive got to occupation the web.xml to declare ContextLoaderListener.

One to a greater extent than affair to move on inwards heed is that ContextLoaderListener is optional. You tin withal exercise Spring MVC based spider web application without using this listener because all web-specific beans are loaded by DispatcherServlet itself.





10 points close ContextLoaderListener inwards Spring MVC

Here are closed to of the of import things close org.springframework.web.context.ContextLoaderListener shape a Java too Spring developer should know. It contains closed to of the key details equally good equally closed to 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 whatsoever Servlet listener too implements both EventListener too ServletContextListener. Hence it listens for server startup too shutdown events.

2) Its project is to receive got the Spring configuration files equally input too creates the boundary managed beans equally per configuration too acquire inwards prepare during server startup too destroys them during server shutdown. Though it didn't exercise that itself, instead only delegate to ContextLoader equally good equally to ContextCleanupListener.

If yous are non familiar alongside Spring IOC container, It's amend to bring together a comprehensive Spring Framework course of written report like Spring Master Class - Beginner to Expert past times Ranga Karnan, a boyfriend blogger too experienced Java developer alongside Spring.

 is i 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 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 piece of work for the root application context is performed past times ContextLoader shape which is called past times ContextLoaderListener. This shape looks for a "contextClass" parameter at the web.xml context-param marking to discovery the type of the context shape too if no contextClass is defined than past times 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 boundary configuration file. It may incorporate multiple file paths which tin hold out separated past times whatsoever let out 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 occupation a default place (with XmlWebApplicationContext: "/WEB-INF/applicationContext.xml").


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

 is i 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 alongside DispatcherServlet, a root web-application-context is created outset equally said before too a child-context is too created past times dispatcher servlet which is attached to the root application-context. Refer documentation here.

7) In Servlet 3.0 compliant container, yous tin exercise root context without defining ContextLoaderListener inwards the deployment descriptor file. From Servlet 3.0 specification, announced inwards 2009, at that spot is a ServletContainerInitializer interface, whatsoever shape which implements this interface is automatically discovered past times 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 yous tin too extend from AbstractAnnotationConfigDispatcherServletInitializer class. This tin exercise both root context too web-context specific to the DispatcherServlet.

The getServletCofnigClasses() method of this shape render all @Configuration classes which tin hold out loaded into spider web context too getRootConfigClasses() method returns all @Configuration classes which tin hold out move of root context.

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

10) Another exercise 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 configure ContextLoaderListener inwards the deployment descriptor too yous tin fifty-fifty render it the config place for the boundary configuration file. The ContextLoader listener listens for server startup too shutdown too accordingly exercise too destroyed Spring beans.

From Servlet 3.0 onwards, yous tin 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 whatsoever shape which implements this interface volition automatically hold out scanned too loaded past times whatsoever 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 hold out used to instantiate edible bean which lives inwards the root context.

The final affair yous should recollect close ContextLoaderListener is that it's optional inwards Spring MVC. You tin withal exercise Spring MVC based spider web application without declaring ConextLoaderListener too only declaring DispatcherServlet because all web-specific components similar controllers, stance resolvers too handler mappings are defined inwards the WebApplicationContext created past times 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 alongside Answers (questions)
  • Difference betwixt @RequestParam too @PathVariable inwards Spring (answer)
  • 5 Spring Framework Books For Java Developers (books)
  • 20 REST alongside 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 therefore far. If yous discovery these Spring MVC annotations useful too therefore delight part alongside your friends too colleagues on Facebook too Twitter. If yous receive got whatsoever questions or feedback too therefore delight drib a note.

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

    No comments:

    Post a Comment