Maven + Spring hello world example
This quick guide example will use Maven to generate a simple Java project structure, and demonstrates how to retrieve Spring bean and print a “hello world” string.
Technologies used in this article :
- Spring 2.5.6
- Maven 3.0.3
- Eclipse 3.6
- JDK 1.6.0.13
For Spring 3, refer to this Maven + Spring 3 hello world example.
1. Generate project structure with Maven
In command prompt, issue following Maven command :
mvn archetype:generate -DgroupId=com.mkyong.common -DartifactId=SpringExamples -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Maven will generated all the Java’s standard folders structure for you (beside resources folder, you need to create it manually)
2. Convert to Eclipse project
Type “mvn eclipse:eclipse” to convert the newly generated Maven style project to Eclipse’s style project.
mvn eclipse:eclipse
Later, import the converted project into Eclipse IDE.
Create a resources “/src/main/resources” folder, the Spring’s bean xml configuration file will put here later. Maven will treat all files under this “resources” folder as resources files, and copy it to output classes automatically.
3. Add Spring dependency
Add Spring dependency in Maven’s pom.xml file.
File : pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong.common</groupId> <artifactId>SpringExamples</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>SpringExamples</name> <url>http://maven.apache.org</url> <dependencies> <!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> </dependencies> </project>
Issue “mvn eclipse:eclipse” again, Maven will download the Spring dependency libraries automatically and put it into your Maven’s local repository. At the same time, Maven will add the downloaded libraries into Eclipse “.classpath” for dependency purpose.
4. Spring bean (Java class)
Create a normal Java class (HelloWorld.java) at “src/main/java/com/mkyong/common/HelloWorld.java”. Spring’s bean is just a normal Java class, and declare in Spring bean configuration file later.
package com.mkyong.common; /** * Spring bean * */ public class HelloWorld { private String name; public void setName(String name) { this.name = name; } public void printHello() { System.out.println("Hello ! " + name); } }
5. Spring bean configuration file
Create a xml file (Spring-Module.xml) at “src/main/resources/Spring-Module.xml“. This is the Spring’s bean configuration files, which declared all the available Spring beans.
File : Spring-Module.xml
<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-2.5.xsd"> <bean id="helloBean" class="com.mkyong.common.HelloWorld"> <property name="name" value="Mkyong" /> </bean> </beans>
6. Review project structure
Review it and make sure the folder structure as following

7. Run It
Run App.java, it will load the Spring bean configuration file (Spring-Module.xml) and retrieve the Spring bean via getBean() method.
File : App.java
package com.mkyong.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "Spring-Module.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloBean"); obj.printHello(); } }
8. Output
Hello ! Mkyong
Appreciated you efforts , but to get more clarity and understanding please post the tutorials developing in Eclipse IDE .
Don’t get you, this post is developed in Eclipse environment.
Mkyong @ Best , I regret for confusion ., what i mean is it would be easy if you can post the step by step screen print of the development in eclipse ide .so that every individual can easily follow . What i felt is there are few developer havinf less experience could not follow this tutorial .Though its really excellent and one of the best in Internet ,like the executing the mvn . Appreciate your thoughts on this mvn eclipse:eclipse creates confusion whether it needs to execute in command prompt and which path .Again Many thanks – Muskandaza ,SIU – Carbondale
i see… sorry for the confusion, and thanks for your suggestion, i will improve it continuously.
MKyong @ Best , Really appreciated for prompt response. I could say one thing your tutorials are really helpful to many people across the globe ,who are enjoying in learning the latest technologies in java . Keep doing good work .Best wishes – Muskandaza,SIU-Carbondale
Very Very good !!
Well done boy !!
[...] This quick guide example will use Maven to generate a simple Java project structure, and demonstrates how to retrieve Spring bean and print a “hello world” string. [...]
Nice and clear ! Find another one at
http://www.java-tutorial.ch/spring/maven-and-spring
Hi mkyong,
I’m getting an error, when i enter the command ‘mvn archetype:generate’ in command prompt.
Please help me.
C:\>mvn archetype:generate
[INFO] Scanning for projects…
[INFO] Searching repository for plugin with prefix: ‘archetype’.
[INFO] org.apache.maven.plugins: checking for updates from central
[WARNING] repository metadata for: ‘org.apache.maven.plugins’ could not be retrieved from repository: central due to an error:
[INFO] Repository ‘central’ will be blacklisted
[INFO] ————————————————————————
[ERROR] BUILD ERROR
[INFO] ————————————————————————
[INFO] The plugin ‘org.apache.maven.plugins:maven-archetype-plugin’ does not exist or no valid version could be found
[INFO] ————————————————————————
[INFO] For more information, run Maven with the -e switch
[INFO] ————————————————————————
[INFO] Total time: 21 seconds
[INFO] Finished at: Tue Apr 19 17:09:47 IST 2011
[INFO] Final Memory: 1M/4M
[INFO] ————————————————————————
‘cmd’ is not recognized as an internal or external command,
operable program or batch file.
C:\>
“Repository ‘central’ will be blacklisted” ? you may behind firewall, try configure Maven to use proxy access.
I don’t understand why you chose vaadin archetype???
15: remote -> vaadin-archetype-sample (This archetype generates a Vaadin application as a Maven project.
The application contains a custom GWT widgetset that is compiled
by the GWT compiler and integrated into the project as part of the
build process. The application is based on the Vaadin Color Picker
Demo application available at http://vaadin.com.)
At my end, “15: internal -> maven-archetype-quickstart ()” , what’s your Maven version?
Hi Yong.
I have maven installed on my machine but when i execute “archetype:generate”
command it gives the following error.
E:\TestWorkspace>maven archetype:generate
__ __
| \/ |__ _Apache__ ___
| |\/| / _` \ V / -_) ‘ \ ~ intelligent projects ~
|_| |_\__,_|\_/\___|_||_| v. 1.0.2
BUILD FAILED
Goal “archetype:generate” does not exist in this project.
Total time: 1 seconds
Finished at: Mon Dec 13 14:16:53 GMT+05:30 2010
Please Help……
What’s your maven version?
It’s v1.0.2.
Do i need to update it? or need to install some plug-in over Maven?
yes, your version is too old, please get at least v 2.0
http://maven.apache.org/
I think you missed one line of explanation.
1) go to \\
here is your pom.xml.
then run following command from here (otherwise will give you error: POM not found).
Type “mvn eclipse:eclipse” to convert the newly generated project to Eclipse’s style project..
Thanks.
Thanks for your extra inputs, really appreciated it.
Thanks a lot Mykong for the nice article !!
Very nice , simple and to the point.
[...] Quick start Maven + Spring Example A quick start to create a project with Maven and Spring. [...]
[...] Quick start Maven + Spring Example A quick start to create a project with Maven and Spring. [...]
[...] Maven Tutorials (function(){ var bsa = document.createElement('script'); bsa.type = 'text/javascript'; bsa.async = true; bsa.src = '//s3.buysellads.com/ac/bsa.js'; (document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(bsa); })(); Maven + Spring + JDBC Example Written on March 9, 2010 at 8:27 am by mkyong In this tutorial, you will reuse the last simple Maven + Spring hello world example. [...]