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 2.2.1
Eclipse 3.6
JDK 1.6.0.13
Generate project structure with maven
In command prompt, issue following Maven command :
mvn archetype:create -DgroupId=wei.peng.app -DartifactId=docgenDemo
Maven will generated all the Java’s standard folders structure for you (beside resources folder, you need to create it manually)
Covert to Eclipse projectType “
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 folder
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.
Add spring dependencyAdd Spring dependency in Maven’s
pom.xml file.
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>wei.peng.app</groupId>
<artifactId>docgen-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>docgen-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 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.
Spring BeanCreate 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 wei.peng.app;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Administrator
* Hello world! (Maven + Spring)
*/
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void printHello() {
System.out.println("Hello " + name);
}
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("docgen-transformations-beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloBean");
obj.printHello();
}
}
Spring bean configuration fileCreate a xml file (Spring-Module.xml) at “
src/main/resources/docgen-transformations-beans.xml“. This is the Spring’s bean configuration files, which declared all the available Spring beans.
<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="wei.peng.app.HelloWorld">
<property name="name" value="Wpeng" />
</bean>
</beans>
Run it