Web App with Spring Core only..

Like a boss!

So in this example I have used:
  • Maven for dependency management and building the application from the source.
  • Tomcat 7 as the Servlet Container and the Web Server
  • Spring Framework 4.2.5 for inversion of control in my 'core' layer.. 
I want to first start with a nice graphic, showing the architecture of the code. Please note that I am not quite sure if this is the correct way to visualise the architecture I have. Check the image below with a grain of salt??

What I want to explain here is that:
  • Tomcat creates a Servlet Context. This is Application - Wide. 
  • Servlet Context bootstraps the Spring Container
  • Servlet Context holds a reference to the Spring Container, so that Servlets can access the Spring Container via it.
  • Spring Container reads the configuration file beans.xml
  • Spring Container manages the dependencies of Beans.
Before moving into the code, I would like to give a link to this page, which actually helped me a lot understanding how on earth I would be able to create such an architecture.

This is my directory layout:

And here is the code I have:
pom.xml
<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>biz.tugay</groupId>
    <artifactId>mar1601</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
 
    <name>mar1601 Maven Webapp</name>
    <url>http://maven.apache.org</url>
 
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
 
        <!-- Spring Framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <!-- Spring Framework -->
    </dependencies>
 
    <build>
        <finalName>mar1601</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
 
    <!-- Tomcat 7.0 implements Servlet 3.0 and JSP 2.2 specifications -->
 
    <listener>
        <listener-class>
            biz.tugay.mar1601.config.SpringApplicationContextListener
        </listener-class>
    </listener>
 
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <scripting-invalid>true</scripting-invalid>
            <trim-directive-whitespaces>true</trim-directive-whitespaces>
            <default-content-type>text/html</default-content-type>
        </jsp-property-group>
    </jsp-config>
</web-app>

beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <bean class="biz.tugay.mar1601.core.DummyServiceImpl" id="dummyService">
        <property name="dummyServiceHelper" ref="dummyServiceHelper"/>
    </bean>
 
    <bean class="biz.tugay.mar1601.core.DummyServiceHelperImpl"
          id="dummyServiceHelper">
    </bean>
</beans>

And all the Java classes I have:
package biz.tugay.mar1601.config;
/* User: koray@tugay.biz Date: 05/03/16 Time: 22:40 */
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
 
public class SpringApplicationContextListener implements ServletContextListener {
 
    public void contextInitialized(ServletContextEvent sce) {
        final ApplicationContext applicationContext
                = new ClassPathXmlApplicationContext("beans.xml");
        final ServletContext servletContext = sce.getServletContext();
        servletContext.setAttribute("coreContext", applicationContext);
    }
 
    public void contextDestroyed(ServletContextEvent sce) {
        final ServletContext servletContext = sce.getServletContext();
        servletContext.removeAttribute("coreContext");
    }
}
 
package biz.tugay.mar1601.core;
/* User: koray@tugay.biz Date: 05/03/16 Time: 22:39 */
 
import java.util.List;
 
public interface DummyService {
 
    List<Integer> getAListOfDummyIntegers();
}
 
package biz.tugay.mar1601.core;
/* User: koray@tugay.biz Date: 05/03/16 Time: 22:51 */
 
import java.util.List;
 
public interface DummyServiceHelper {
 
    List<Integer> helperMethod();
}
 
 
package biz.tugay.mar1601.core;
/* User: koray@tugay.biz Date: 05/03/16 Time: 22:52 */
 
import java.util.Arrays;
import java.util.List;
 
public class DummyServiceHelperImpl implements DummyServiceHelper {
    @Override
    public List<Integer> helperMethod() {
        return Arrays.asList(1, 2, 3, 4, 5);
    }
}
 
package biz.tugay.mar1601.core;
/* User: koray@tugay.biz Date: 05/03/16 Time: 22:39 */
 
import java.util.List;
 
public class DummyServiceImpl implements DummyService {
 
    private DummyServiceHelperImpl dummyServiceHelper;
 
    @Override
    public List<Integer> getAListOfDummyIntegers() {
        return dummyServiceHelper.helperMethod();
    }
 
    public void setDummyServiceHelper(DummyServiceHelperImpl dummyServiceHelper) {
        this.dummyServiceHelper = dummyServiceHelper;
    }
}
 
package biz.tugay.mar1601;
/* User: koray@tugay.biz Date: 05/03/16 Time: 22:05 */
 
import biz.tugay.mar1601.core.DummyService;
import org.springframework.context.ApplicationContext;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
 
@WebServlet(urlPatterns = "/hello")
public class HelloWorldServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        final ApplicationContext coreContext = (ApplicationContext) req.getServletContext().getAttribute("coreContext");
        final DummyService dummyService = coreContext.getBean("dummyService", DummyService.class);
        final List<Integer> integers = dummyService.getAListOfDummyIntegers();
        req.setAttribute("integers", integers);
        req.getRequestDispatcher("/hello.jsp").forward(req, resp);
    }
}

And finally, the jsp files.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:redirect url="/hello"/>
 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
<%--@elvariable id="integers" type="java.util.List<java.lang.Integer>"--%>
<c:forEach items="${requestScope.integers}" var="integer">
  ${integer}
  <br/>
</c:forEach>
</body>
</html>

The first 2 lines are in index.jsp and the rest is in hello.jsp.

Please note how the HelloWorldServlet gets the bean it requires from the Spring Container via the Servlet Context