A Spring Hello World Example with only one Application Context.

So, no hierarchy! Bootstrap in Java, Configuration in Java!

This is my directory layout:

pom.xml and web.xml is exactly same as shown in here.

And now the remaining classes:
package biz.tugay.janeleven.root.service;
/* User: koray@tugay.biz Date: 12/01/16 Time: 19:26 */
 
public interface RandomNumberGenerator {
    int randomNumber();
}
 
package biz.tugay.janeleven.root.service;
/* User: koray@tugay.biz Date: 12/01/16 Time: 19:24 */
 
import org.springframework.stereotype.Service;
 
@Service
public class RandomNumberGeneratorImpl implements RandomNumberGenerator {
    public int randomNumber() {
        return 5;
    }
}
 
@Controller
@RequestMapping(value = "/")
public class HomeController {
 
    @Autowired
    private RandomNumberGenerator randomNumberGenerator;
 
    @RequestMapping(value = "/")
    public String handleRequest(Model model) {
        final int randomNumber = randomNumberGenerator.randomNumber();
        model.addAttribute("randomNumber", randomNumber);
        return "home.jsp";
    }
}
 
package biz.tugay.janeleven.root.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
 
@Configuration
@ComponentScan(basePackages = "biz.tugay.janeleven")
@EnableWebMvc
public class ApplicationContextConfig {
    @Bean
    public InternalResourceViewResolver getViewResolver() {
        final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        return viewResolver;
    }
}
 
package biz.tugay.janeleven.root.config;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
 
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
 
public class SpringWebAppInitializer implements WebApplicationInitializer {
    public void onStartup(ServletContext container) throws ServletException {
        final AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(ApplicationContextConfig.class);
        final DispatcherServlet dispatcherServlet = new DispatcherServlet(appContext);
        ServletRegistration.Dynamic dispatcher = container.addServlet("SpringDispatcher", dispatcherServlet);
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

and finally home.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
</head>
<body>
<div align="center">
    <h1>Hello World!</h1>
    <h3>How are you?</h3>
    <c:out value="${requestScope.randomNumber}"/>
</div>
</body>
</html>

And the result: