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


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.junjun.spring.tutorials</groupId>
<artifactId>spring-mvc-bootstrap</artifactId>
<packaging>war</packaging>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<org.springframework.version>4.2.6.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>
</project>
view raw pom.xml hosted with ❤ by GitHub







3. ENABLE SPRING MVC WITH JAVA CONFIG


package org.junjun.spring.tutorials.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan({ "org.junjun.spring.tutorials.web" })
public class AppConfig {
}
view raw AppConfig.java hosted with ❤ by GitHub

@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
package org.junjun.spring.tutorials.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/")
@ResponseBody
public String hi() {
return "hello";
}
}







6. REPLACE WEB.XML

package org.junjun.spring.tutorials.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { AppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}


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