Spring 3 hello world example
Here is the first step to start spring application development. This page helps you to understand basic spring hello world example.
This page gives details about all required components to build spring application.
Create a maven project, and here is the pom.xml reference. The pom.xml should contain all spring dependencies.
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringHelloWorld</groupId>
<artifactId>SpringHelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>3.2.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
|
Create a simple spring bean class, here is the sample class:
package com.java2novice.beans;
public class SpringFirstTest {
public void testMe(){
System.out.println("I am listening...");
}
}
|
You need to create spring configuration file and declare all of your beans here. I am naming the configuration file as
applicationContext.xml file.
<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.0.xsd">
<bean id="springTest" class="com.java2novice.beans.SpringFirstTest" />
</beans>
|
Project structure snapshot:

Here is the Spring Demo class to run the spring bean:
package com.java2novice.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2novice.beans.SpringFirstTest;
public class SpringDemo {
public static void main(String a[]){
String confFile = "applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(confFile);
SpringFirstTest sft = (SpringFirstTest) context.getBean("springTest");
sft.testMe();
}
}
|
|