First Spring project configuration in Eclipse using 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 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.
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…
- Search for maven and select “Maven Integration for Eclipse WTP (Incubation)”
- On clicking next, select all the features of maven plugin.
- The installer will take some time to complete, and will then ask for restart of eclipse. Complete the restart.
- Go to File -> New -> Other and start creating a new Maven project.
- On clicking next, check the check-box for “Create a simple project (skip archetype selection)”
- Click next and add the following information in the text boxes. The name in the following screen refers to the project name in Eclipse.
- Click finish and you will see the following 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:
- 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.
- Right click on pom.xml and select Run As -> Maven Install
- 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] BUILD SUCCESS
[INFO] Total time: 30.044s
[INFO] Finished at: Thu Dec 06 21:21:31 IST 2012
[INFO] Final Memory: 7M/59M
- 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:
- Now right click on App.java, and run it as a Java 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: