0 configuration file Construction Springmvc practice record

Source: Internet
Author: User

This record uses pure Java code to build the practice of SPRINGMVC Engineering, just a demo. Before you start, warm up and give the SPRINGMVC call flowchart, which explains the process of an HTTP request request reaching the SPRINGMVC framework, as follows:

Starting with Servlet 3.0, the class implementing the Javax.servlet.ServletContainerInitializer interface executes the Onstartup method when the container is started. SPRINGMVC's "0 configuration" is based on this feature. So, for a container under Servlet 3.0, it's still an honest configuration in Web. Xml. Let's build the SPRINGMVC step by step, first:

The initialization process for starting a critical class is shown, first the container starts, initializes and executes the spring class that implements the Servletcontainerinitalzer interface, The class initializes the Playwebappinitializer class that implements the Webapplicationinitializer interface (since the Abstractannotationconfigdispatcherservletinitializer abstract class is inherited here) 。

In the tutorial Playwebappinitializer initializes two bean containers, a bean that is directly related to the MVC framework associated with Dispatcherservlet, such as controller,service,repository, and so on. Another kind of bean container before recalling, remember Contextloaderlistener, do SSH framework integration in Web. XML to configure this class, so that this container is more general configuration of beans. So the question is: why should there be two coexistence? A: I guess it is convenient to separate, if you do not want to use SPRINGMVC and other MVC framework can be dispatcherservlet related to the bean container removed. (Pure fantasy, welcome to shoot Bricks).

Here Webconfig is responsible for the configuration of dispacherservlet related beans, rootconfig is responsible for Contextloaderlistener related bean configuration.

Here's an excerpt from Spring in Action 4th about two ApplicationContext, on page 135:

A TALE of application contexts

When Dispatcherservlet starts up, it creates a Spring application context and starts loading it with beans declared in the Configuration files or classes that it ' s given. With the Getservletconfigclasses () method in Listing 5.1, you ' ve asked that Dispatcher-servlet load its application conte XT with beans defined in the Webconfig configura-tion class (using Java configuration). But in Spring Web applications, there ' s often another application context. This and application context is created by Contextloaderlistener. Whereas Dispatcherservlet is expected to load beans containing Web Components such as controllers, view resolvers, and Han Dler mappings, Contextloaderlistener is expected to load the other beans in your application. These beans is typically the middle-tier and data-tier components of this drive, the back end of the application. 136 CHAPTER 5 Building Spring Web applications under the covers, Abstractannotationconfigdispatcherservletinitializer CRE -Ates both a dispatcherserVlet and a contextloaderlistener. The @Configuration classes returned from Getservletconfigclasses () would define beans for Dispatcher-servlet ' s application Context. Meanwhile, the @Configuration class ' s returned Get-rootconfigclasses () would be a used to configure the application context Created by Contextloaderlistener. In this case, your root configuration was defined in RootConfig, whereas Dispatcher-servlet ' s configuration was declared in Webconfig. You'll see what those the Configura-tion classes look like in a moment.

On the Code of Practice: Com.bob.playspring.PlayWebAppInitializer

 Packagecom.bob.playspring;ImportOrg.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;/*** Initialize Dispatherservlet, instead of config in Web. xml to Dispatherservlet, *@authorBob **/ Public classPlaywebappinitializerextendsAbstractannotationconfigdispatcherservletinitializer {@OverrideprotectedClass<?>[] getrootconfigclasses () {return NewClass[] {rootconfig.class }; } @OverrideprotectedClass<?>[] getservletconfigclasses () {return NewClass[] {webconfig.class }; }    /*** Identifies one or more paths that dispatcherservlet'll be a mapped to.<br> * in this case, it's Mapp Ed to/, indicating that it'll be the application ' s default servlet.<br> * It'll handle all requests comin     G into the application. */@Overrideprotectedstring[] Getservletmappings () {return NewString[] {"/" }; }}

Com.bob.playspring.WebConfig

 Packagecom.bob.playspring;ImportOrg.springframework.context.annotation.Bean;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.web.servlet.ViewResolver;ImportOrg.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;ImportORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;ImportOrg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;ImportOrg.springframework.web.servlet.view.InternalResourceViewResolver;/*** System Configuration *@authorBob **/@EnableWebMvc @configuration@componentscan ("Com.bob") Public classWebconfigextendsWebmvcconfigureradapter {@Bean Publicviewresolver Viewresolver () {internalresourceviewresolver resolver=NewInternalresourceviewresolver (); Resolver.setprefix ("/web-inf/views/"); Resolver.setsuffix (". JSP"); Resolver.setexposecontextbeansasattributes (true); returnResolver; } @Override Public voidconfiguredefaultservlethandling (Defaultservlethandlerconfigurer configurer) {configurer.enable (); }}

Com.bob.playspring.RootConfig

 Packagecom.bob.playspring;ImportOrg.springframework.context.annotation.ComponentScan;ImportOrg.springframework.context.annotation.ComponentScan.Filter;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.context.annotation.FilterType;ImportORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC, @Configuration @componentscan (basepackages = {"Com.bob"}, Excludefilters ={@Filter (type= filtertype.annotation, value = Enablewebmvc.class) }) Public classRootConfig {}

I will not post the code about controller,service,repository.

0 configuration file build Springmvc practice record

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.