☛ Setup Your Spring MVC Using fully Java Based Configuration example (fully java source code)

pom.xml

if you configure your spring mvc project using java soucre code frist setup pox.xml file

<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>com</groupId>
  <artifactId>spring-mvc-fully-java-source-code</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
  <name>spring-mvc-fully-java-source-code Maven Webapp</name>
  <properties>
	<springframework.version>5.2.2.RELEASE</springframework.version>
	<maven.compiler.target>1.8</maven.compiler.target>
	<maven.compiler.source>1.8</maven.compiler.source>
  </properties>
  <dependencies>
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-webmvc</artifactId>
		    <version></version>
		</dependency>
		<dependency>
		    <groupId>javax.servlet</groupId>
		    <artifactId>javax.servlet-api</artifactId>
		    <version>4.0.1</version>
		</dependency>
  </dependencies>
  <build>
    <finalName>spring-mvc-fully-java-source-code</finalName>
    <plugins>
		<plugin>
		<!-- Add Maven coordinates (GAV) for: maven-war-plugin -->
		    <groupId>org.apache.maven.plugins</groupId>
		    <artifactId>maven-war-plugin</artifactId>
		    <version>3.2.0</version>					
		</plugin>						
	</plugins>
  </build>
</project>

Save As SpringMvcDispatcherServletInitializer.java

web.xml file configration equel as SpringMvcDispatcherServletInitializer class file. Here you can see web.xml file configration as SpringMvcDispatcherServletInitializer class and your class must be extends AbstractAnnotationConfigDispatcherServletInitializer pre-define class

package com.code4devops;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

	@Override
	protected Class[] getRootConfigClasses() {
		return null;
	}

	@Override
	protected Class[] getServletConfigClasses() {
		return new Class[]{ApplicationContextConfig.class};
	}

	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}

}

ApplicationContextConfig.java

spring-mvc-dispatcher.xml file configration equel as ApplicationContextConfig class file. Here you can see spring-mvc-dispatcher.xml file configration as ApplicationContextConfig class and

package com.code4devops;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.code4devops")
public class ApplicationContextConfig {
		// define a bean for ViewResolver
		@Bean
		public ViewResolver viewResolver() {
			InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
			viewResolver.setPrefix("/WEB-INF/view/");
			viewResolver.setSuffix(".jsp");
			return viewResolver;
		}
}

MainController.java

package com.code4devops;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "home")
public class MainController {
@RequestMapping( value = "/",method = RequestMethod.GET)	
public String getHome() {
	return "home";
}
}