Spring part 1: Getting Started with Gradle

Table of Contents

  1. Overview
  2. Project Setup
    • Gradle Config : build.gradle
    • Configuration : web.xml
    • Context.xml
    • JSP
    • Controller

1. Overview
Spring is prominent framework developed by Rod Johnson in 2003. It is modular, light weight, open source framework and inversion of control container for the Java platform. Spring is based on POJO model, loose coupling and is non-invasive.

2. Project Setup
We’ll start by creating simple spring web application with gradle (project automation tool built upon concepts of Apache Ant and Apache Maven and is Groovy based DSL) in Eclipse IDE.

File > New > Dynamic Web Project

Provide a suitable project name and proceed, check generate web.xml and finish.

2.a. Gradle Config : build.gradle
Gradle is project automation tool built upon concepts of Apache Ant and Apache Maven and is Groovy based DSL(Domain Specific Language) instead of the more traditional XML form of declaring the project configuration. Unlike Maven, Gradle uses a directed acyclic graph (“DAG”) to determine the order in which tasks can be run.

apply plugin : 'eclipse'
apply plugin : 'java'
apply plugin : 'war'</span>

ext {
springVersion = '4.0.3.RELEASE'
logbackClassickVersion= '1.1.1'

repositories {

dependencies {
compile ("commons-codec:commons-codec:1.9")
compile ("commons-logging:commons-logging:1.1.1")
// Spring framework jars
compile ("org.springframework:spring-web:${springVersion}")
compile ("org.springframework:spring-webmvc:${springVersion}")
compile ("org.springframework:spring-context:${springVersion}")
compile ("org.springframework:spring-expression:${springVersion}")
compile ("org.springframework:spring-beans:${springVersion}")
compile ("org.springframework:spring-core:${springVersion}")
compile ("org.springframework:spring-aop:${springVersion}")
compile ("jstl:jstl:1.2")

task wrapper(type: Wrapper){
gradleVersion = '1.11'

The ‘plugins’ provide focused functionality to the Gradle.
Java Plugin – compiling, testing and publishing Java based software
Eclipse Plugin – integrating with the Eclipse IDE for development
War Plugin – project war generation

2.b. Configuration : web.xml
A deployment descriptor describes how a component, module or application (such as a web application or enterprise application) should be deployed. It directs a deployment tool to deploy a module or application with specific container options, security settings and describes specific configuration requirements. source



The DispatcherServlet creates its own WebApplicationContext and is a central servlet that manages handlers/controllers/view-resolvers and offers functionality facilitating the development of web applications.
Basically, ContextLoaderListener creates a root web-application-context for the web-application and puts it in the ServletContext. It can be used to load and unload the spring-managed beans irrespective of what technology is being used in the controller layer.

2.c. Configuration: context.xml

&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
&lt;import resource="controllers.xml" /&gt;


&lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;
&lt;beans:property name="prefix" value="/WEB-INF/jsp/" /&gt;
&lt;beans:property name="suffix" value=".jsp" /&gt;

“InternalResourceViewResolver” resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/jsp directory.

2.d. Pages
Create all jsp file under WEB-INF/jsp directory.

&lt;a href='&lt;c:out value="/nextpage"/&gt;'&gt;Next Page&lt;/a&gt;


&lt;h3&gt;Welcome to Spring Framework.&lt;/h3&gt;

2.e. Controllers.xml
A context file for defining application controllers.

&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
&lt;context:component-scan base-package="com.pj.ci.server" /&gt;
&lt;mvc:annotation-driven /&gt;

“component-scan” scan across the package to find and register the beans in application context.
“annotation-driven” declares support for spring MVC features like @RequestMapping, @Controller as well as declrative validation with @Valid and message body conversion with @RequestBody & @ResponseBody
MainController class

public class MainController {</span>

@RequestMapping(value = "/", method = RequestMethod.GET)
public String getMainPage(ModelMap modelMap) {
return "index";

@RequestMapping(value = "/nextpage", method = RequestMethod.GET)
public String getNextPage(ModelMap modelMap) {
return "nextpage";

This controller redirects request for ‘/’ and ‘/nextpage’ to ‘index’ & ‘nextpage’ view mapped in WEB-INF/jsp directory. The application default page is ‘index.jsp’

Project compile & running
In the terminal or console, browse the project directory and execute below commands. To use ‘gradle’ command in console we’ll need to set GRADLE_HOME in system.

gradle eclipse

This command will locallize the classpath of project for Eclipse IDE.

gradle build

This command will compile and generate necessary artifacts for project.
* “gradle wrapper” command can also be executed to generate wrapper file for the project. For this, we’ll need to add ‘task wrapper’ in build.gradle. With wrapper file, we’ll be able to use “gradlew eclipse” or “gradlew build”.

Reference :
1. Spring docs : 4.0.3 Release
2. Gradle user guide

Leave a comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.