Wednesday 5 July 2017

Spring MVC Java Config : Part 1 Bootstrapping a Web Application

It actually becomes confusing to me when  java config is introduced in Spring 4 as any class could be used to initiate a bean container.

Hope this and following posts would be helpful to us all.







1.Tools and Environment 

IDE : Spring Tool Suite 3.7.3
JDK : 1.8
Tomcat : 8.0.18
Spring : 4.2.6.RELEASE 





2. POM.XML









3. ENABLE SPRING MVC WITH JAVA CONFIG



@Configuration : indicates that a class declares one or more methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime, class with this annotation would be the source of bean definitions for the Spring IoC Container.

@EnableWebMvc : provides the configuration behind Spring MVC which would register default HandlerMapping, HandlerAdapter, HandlerExceptionResolverComposite, AntPathMatcher, RequestMappingHandlerAdapter, ExceptionHandlerExceptionResolver.

@ComponentScan : configures the packages that the IoC container should scan for bean initialization.








4. SPRING MVC DIAGRAM










5. CREATE A CONTROLLER







6. REPLACE WEB.XML



Servlet 3.0+ container will pick up this class and run it automatically, this class replaces the web.xml.







7. RUN ON TOMCAT











8. SOURCE CODE

https://github.com/junjun-dachi/spring-tutorials/tree/master/spring-mvc-bootstrap




No comments:

Post a Comment

Flag Counter