Start Spring with Maven & Eclipse
First download the latest binaries from apache. Then extract it and copy to C:Program Files. Now set the environmental variables as follows.
Then edit the path variable as follows .
Now verify the installation.
Then , create a project using maven.
Type the following command .
mvn archetype:generate -DgroupId=com.supun.common -DartifactId=SpringExamples
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This will generate the folder with pom.xml file. Now open the pom.xml file. Edit the file so as it looks like below.
[sourcecode language=”xml”]
<project xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance" xmlns=”http://maven.apache.org/POM/4.0.0" 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.supun.common</groupid>
<artifactid>SpringExamples</artifactid>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringExamples</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring</artifactid>
<version>2.5.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-resources-plugin</artifactid>
<version>2.4.3</version>
</plugin>
</plugins>
</build>
</project>
[/sourcecode]
Then convert this project into an eclipse project using mvn eclipse:eclipse. Then import it as follows.
Here is the folder structure.
Now add the following classes to the App class.
[sourcecode language=”java”]
package com.supun.common;
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(Spring-Module.xml);
HelloWorld obj = (HelloWorld) context.getBean(helloBean);
obj.printHello();
}
}
[/sourcecode]
And Helloworld java as follows.
[sourcecode language=”java”]
package com.supun.common;
public class HelloWorld {
private String name;
public void setName(String name){
this.name = name;
}
public void printHello(){
System.out.println(Hello !+name);
}
}
[/sourcecode]
Then create Spring-Module.xml inside the src/main/resources folder as follows.
[sourcecode language=”xml”]
<beans xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance" xmlns=”http://www.springframework.org/schema/beans" xsi:schemalocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean class=”com.supun.common.HelloWorld” id=”helloBean”>
<property name=”name” value=”Supun”>
</property></bean>
</beans>
[/sourcecode]
Run the application
Here the output