Web Configuration
Public classSpitterwebinitializerextendsAbstractannotationconfigdispatcherservletinitializer {@OverrideprotectedClass<?>[] getrootconfigclasses () {return NewClass<?>[] {rootconfig.class }; } @OverrideprotectedClass<?>[] getservletconfigclasses () {return NewClass<?>[] {webconfig.class }; } @Overrideprotectedstring[] Getservletmappings () {return NewString[] {"/" }; }}
View parser, etc.
@Configuration @enablewebmvc@componentscan ("Spittr.web") Public classWebconfigextendsWebmvcconfigureradapter {@Bean Publicviewresolver Viewresolver () {internalresourceviewresolver resolver=NewInternalresourceviewresolver (); Resolver.setprefix ("/web-inf/views/"); Resolver.setsuffix (". JSP"); returnResolver; } @Override Public voidconfiguredefaultservlethandling (Defaultservlethandlerconfigurer configurer) {configurer.enable (); } @Override Public voidaddresourcehandlers (Resourcehandlerregistry registry) {//TODO auto-generated Method Stub Super. Addresourcehandlers (registry); }}
Controller
@Controller @requestmapping ("/") Public class HomeController { = GET) public String Home (model) { return "Home"; }}
Other Configuration Classes
@Configuration @import (dataconfig.class) @ComponentScan (basepackages= {"Spittr"}, Excludefilters ={@Filter (type= Filtertype.custom, value = WebPackage.class) }) Public classRootConfig { Public Static classWebPackageextendsRegexpatterntypefilter { PublicWebPackage () {Super(Pattern.compile ("Spittr\\.web")); } }}
@Configuration Public class dataconfig { @Bean public DataSource DataSource () { return New Embeddeddatabasebuilder (). SetType (EMBEDDEDDATABASETYPE.H2). Addscript ("Schema.sql"). Build (); } @Bean public jdbcoperations jdbctemplate (DataSource DataSource) { Returnnew JdbcTemplate (DataSource); }
Spring MVC fully Annotation-based reference example