This tutorial will help you to write your first Hello World Spring program. We will use Maven tool to generate the project and Eclipse IDE to implement a method which will print 'Hello World!' in the console.
Tools and Technologies used in this article
- Spring 3.1
- Maven
- JDK 1.6
- Eclipse 3.7
Note : Spring 3 requires at least JDK 5. So, make sure you have JDK 5 or above.
1. Create a Java Project using Maven Tool
In the command prompt execute the following command to generate Maven compatible Java project named as 'SpringCoreHelloWorld'.
mvn archetype:generate -DgroupId=com.srccodes.example.spring -DartifactId=SpringCoreHelloWorld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Generated Maven Java Project structure
2. Update pom.xml
Add dependency of Spring core and context.
File: 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>com.srccodes.example.spring</groupId>
<artifactId>SpringCoreHelloWorld</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringCoreHelloWorld</name>
<url>http://maven.apache.org</url>
<properties>
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3. Convert to Eclipse compatible Java project
Open the directory 'SpringCoreHelloWorld' in command prompt and run the following maven command.
mvn eclipse:eclipse
Screenshot of command prompt
On completion of the above command, Maven Java project will be converted to Eclipse compatible java project.
Eclipse compatible Java Project structure
4. Import project in Eclipse
Open Eclipse IDE and select from the menu File --> Import --> General --> Existing Projects into Workspace
Browse to the directory of the newly converted Eclipse compatible Java Project and click 'Finish' button.
Screenshot of Eclipse project structure
5. Write Spring bean
Create a class 'SpringCoreHelloWorld' under the package 'com.srccodes.example.spring' and copy the following content.
package com.srccodes.example.spring;
/**
* @author Abhijit Ghosh
* @version 1.0
*
*/
public class SpringCoreHelloWorld {
private String message = null;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void sayHello() {
System.out.println(getMessage());
}
}
We will use Spring framework to get the instance of this class from Spring runtime and call the 'sayHello()' method to print 'Hello World!' message in the console.
Note:
Though Interface based implementation is advisable in Spring Framework, we have not written any interface for our bean class 'SpringCoreHelloWorld'to keep it simple for our first Spring Hello World program.
6. Add Spring Application context Configuration file
Right click on 'main' and select from context menu 'New' --> 'Folder'.
Enter 'resources' in the 'Folder name' field and click the 'Finish' button.
Copy the 'springApplicationContext.xml' file in the 'resources' folder.
File: springApplicationContext.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-3.1.xsd">
<bean id="helloWorldBean" class="com.srccodes.example.spring.SpringCoreHelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
Note: We have used Spring's setter injection to pass our message 'Hello World!' to 'setMessage' method of 'SpringCoreHelloWorld'. This message is printed by the 'sayHello()' method.
7. Configure Java Build Path
Right click on 'SpringCoreHelloWorld' project and select from context menu 'Properties' --> 'Java Build Path'.
Add 'resources' folder as shown in the screenshot below
8. Code to interact with Spring's application context
Copy the following code to 'App' class of package 'com.srccodes.example.spring'.
package com.srccodes.example.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
// Get Spring application context
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springApplicationContext.xml");
// Get a bean instance
SpringCoreHelloWorld helloWorldBeanInst = applicationContext.getBean("helloWorldBean", SpringCoreHelloWorld.class);
// Execute SpringCoreHelloWorld bean's method
helloWorldBeanInst.sayHello();
}
}
9. Final project structure
After doing all the changes the overall project structure will look like this
10. Run Your Code
Right click on 'App.java' and select from context menu 'Run As' --> 'Java Application'.
11. Console Output
Console
Sep 2, 2012 9:01:13 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@145d068: startup date [Sun Sep 02 21:01:13 IST 2012]; root of context hierarchy
Sep 2, 2012 9:01:13 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [springApplicationContext.xml]
Sep 2, 2012 9:01:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@f11404: defining beans [helloWorldBean]; root of factory hierarchy
Hello World!
Download SrcCodes
All code samples shown in this post are available in the following link SpringCoreHelloWorld.zip
Comments