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.

<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>

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.

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();

}
}

And Helloworld java as follows.

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);
}
}


Then create Spring-Module.xml inside the src/main/resources folder as follows.

<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>

Run the application
Here the output

Advertisements

2 thoughts on “Start Spring with Maven & Eclipse

  1. It seems like u actually understand quite a lot
    pertaining to this subject matter and it exhibits
    thru this excellent blog post, given the name “Start Spring with Maven & Eclipse Technodyne”.
    Many thanks ,Eliza

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s