Spring is fully based on Java and annotation configuration

Source: Internet
Author: User

Points:

    1. Configure the class that inherits Webapplicationinitializer as the startup class, which is equivalent to configuring the Web. xml file
    2. Use @configuration to annotate a class using @bean annotations in the same way as in a class, the table name returns a bean, corresponding to the XML configuration file for the configuration applicationcontext.xml such as spring

Configuring the Startup Class

Inherit Webapplicationinitializer and re-onstartup method, load configuration class, equivalent to configure Web. xml file

1  Public classWebappinitconfigImplementsWebapplicationinitializer {2 3 @Override4      Public voidOnstartup (ServletContext Container)throwsservletexception {5         //Create the ' root ' Spring application context6Annotationconfigwebapplicationcontext Rootcontext =NewAnnotationconfigwebapplicationcontext ();7Rootcontext.register (RootConfig.class);//This is a custom configuration class that affects the project global8 9         //Manage The lifecycle of the root application contextTenContainer.addlistener (NewContextloaderlistener (Rootcontext)); One  A         //Create The Dispatcher servlet ' s Spring application context -Annotationconfigwebapplicationcontext Dispatchercontext =NewAnnotationconfigwebapplicationcontext (); -Dispatchercontext.register (Webmvcconfig.class);//This is a custom configuration class that only affects the current module the  -         //Register and map the dispatcher servlet -Servletregistration.dynamic Dispatcher = Container.addservlet ("Dispatcher",NewDispatcherservlet (Dispatchercontext)); -Dispatcher.setloadonstartup (1); +Dispatcher.addmapping ("/"); -     } +}

Annotationconfigapplicationcontext: Used to load a configuration class using annotations into a spring container, that is, the beans defined in the configuration class are given to the spring management

You can also use the Webapplicationinitializer implementation class Abstractdispatcherservletinitializer 、 Abstractannotationconfigdispatcherservletinitializer for a more concise configuration, Official document address, As long as the Webapplicationinitializer is implemented, it will be automatically recognized and loaded by the servlet container.

Use the Abstractannotationconfigdispatcherservletinitializer configuration example below

1  Public classAppinitializerextendsAbstractannotationconfigdispatcherservletinitializer {2 3 @Override4     protectedClass<?>[] getrootconfigclasses () {5         return NewClass[]{rootconfig.class};6     }7 8 @Override9     protectedClass<?>[] getservletconfigclasses () {Ten         return NewClass[]{webmvcconfig.class};; One     } A  - @Override -     protectedstring[] Getservletmappings () { the         return Newstring[]{"/"}; -     } -  -}

Custom Configuration Classes

Configure the spring management bean, corresponding to the XML configuration file of spring, such as configuration Applicationcontext.xml, which is the class loaded with Webapplicationinitializer above

1 @Configuration2@EnableWebMvc//note-driven SPRINGMVC equivalent to <mvc:annotation-driven> in XML3@ComponentScan (basepackages = "Com.woncode")//Enable component Scanning4  Public classWebmvcconfigextendsWebmvcconfigureradapter {5     //configuring the processing of static resources requires that Dispatchservlet requests for static resources be forwarded to the default servlet in the servlet container6 @Override7      Public voidconfiguredefaultservlethandling (Defaultservlethandlerconfigurer configurer) {8 configurer.enable ();9     }Ten  One @Override A      Public voidaddresourcehandlers (Resourcehandlerregistry registry) { -Registry.addresourcehandler ("/statics/**"). Addresourcelocations ("/web-inf/classes/statics/")); -         Super. Addresourcehandlers (registry); the     } -  - @Bean -      Publicinternalresourceviewresolver Viewresolver () { +Internalresourceviewresolver resolver=Newinternalresourceviewresolver (); -Resolver.setprefix ("/web-inf/classes/templates/"); +Resolver.setsuffix (". html"); AResolver.setexposecontextbeansasattributes (true); at         returnResolver; -     } -  -}

@Import Note: To introduce additional configuration classes to the current configuration class, you can then use the beans in the other configuration classes in the current configuration class, which is useful in a sub-module project, because the sub-modules want to split the configuration, but sometimes you need to use some other already defined configuration classes. So you can use this to import

Spring is fully based on Java and annotation configuration

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.