Sunday, May 12, 2013

Spring 3 Hello World

Create a Maven project, make the pom.xml as the sample code below and convert it to Eclipse importable by mvn eclipse:eclipse

<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>com.example.core</groupId>
  <artifactId>Spring3HelloWorld</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Spring3HelloWorld</name>
  <url>http://maven.apache.org</url>

  <properties>
    <org.springframework-version>3.2.2.RELEASE</org.springframework-version>
  </properties>

  <dependencies>
  
        <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
        </dependency>
 
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${org.springframework-version}</version>
 </dependency>
 
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${org.springframework-version}</version>
 </dependency>
 
  </dependencies>
</project>
 
After the project is imported into Eclipse, create a resources folder first. Select the project on Project Explorer, then go to Project -> Properties, then Add Folder...  

















Add the resource under <Project Name>/src/main

 
 
 
 
 
  
 
 
 
 
 
 
 
 
 
Add the AppBeans.xml with the sample code to <Project Name>/main/src/resources
 
<?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.0.xsd">
 
 <bean id="helloworldBean" class="com.example.core.HelloWorld">
  <property name="name" value="Spring 3" />
 </bean>
</beans> 
 
 
Add the HelloWorld.java to <Project Name>/main/src/java
 
package com.example.core;

public class HelloWorld {
 private String name;
 
 public void setName(String _name){
  this.name = _name;
 }
 
 public void sayHello(){
  System.out.println("Hello World, " + this.name + "!");
 }
}
 
Add the code to App.java
 
package com.example.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ) {
     ApplicationContext context = new ClassPathXmlApplicationContext("AppBeans.xml");
     HelloWorld obj = (HelloWorld)context.getBean("helloworldBean");
     obj.sayHello();
    }
} 
 

The final file structure 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Select App.java and run as Java Application
Hello World, Spring 3!
 
 

No comments:

Post a Comment