Spring 3 Hello world example
Technologies used
STS
Maven
JDK 1.6
POM file
Technologies used
STS
Maven
JDK 1.6
POM file
1: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2: xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3: <modelVersion>4.0.0</modelVersion>
4: <groupId>com.ravi.helloworld</groupId>
5: <artifactId>Spring3HelloWorld</artifactId>
6: <packaging>jar</packaging>
7: <version>1.0-SNAPSHOT</version>
8: <name>Spring3HelloWorld</name>
9: <url>http://maven.apache.org</url>
10: <properties>
11: <spring.version>3.2.3.RELEASE</spring.version>
12: </properties>
13: <dependencies>
14: <dependency>
15: <groupId>junit</groupId>
16: <artifactId>junit</artifactId>
17: <version>4.8.2</version>
18: <scope>test</scope>
19: </dependency>
20: <!-- Spring 3 dependencies -->
21: <dependency>
22: <groupId>org.springframework</groupId>
23: <artifactId>spring-core</artifactId>
24: <version>${spring.version}</version>
25: </dependency>
26: <dependency>
27: <groupId>org.springframework</groupId>
28: <artifactId>spring-context</artifactId>
29: <version>${spring.version}</version>
30: </dependency>
31: </dependencies>
32: </project>
The source code looks like this
1: package com.ravi.helloworld;
2: import org.springframework.context.ApplicationContext;
3: import org.springframework.context.support.ClassPathXmlApplicationContext;
4: public class App {
5: public static void main(String[] args) {
6: ApplicationContext context = new ClassPathXmlApplicationContext(
7: "AppContext.xml");
8: HelloWorld obj = (HelloWorld) context.getBean("welcomeBean");
9: obj.printHello();
10: }
11: }
The application context file
1: <beans xmlns="http://www.springframework.org/schema/beans"
2: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3: xsi:schemaLocation="http://www.springframework.org/schema/beans
4: http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
5: <bean id="welcomeBean" class="com.ravi.core.HelloWorld">
6: <property name="name" value="Ravisankar" />
7: </bean>
8: </beans>
When you run it the output will look something like this
1: Spring 3 : Hello ! Ravisankar
No comments:
Post a Comment