准备工作:

1.下载eclipse(Eclipse Java EE IDE for Web Developers,Version: Juno Service Release 2)。

2.下载maven3.3.3。

3.安装maven插件()。 

注意事项:maven3.3.3支持jdk1.7,maven3.2.5仅仅支持到1.6。 

第一步:新建maven项目,选择Maven Project,如图1。 

eclipse下构建maven spring项目_java eclipse下构建maven spring项目_xml_02 

 eclipse下构建maven spring项目_maven-java-Spring_03eclipse下构建maven spring项目_spring_04

第二步:配置转换项目为spring项目。

首先右击项目,点击Properties,选择左边的java build path,我们先把出问题的删除,如果不删除,后面在新建source file时无法新建,如图,选择remove就行了。

eclipse下构建maven spring项目_xml_05 

  新建src/main/java、src/test/java/、src/test/resources,如图,提示:如果你eclipse是选择java模式,可以直接新建source file,j2ee模式可以切换到java模式。

 eclipse下构建maven spring项目_java_06

修改jdk版本,如果此版本不修改,会造成maven与jdk不兼容,导致编译出问题,右击JRE System Library[J2SE-1.5] 修改问1.7,如图。

eclipse下构建maven spring项目_xml_07 

 你可以调整source folder的顺序,根据自己的喜好,我不知道有什么影响,看到有的教程上有调整的,如果你有强迫症,可以修改一下顺序,如图。

eclipse下构建maven spring项目_spring_08 

下面,我们解决最重要的一个步骤,spring项目的雏形就在这里显现, 把项目转化为web项目,先找到位置,如图。

 eclipse下构建maven spring项目_java_09

点击Convert to faceted form... (注释:有的教程修改Further configuration available...,其实一般默认就可以,不用可以修改,我的eclipse就选的默认的,没修改),在右边选择你项目运行的应用服务器,我选的是tomcat 7.0,你选的maven 和tomcat与你的jdk有很的关系,现在虽然8出来了,单是选择1.7较为稳定,只是建议,根据个人喜好。

eclipse下构建maven spring项目_maven_10 

现在就转化为了web项目,我们在配置其他的东西,完成整个项目 。

  现在的目录结构为这种情况,如图:

eclipse下构建maven spring项目_xml_11

  第三步:转化为spring项目,添加applicationContext.xml、simple-servlet.xml,配置spring文件,配置spring引用包即确定引用文件pom.xml。

applicationContext.xml 、demo-servlet.xml、web.xml为spring配置文件,demo-servlet.xml为配置转发器,主要作用与view层,applicationContext.xml主要配置数据库与事务,在这里不过多讲解,单是这两个文件一定要搞清楚怎么配置,每个配置模块什么意思,不求甚解会害惨自己,出个问题,找不到原因,我就曾经在这个地方吃过亏。对于pom.xml文件,我建议大家整理出一个自己需要的版本,之前大家不使用maven的时候,都是整理自己一个完整的lib包,maven的作用就是替大家管理引用文件,pom.xml文件就是配置需要引入的包,你项目依赖哪些包,就在这些pom.xml文件里引用,maven会帮你下载。下面依次列出每个文件的内容。

demo-servlet.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

<context:component-scan base-package="com.demo.controller" />
<mvc:annotation-driven />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>


applicationContext.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<context:component-scan base-package="com.demo" />

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://XXX.XXX.130.XXX:3306/anhe?useUnicode=true&characterEncoding=UTF-8"
p:username="root" p:password="XXXXXX" />
<!-- 配置Jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource" />

<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />

<!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod"
expression=" execution(* com.baobaotao.service..*(..))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
</beans>


web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>demo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>


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>smart</groupId>
<artifactId>demo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>com.springsource.repository.bundles.release</id>
<name>EBR Spring Release Repository</name>
<url>http:// repository.springsource.com/maven/bundles/release</url>
</repository>
<repository>
<id>com.springsource.repository.bundles.external</id>
<name>EBR External Release Repository</name>
<url>http:// repository.springsource.com/maven/bundles/external</url>
</repository>
</repositories>
<properties>
<org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>
<!-- Core utilities used by other modules. Define this if you use Spring
Utility APIs (org.springframework.core.*/org.springframework.util.*) -->
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Expression Language (depends on spring-core) Define this if you use
Spring Expression APIs (org.springframework.expression.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define
this if you use Spring Bean APIs (org.springframework.beans.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core,
spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Application Context (depends on spring-core, spring-expression, spring-aop,
spring-beans) This is the central artifact for Spring's Dependency Injection
Container and is generally always defined -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Various Application Context utilities, including EhCache, JavaMail,
Quartz, and Freemarker integration Define this if you need any of these integrations -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Transaction Management Abstraction (depends on spring-core, spring-beans,
spring-aop, spring-context) Define this if you use Spring Transactions or
DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context,
spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA,
and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx)
Define this if you need ORM (org.springframework.orm.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB,
JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans,
spring-context) Define this if you need OXM (org.springframework.oxm.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Web application development utilities applicable to both Servlet and
Portlet Environments (depends on spring-core, spring-beans, spring-context)
Define this if you use Spring MVC, or wish to use Struts, JSF, or another
web framework with Spring (org.springframework.web.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans,
spring-context, spring-web) Define this if you use Spring MVC with a Servlet
Container such as Apache Tomcat (org.springframework.web.servlet.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans,
spring-context, spring-web) Define this if you use Spring MVC with a Portlet
Container (org.springframework.web.portlet.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Support for testing Spring applications with tools such as JUnit and
TestNG This artifact is generally always defined with a 'test' scope for
the integration testing framework and unit testing stubs -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
</dependencies>
<build>
<finalName>demo</finalName>
</build></project>


配置一下项目依赖关系,如图,remove右边红色框里面的内容,部署不需要把test文件部署上去,是不是???毋庸置疑。

 


eclipse下构建maven spring项目_maven_12


再Add如下文件,如图:

 


eclipse下构建maven spring项目_spring_13


不知道你们发现吗?现在项目还是红叉+s,那是因为我们还有一个地方没有修改,如图: 

 


eclipse下构建maven spring项目_spring_14


当我们配置好之后,项目又有可能自动改回来,看看之前是不是我们选择过jdk版本,但是现在又改了一次,那是因为还有个地方捣鬼,我当时就吃亏到这个地方,浪费了好几十分钟思考,结果还是不如谷歌,哦,又想起一点,谷歌不是被封了,用这个"谷粉http://www.jwss.com/" ,也是谷歌的引擎。修改下图配置,java编译器:


eclipse下构建maven spring项目_java_15


 

此刻,我们神奇的发现,是不是在maven依赖包里出现了如下美好画面:


eclipse下构建maven spring项目_maven_16 


至此,项目配置文件搞定,下面我们添加示例代码演示maven神奇之处。

第五步:测试项目。我们只写一个拦截器,来测试一下。

新建一个jsp文件夹,如图:


eclipse下构建maven spring项目_maven_17


 

在controller包中,我们新建一个java类HomeController,添加如下代码:



package com.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.demo.dao.HomeServcies;

@Controller
public class HomeController {
@Autowired
HomeServcies homeServcies;

@RequestMapping(value = "index")
public String Index() {
return "/jsp/index";
}

}


 测试运行:

出现神奇的,如图:

eclipse下构建maven spring项目_xml_18

到此,结束了。