A Developer's Diary

Dec 13, 2012

Setting up Spring Development Environment using Maven

The step by step guide for creating a java project using Maven and configuring it for Spring Framework

#1. Setup a simple Java project using Maven
Use mvn archetype:generate command for creating a sample java project. When prompted hit enter to select the version as 1.0-SNAPSHOT and package as com.examples.spring

mvn archetype:generate -DgroupId=com.examples.spring -DartifactId=HelloSpring -DarchetypeArtifactId=maven-archetype-quickstart

After the command has executed successfully, you will find HelloSpring folder created with the following file structure
C:\Workspace>tree /F HelloSpring
Folder PATH listing
Volume serial number is 48F5-8605

C:\WORKSPACE\HELLOSPRING
│   pom.xml
│
└───src
    ├───main
    │   └───java
    │       └───com
    │           └───examples
    │               └───spring
    │                       App.java
    │
    └───test
        └───java
            └───com
                └───examples
                    └───spring
                            AppTest.java

#2. Configure pom.xml file
Open the pom.xml located under HelloSpring folder. The file looks like as below
 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4   <groupId>com.examples.spring</groupId>
 5   <artifactId>HelloSpring</artifactId>
 6   <packaging>jar</packaging>
 7   <version>1.0-SNAPSHOT</version>
 8   <name>HelloSpring</name>
 9   <url>http://maven.apache.org</url>
10   <dependencies>
11     <dependency>
12       <groupId>junit</groupId>
13       <artifactId>junit</artifactId>
14       <version>3.8.1</version>
15       <scope>test</scope>
16     </dependency>
17   </dependencies>
18 </project>

Make the following modifications to the above file
1. Add the property org.springframework.version under properties tag
 10     <properties>
 11         <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
 12     </properties>

2. Add the spring framework dependent jar files in the pom.xml under the dependencies tag
 22         <!-- Core utilities used by other modules. Define this if you use Spring 
 23             Utility APIs (org.springframework.core.*/org.springframework.util.*) -->
 24         <dependency>
 25             <groupId>org.springframework</groupId>
 26             <artifactId>spring-core</artifactId>
 27             <version>${org.springframework.version}</version>
 28         </dependency>
 29 
 30         <!-- Expression Language (depends on spring-core) Define this if you use 
 31             Spring Expression APIs (org.springframework.expression.*) -->
 32         <dependency>
 33             <groupId>org.springframework</groupId>
 34             <artifactId>spring-expression</artifactId>
 35             <version>${org.springframework.version}</version>
 36         </dependency>
 37 
 38         <!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define 
 39             this if you use Spring Bean APIs (org.springframework.beans.*) -->
 40         <dependency>
 41             <groupId>org.springframework</groupId>
 42             <artifactId>spring-beans</artifactId>
 43             <version>${org.springframework.version}</version>
 44         </dependency>
 45 
 46         <!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core, 
 47             spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) -->
 48         <dependency>
 49             <groupId>org.springframework</groupId>
 50             <artifactId>spring-aop</artifactId>
 51             <version>${org.springframework.version}</version>
 52         </dependency>
 53 
 54         <!-- Application Context (depends on spring-core, spring-expression, spring-aop, 
 55             spring-beans) This is the central artifact for Spring's Dependency Injection 
 56             Container and is generally always defined -->
 57         <dependency>
 58             <groupId>org.springframework</groupId>
 59             <artifactId>spring-context</artifactId>
 60             <version>${org.springframework.version}</version>
 61         </dependency>
 62 
 63         <!-- Various Application Context utilities, including EhCache, JavaMail, 
 64             Quartz, and Freemarker integration Define this if you need any of these integrations -->
 65         <dependency>
 66             <groupId>org.springframework</groupId>
 67             <artifactId>spring-context-support</artifactId>
 68             <version>${org.springframework.version}</version>
 69         </dependency>
 70 
 71         <!-- Transaction Management Abstraction (depends on spring-core, spring-beans, 
 72             spring-aop, spring-context) Define this if you use Spring Transactions or 
 73             DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) -->
 74         <dependency>
 75             <groupId>org.springframework</groupId>
 76             <artifactId>spring-tx</artifactId>
 77             <version>${org.springframework.version}</version>
 78         </dependency>
 79 
 80         <!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, 
 81             spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->
 82         <dependency>
 83             <groupId>org.springframework</groupId>
 84             <artifactId>spring-jdbc</artifactId>
 85             <version>${org.springframework.version}</version>
 86         </dependency>
 87 
 88         <!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, 
 89             and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx) 
 90             Define this if you need ORM (org.springframework.orm.*) -->
 91         <dependency>
 92             <groupId>org.springframework</groupId>
 93             <artifactId>spring-orm</artifactId>
 94             <version>${org.springframework.version}</version>
 95         </dependency>
 96 
 97         <!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, 
 98             JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans, 
 99             spring-context) Define this if you need OXM (org.springframework.oxm.*) -->
100         <dependency>
101             <groupId>org.springframework</groupId>
102             <artifactId>spring-oxm</artifactId>
103             <version>${org.springframework.version}</version>
104         </dependency>
105 
106         <!-- Web application development utilities applicable to both Servlet and 
107             Portlet Environments (depends on spring-core, spring-beans, spring-context) 
108             Define this if you use Spring MVC, or wish to use Struts, JSF, or another 
109             web framework with Spring (org.springframework.web.*) -->
110         <dependency>
111             <groupId>org.springframework</groupId>
112             <artifactId>spring-web</artifactId>
113             <version>${org.springframework.version}</version>
114         </dependency>
115 
116         <!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans, 
117             spring-context, spring-web) Define this if you use Spring MVC with a Servlet 
118             Container such as Apache Tomcat (org.springframework.web.servlet.*) -->
119         <dependency>
120             <groupId>org.springframework</groupId>
121             <artifactId>spring-webmvc</artifactId>
122             <version>${org.springframework.version}</version>
123         </dependency>
124 
125         <!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans, 
126             spring-context, spring-web) Define this if you use Spring MVC with a Portlet 
127             Container (org.springframework.web.portlet.*) -->
128         <dependency>
129             <groupId>org.springframework</groupId>
130             <artifactId>spring-webmvc-portlet</artifactId>
131             <version>${org.springframework.version}</version>
132         </dependency>
133 
134         <!-- Support for testing Spring applications with tools such as JUnit and 
135             TestNG This artifact is generally always defined with a 'test' scope for 
136             the integration testing framework and unit testing stubs -->
137         <dependency>
138             <groupId>org.springframework</groupId>
139             <artifactId>spring-test</artifactId>
140             <version>${org.springframework.version}</version>
141             <scope>test</scope>
142         </dependency>

On executing the command mvn clean package maven will start downloading the spring framework jar files. After all the jar files have been downloaded you will see BUILD SUCCESSFUL message.
Congratulations!! your spring framework development environment is ready

#3. Create Eclipse Project
Run the command mvn eclipse:eclipse in the terminal. This command will create .classpath and .project in the HelloSpring directory. Your project is now ready for import in eclipse. For importing, follow the steps mentioned in the earlier post

#4. Sample Spring Application
Create a new java bean class called Greeting.java

1 package com.examples.spring;
2 
3 public class Greeting {
4 
5     public String message() {
6         return "Hello";
7     }
8 }

Declare the beans in an XML configuration file called beans.xml which will be used by the Spring Framework for instantiation. Make sure beans.xml is present in the build path.

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5 
6     <bean id="greeting" class="com.examples.spring.Greeting" />
7 </beans>

Modify the App.java file created by maven to initialize the springs ApplicationContext. The application context instantiates the bean instance and makes available for use in the code

 1 package com.examples.spring;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class App {
 7     public static void main(String[] args){
 8         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
 9         Greeting greeting = (Greeting) context.getBean("greeting");
10         System.out.println(greeting.message());
11     }
12 }

On running the application, you will see the output as below
C:\Workspace\HelloSpring
Dec 13, 2012 8:45:23 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@459efb: startup date [Thu Dec 13 08:45:23 IST 2012]; root of context hierarchy
Dec 13, 2012 8:45:23 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Dec 13, 2012 8:45:24 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c701cb: defining beans [greeting]; root of factory hierarchy
Hello

No comments :

Post a Comment