Pre Requisite :
Download and install the Spring STS into your machine. This will provide all the default spring configurations whenever you create the project
Application Creation Steps:
Step 1: Spring Maven project in your STS. This gives the project folder structure and one pom.xml file in root folder. Below is the Sample 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/xsd/maven-4.0.0.xsd”>
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>Spring-Project</artifactId>
<version>0.0.1-SNAPSHOT</version><properties>
<!– Generic properties –>
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!– Spring –>
<spring-framework.version>3.2.3.RELEASE</spring-framework.version><!– Hibernate / JPA –>
<hibernate.version>4.2.1.Final</hibernate.version><!– Logging –>
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version><!– Test –>
<junit.version>4.11</junit.version></properties>
<dependencies>
<!– Spring and Transactions –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency><!– Logging with SLF4J & LogBack –>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency><!– Hibernate –>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!– Test Artifacts –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency></dependencies>
</project>
Step 2: Create the bean class HelloWorldBean.java as below
package in.malliktalksjava.spring.examples;
public class HelloWorldBean {
private String message;
public void setMessage(String message){
this.message = message;
}public void printMessage(){
System.out.println(“You are in Print Message : “+message);
}
}
Step 3: Create Beans Xml file and configure the HellowWorldBean into that file to make use of dependency injection.
<?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.2.xsd”>
<bean id=”helloWorldBean” class=”in.malliktalksjava.spring.examples.HelloWorldBean”>
<property name=”message” value=”Hello World”></property>
</bean></beans>
Step 4: Create the Main Application class SpringCoreBeansXmlExample.java and load the beans xml
package in.malliktalksjava.spring.examples;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/**
* @author mallikarjungunda
*
*/
public class SpringCoreBeansXmlExample {/**
* @param args
*/
public static void main(String[] args) {//Load the beans.xml into Application Context
ApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”);//Inject the object whenever required
HelloWorldBean helloWorldBean = (HelloWorldBean)context.getBean(“helloWorldBean”);helloWorldBean.printMessage();
}
}
Below is the out put that prints whenever you run the class.
You are in Print Message : Hello World
Thank you for going through the this example. Refer to Menu bar for other tutorials.