Spring MVC Configuration Note-based on Javaconfig

Source: Internet
Author: User

Primarily using Javaconfig-based configuration

Configure Dispatcherservlet

AbstractAnnotationConfigDispatcherServletInitializer Dispatcherservlet and Spring application contexts are configured by inheriting abstract classes (no need to configure in XML)

 Public classSpittrwebappinitializerextendsAbstractannotationconfigdispatcherservletinitializer {? //Specify the Spring app context configuration class (the Bean for the primary Configuration Web Component)@OverrideprotectedClass<?>[] getservletconfigclasses () {return NewClass[]{webconfig.class}; }      //Another application context configuration class (other beans in the app) that corresponds to@OverrideprotectedClass<?>[] getrootconfigclasses () {return NewClass[]{rootconfig.class};    }? //map Dispatcherservlet to "/"@Overrideprotectedstring[] Getservletmappings () {return Newstring[]{"/"}; }}

Where webconfig primarily configures Web component-related beans, such as controllers, view resolvers, and processing mappers, the content is roughly as follows

@Configuration//flag This class is a configuration class@EnableWebMvc//Enable annotation-driven Spring MVC, with the <mvc:annotation-driven> of XML configuration Methods@ComponentScan ("Spitter.web")//Scan the Bean component in this package Public classWebconfigextendswebmvcconfigureradapter{? /*** Configure View resolver*/@Bean Publicviewresolver Viewresolver () {internalresourceviewresolver resolver=NewInternalresourceviewresolver (); Resolver.setprefix ("/web-inf/views/"); Resolver.setsuffix (". JSP"); Resolver.setexposecontextbeansasattributes (true); returnResolver;    }? /*** Configure the processing of static resources*/@Override Public voidconfiguredefaultservlethandling (Defaultservlethandlerconfigurer configurer) {configurer.enable (); }      //other web-related beans, etc. ..}

RootConfig Configure a Bean other than the Web component, the content is as follows

@Configuration // Configure the scanned package, as well as the filter @ComponentScan (basepackages = {"Spitter"}, Excludefilters = {    = Filtertype.annotation, value = Enablewebmvc. class )})publicclass  rootconfig {    //  Bean etc }
Add Controller, etc.

You can add the corresponding Controller, JSP files and so on in the corresponding location

To list a simple Controller

@Controller @requestmapping ("/")  Public class HomeController {    = requestmethod.get)    public  String Home () {          return "Home";    }}
File structure

The overall structure of the file is as follows:

Spring MVC Configuration Note-based on Javaconfig

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.