概观
In this article we are going to work on Maven integration with Spring framework. As in the earlier Spring series, we are going to directly jump into the coding part to explore the implementation. We will explain all the components and how they can be implemented by using working code examples.
Maven is very popular nowadays for building projects in a clean way. The meaning of the word ‘MAVEN’ is ‘Accumulator of knowledge’. The main advantage of maven is to handle building process in an easy way and it makes the entire process more manageable compared to ant script.
Following is a simple example of maven and spring integration.
- The first component is a java class SpringMaven.java which contains a simple message.
package com.techalpine;
public class SpringMaven
{
公共静态无效的主要( 串[] 参数 )
{
System.out.println( “Hello Maven!” );
}
}
- The second component is xml file used by maven to specify dependencies and other details. We’re going to add a little bit to this file so that we can easily run our application though maven.
<project
<build>
<plugins>
<插入>
<的groupId>org.codehaus.mojo</的groupId>
<的artifactId>exec-maven-plugin</的artifactId>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<组态>
<mainClass>com.techalpine.SpringMaven</mainClass>
</组态>
</插入>
</plugins>
</build>
</project
- Now the 3rd step is to compile the code by using the command ‘mvn compile’. It will display an output something like this.
$ mvn compile
[INFO] Scanning for projects…
…
[INFO] ————————————————————————
[INFO] BUILD SUCCESSFUL
[INFO] ————————————————————————
[INFO] Total time: 1 second
[INFO] Finished at: Fri Sep 05 19:50:45 BST 2008
[INFO] Final Memory: 5M/12M
[INFO] ————————————————————————
$ mvn exec:java
…
[INFO] [exec:java]
Hello Maven
- The following is a modification of pom.xml file to integrate the spring framework.
<dependencies>
…
<依赖>
<的groupId>org.springframework</的groupId>
<的artifactId>spring</的artifactId>
<版本>2.5.5</版本>
</依赖>
</dependencies
- The following class has been created to test the integration of spring with maven. 这又是一个简单的Java类打印的messge,,en,公共类helloMaven,,en,公共无效迎接,,en,以下是xml配置文件,,en,春天框架,,en,DOCTYPE豆PUBLIC,,en,SPRING // DTD BEAN // EN,,da,www.springframework.org/dtd/spring-beans.dtd,,en,你好,,en,com.techalpine.helloMaven,,vi,以下是Java类测试春天Maven的整合,,en,进口com.techalpine.helloMaven,,vi,进口org.springframework.core.io.ClassPathResource,,en,进口org.springframework.beans.factory.BeanFactory,,en,进口org.springframework.beans.factory.xml.XmlBeanFactory,,en,公共类SpringMavenTest,,en,Bean工厂厂=新XmlBeanFactory类,,en,新使用ClassPathResource,,en,spring_maven.xml,,en,helloMaven你好=,,en,helloMaven,,vi,factory.getBean,,en,hello.greet,,en,现在,如果你建立并运行应用程序会显示以下输出,,en,这篇文章主要是针对谁愿意尝试的事情动手开发商,,en.
package com.techalpine;
public class helloMaven
{
private String name;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void greet()
{
System.out.println(“Hello ” + 的getName());
}
}
- Following is the xml configuration file (xml) for spring framework.
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN”
“HTTP://www.springframework.org/dtd/spring-beans.dtd”>
<beans>
<bean id=”hello” class=”com.techalpine.helloMaven”>
<property name=”名称” value=”TechAlpine” />
</bean>
</beans
- Following is the java class to test the spring maven integration.
package com.techalpine;
import com.techalpine.helloMaven;
import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
public class SpringMavenTest
{
公共静态无效的主要( 串[] 参数 )
{
BeanFactory factory = new XmlBeanFactory(
new ClassPathResource(“spring_maven.xml”));
helloMaven hello = (helloMaven) factory.getBean(“hello”);
hello.greet();
}
}
Now if you build and run the application it will display the following output.
Hello TechAlpine
结论
This article is mainly for the developers who wants to try things hands-on. 我们已经把一些理论部分有更大的压力在编码部分,,en,重点对今年春季系列是帮助开发人员编写的代码,并在他们的项目中实现它,,en,希望这篇文章能达到这一目的,,en,Maven的,,en. The focus on this Spring series is to help the developers to write code and implement it in their project. Hope this article will serve that purpose.