Tools and Technologies used in this article :
-
Spring 3.1
-
Maven
-
JDK 1.6
-
Eclipse 3.7
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'.
1
|
mvn archetype:generate -DgroupId=com.srccodes.example.spring -DartifactId=SpringCoreHelloWorld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=
false
|
2. Update pom.xml
Add dependency of Spring core and context.
File : pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<
project
xmlns
=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
<
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
>
<
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.
1
|
mvn eclipse:eclipse
|
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.
5. Write Spring bean
Create a class 'SpringCoreHelloWorld' under the package 'com.srccodes.example.spring' and copy the following content.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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.
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
1
2
3
4
5
6
7
8
9
10
11
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
xsi:schemaLocation="http://www.springframework.org/schema/beans
<
bean
id
=
"helloWorldBean"
class
=
"com.srccodes.example.spring.SpringCoreHelloWorld"
>
<
property
name
=
"message"
value
=
"Hello World!"
/>
</
bean
>
</
beans
>
|
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'.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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
1
2
3
4
5
6
7
|
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!
|