We shall be creating a very simple Spring project which will use ApplicationContext and Dependency Injection to inject properties into a bean and print the value of injected property.

You can further enhance the project for web and database capabilities. The purpose is to show you how to configure Spring project in Eclipse without touching command prompt.

We have tried to keep the chaff away by providing links to source code files instead of putting inline code here in the tutorial.

Prerequisite:
You need Eclipse Juno and JRE 7 configured. For other versions of Eclipse and JDK/JRE, you need to test on your own if these steps work.

If you still need some help regarding Spring maven configuration, do leave a comment.

  • Go to Help -> Eclipse Marketplace…

Eclipse marketplace

  • Search for maven and select “Maven Integration for Eclipse WTP (Incubation)”

Eclipse maven plugin

  • On clicking next, select all the features of maven plugin.

plugin features

  • The installer will take some time to complete, and will then ask for restart of eclipse. Complete the restart.

installing maven

  • Go to File -> New -> Other and start creating a new Maven project.

new maven project

  • On clicking next, check the check-box for “Create a simple project (skip archetype selection)”

new project

  • Click next and add the following information in the text boxes. The name in the following screen refers to the project name in Eclipse.

configuration parameters

  • Click finish and you will see the following project structure.

project structure

  • After configuring the project, there will be a problem reported regarding wrong JRE on classpath. This is because maven couldn’t tell which JRE would you prefer so it just sets a default JRE on classpath. Go to project build path, and configure the required JDK/JRE:

JDK on classpath

  • Now change the pom.xml and replace the existing contents with following contents. Make user to change the contents of pom.xml as per requirements. Here we are importing version 3.0.5 of Spring and adding core, web and beans modules. You can add more dependencies here like we added for log4j and commons-lang etc.

pom.xml

  • Right click on pom.xml and select Run As -> Maven Install

maven install of project

  • The above step will fix the archetype issues reported by Eclipse in Problems view. When running maven install, the following output shall be see on the console:

[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 30.044s
[INFO] Finished at: Thu Dec 06 21:21:31 IST 2012
[INFO] Final Memory: 7M/59M
[INFO] ————————————————————————

  • After doing a maven install, there could still be some issues reported in pom.xml. Perform a clean of your project using Project -> Clean…
    Now create the following files in your project. We are creating one Java Bean, a class with main method and the spring configuration file. Each of these files can be downloaded from following links:

App.java
HelloWorld.java
spring.xml

  • Now right click on App.java, and run it as a Java Application:

run application

  • The following output shall be seen on the screen:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hello ! Spring

  • The whole project is available for download using the following link:
First Spring project configuration in Eclipse using Maven admin Maven
We shall be creating a very simple Spring project which will use ApplicationContext and Dependency Injection to inject properties into a bean and print the value of injected property. You can further enhance the project for web and database capabilities. The purpose is to show you how to configure...
We shall be creating a very simple Spring project which will use ApplicationContext and Dependency Injection to inject properties into a bean and print the value of injected property. You can further enhance the project for web and database capabilities. The purpose is to show you how to configure Spring project in Eclipse without touching command prompt. We have tried to keep the chaff away by providing links to source code files instead of putting inline code here in the tutorial. <strong>Prerequisite:</strong> You need Eclipse Juno and JRE 7 configured. For other versions of Eclipse and JDK/JRE, you need to test on your own if these steps work. If you still need some help regarding Spring maven configuration, do leave a comment. <ul> <li>Go to Help -> Eclipse Marketplace...</li> </ul> <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/Eclipse Marketplace.png" alt="Eclipse marketplace"/> <ul> <li>Search for maven and select "Maven Integration for Eclipse WTP (Incubation)"</li> </ul> <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/maven plugin for eclipse.png" alt="Eclipse maven plugin"/> <ul> <li>On clicking next, select all the features of maven plugin.</li> </ul> <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/maven features.png" alt="plugin features"/> <ul> <li>The installer will take some time to complete, and will then ask for restart of eclipse. Complete the restart.</li> </ul> <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/installation progress.png" alt="installing maven"/> <ul> <li>Go to File -> New -> Other and start creating a new Maven project.</li> </ul> <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/new maven project.png" alt="new maven project"/> <ul> <li>On clicking next, check the check-box for "Create a simple project (skip archetype selection)"</li> </ul> <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/simple archetype.png" alt="new project"/> <ul> <li>Click next and add the following information in the text boxes. The name in the following screen refers to the project name in Eclipse.</li> </ul> <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/configure maven project.png" alt="configuration parameters"/> <ul> <li>Click finish and you will see the following project structure.</li> </ul> <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/maven project structure.png" alt="project structure"/> <ul> <li>After configuring the project, there will be a problem reported regarding wrong JRE on classpath. This is because maven couldn't tell which JRE would you prefer so it just sets a default JRE on classpath. Go to project build path, and configure the required JDK/JRE:</li> </ul> <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/proper JRE JDK.png" alt="JDK on classpath"/> <ul> <li>Now change the pom.xml and replace the existing contents with following contents. Make user to change the contents of pom.xml as per requirements. Here we are importing version 3.0.5 of Spring and adding core, web and beans modules. You can add more dependencies here like we added for log4j and commons-lang etc.</li> </ul> <a href="http://www.javaexperience.com/wp-content/uploads/2012/12/pom.xml"/>pom.xml</a> <ul> <li>Right click on pom.xml and select Run As -> Maven Install</li> </ul> <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/run maven install.png" alt="maven install of project"/> <ul> <li>The above step will fix the archetype issues reported by Eclipse in Problems view. When running maven install, the following output shall be see on the console:</li> </ul> <blockquote> [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 30.044s [INFO] Finished at: Thu Dec 06 21:21:31 IST 2012 [INFO] Final Memory: 7M/59M [INFO] ------------------------------------------------------------------------ </blockquote> <ul> <li>After doing a maven install, there could still be some issues reported in pom.xml. Perform a clean of your project using Project -> Clean... Now create the following files in your project. We are creating one Java Bean, a class with main method and the spring configuration file. Each of these files can be downloaded from following links:</li> </ul> <a href="http://www.javaexperience.com/wp-content/uploads/2012/12/App.java"/>App.java</a> <a href="http://www.javaexperience.com/wp-content/uploads/2012/12/HelloWorld.java"/>HelloWorld.java</a> <a href="http://www.javaexperience.com/wp-content/uploads/2012/12/spring.xml"/>spring.xml</a> <ul> <li>Now right click on App.java, and run it as a Java Application:</li> </ul> <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/run spring application.png" alt="run application"/> <ul> <li>The following output shall be seen on the screen:</li> </ul> <blockquote> log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Hello ! Spring </blockquote> <ul> <li>The whole project is available for download using the following link:</li> </ul> <div class="download-code"><img src="http://www.javaexperience.com/wp-content/uploads/2012/12/downloadcode-e13492789421646.jpg"><a href="http://www.javaexperience.com/wp-content/uploads/2012/12/HelloSpring.zip"><br><br><strong>HelloSpring</strong></a></div>
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.