Below are the Steps to Create Sample Hello World Example in Spring
Add the Spring 3.0 dependencies listed below in Maven’s pom.xml file.
Add the Spring 3.0 dependencies listed below in Maven’s pom.xml 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.mahesh.core</groupId> <artifactId>Spring3HelloExample</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>Spring3HelloExample</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.0.5.RELEASE</spring.version> </properties> <dependencies> <!-- Spring 3 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>
Spring bean
package com.mahesh.core; /** * Spring bean * */ public class HelloWorld { private String name; public void setName(String name) { this.name = name; } public void printHello() { System.out.println("Spring 3 : Hello World ! " + name); } }
Spring bean configuration file
SpringBeans.xml
<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="helloBean" class="com.mahesh.core.HelloWorld"> <property name="name" value="Mahesh" /> </bean> </beans>
Run It
package com.mahesh.core; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "SpringBeans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloBean"); obj.printHello(); } }
Output
Spring 3 : Hello World ! Mahesh
Very Nice Tutorial, it is very helpful for beginners who wants to Use Spring. Expecting More tutorials on Spring ...
ReplyDelete