基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之一 : Maven的pom.xml环境配置及注意方面

本文主要是介绍基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之一 : Maven的pom.xml环境配置及注意方面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        先说下我们的开发环境,用的Eclipse版本为最新下载的Eclipse for JavaEE, Mars.2 Release (4.5.2),已经内置了maven,因为外面maven越来越流行,而且管理库依赖非常方便,所以也顺便用学习学习,但这几天的使用感觉,eclipse下的maven有点傻,而且会无缘无故让项目的配置参数丢失,最后不知道动了什么修改的xml文件、java文件不会自动编译了亦不会同步到tomcat的调试目录,为此不得不重新恢复之前的备份。

        用的框架版本:spring 4.2.0.RELEASE, hibernate-core/hibernate-entitymanager版本4.3.5.Final,postgresql jdbc版本9.4.1208

下面给出完整的pom.xml, pom配置对spring来说至关重要,因为一不小心配置里面有不同版本的依赖同时存在,就很可以让整个spring加载不起来!

(16/Jun/2016重新修改)

<!-- http://mvnrepository.com/ 想要什麽,通過這個URL查找 -->
<!-- -->
<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.freestyle.gpchat</groupId><artifactId>gpchat</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>gpchat Maven Webapp</name><url>http://maven.apache.org</url><properties><spring.version>4.2.0.RELEASE</spring.version><struts.version>2.3.28.1</struts.version><aspectj.version>1.8.7</aspectj.version><spring-hibernate.version>4.3.5.Final</spring-hibernate.version><hibernate.version>3.2.7.ga</hibernate.version></properties><dependencies><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- Spring dependencies --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><!-- http://mvnrepository.com/artifact/org.springframework/spring-jdbc --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><!-- http://mvnrepository.com/artifact/org.springframework/spring-orm --><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version></dependency><!-- http://mvnrepository.com/artifact/org.springframework/spring-aop --><!-- <dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency>--><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>${aspectj.version}</version></dependency>		<!-- http://mvnrepository.com/artifact/org.aspectj/aspectjweaver --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.9</version></dependency><!-- http://mvnrepository.com/artifact/javassist/javassist -->
<!-- 
<dependency><groupId>javassist</groupId><artifactId>javassist</artifactId><version>3.8.1.GA</version>
</dependency>-->	<!-- Struts2 --><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>${struts.version}</version><exclusions><exclusion><groupId>javassist</groupId><artifactId>javassist</artifactId></exclusion><exclusion> <!-- we prefer our explicit version, though it should be the same --><groupId>asm</groupId><artifactId>asm</artifactId></exclusion><exclusion> <!-- we prefer our explicit version, though it should be the same --><groupId>asm</groupId><artifactId>asm-commons</artifactId></exclusion><exclusion> <!-- we prefer our explicit version, though it should be the same --><groupId>org.ow2.asm</groupId><artifactId>asm</artifactId></exclusion><exclusion> <!-- we prefer our explicit version, though it should be the same --><groupId>org.ow2.asm</groupId><artifactId>asm-commons</artifactId></exclusion></exclusions></dependency><!-- http://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin --><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>${struts.version}</version></dependency><!-- http://mvnrepository.com/artifact/org.apache.struts/struts2-convention-plugin --><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-convention-plugin</artifactId><version>${struts.version}</version></dependency><!-- 添加Hibernate依赖 --><!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-core --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>${spring-hibernate.version}</version><exclusions><exclusion><groupId>org.javassist</groupId><artifactId>javassist</artifactId></exclusion></exclusions>			</dependency><!-- http://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api --><!-- -<dependency><groupId>org.hibernate.javax.persistence</groupId><artifactId>hibernate-jpa-2.0-api</artifactId><version>1.0.1.Final</version></dependency>--><!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>4.3.11.Final</version><!-- <exclusions><exclusion><groupId>org.javassist</groupId><artifactId>javassist</artifactId></exclusion></exclusions>-->		    </dependency><!-- http://mvnrepository.com/artifact/org.postgresql/postgresql --><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><version>9.4.1208</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency><!-- http://mvnrepository.com/artifact/com.cloudhopper.proxool/proxool --><dependency><groupId>com.cloudhopper.proxool</groupId><artifactId>proxool</artifactId><version>0.9.1</version></dependency><!-- http://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.3.2</version></dependency><!-- http://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.3.2</version></dependency><!-- http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.6.3</version></dependency><!-- http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.6.3</version></dependency><!-- http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.6.3</version></dependency><!-- http://mvnrepository.com/artifact/log4j/log4j --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- http://mvnrepository.com/artifact/javax.mail/mail --><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.7</version></dependency><!-- http://mvnrepository.com/artifact/commons-logging/commons-logging --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.1</version></dependency><!-- http://mvnrepository.com/artifact/com.belerweb/pinyin4j --><dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.0</version></dependency><!-- http://mvnrepository.com/artifact/com.google.zxing/core --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.2.0</version></dependency><!-- http://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.0</version></dependency><!-- http://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --><dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.9.2</version></dependency><!-- http://mvnrepository.com/artifact/cglib/cglib -->
<dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>3.1</version>
</dependency></dependencies><build><finalName>gpchat</finalName><sourceDirectory>src/main/java</sourceDirectory>  <testSourceDirectory>src/test/java</testSourceDirectory>  <resources>  <resource>  <directory>src/main/resources</directory>  </resource>  </resources>  <testResources>  <testResource>  <directory>src/test/resources</directory>  </testResource>  </testResources>  <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>  <testOutputDirectory>src/main/webapp/WEB-INF/classes</testOutputDirectory>  <plugins>  <plugin>  <groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId>  <version>3.2</version>  <configuration>  <source>1.6</source>  <target>1.6</target>  </configuration>  </plugin>  <plugin>  <artifactId>maven-resources-plugin</artifactId>  <version>2.5</version>  <executions>  <execution>  <phase>compile</phase>  </execution>  </executions>  </plugin>  <plugin>  <artifactId>maven-clean-plugin</artifactId>  <version>2.4.1</version>  <configuration>  <filesets>  <fileset>  <directory>src/main/webapp/WEB-INF/lib</directory>  <followSymlinks>false</followSymlinks>  </fileset>  </filesets>  </configuration>  </plugin>  </plugins>  </build>
</project>



    另外,需要注意的是如果tomcat启动加载spring失败,提示spring组件类找不到,但如果复制这个类在ide里面随便打开个java文件用其定义变量又可以解析成功,这时候通常是maven的依赖库没有分发到tomcat的调试目录 ,而且是用着用着就丢失的,真不知道做错了什么动作,解决办法是右键打开project的properties,找到 deployment assembly,将maven依赖分发出去,如下图:




maven install后,请打开target\xxx\WEB-INF\lib,仔细检查是否有同一个jar是否有不同版本存在,如果有,则说明pom里面有重复配置,需要删除,按照上面的pom.xml运行,我的jar file list 如下:

2016/06/01  09:21            62,983 activation-1.1.jar
2015/09/11  10:56           445,288 antlr-2.7.7.jar
2015/09/11  10:56             4,467 aopalliance-1.0.jar
2016/06/16  11:59            47,895 asm-4.2.jar
2015/12/25  16:37           116,927 aspectjrt-1.8.7.jar
2016/06/14  11:53         1,864,922 aspectjweaver-1.8.9.jar
2016/01/18  17:35            32,112 avalon-framework-api-4.3.jar
2016/01/18  17:35            83,582 avalon-logkit-2.1.jar
2016/06/16  11:59           283,080 cglib-3.1.jar
2016/06/15  12:17           233,859 commons-beanutils-1.9.2.jar
2016/05/31  17:51           232,771 commons-codec-1.6.jar
2015/09/11  10:56           575,389 commons-collections-3.2.1.jar
2016/01/19  09:17            69,002 commons-fileupload-1.3.1.jar
2016/01/19  09:17           173,587 commons-io-2.2.jar
2016/06/15  12:14           306,008 commons-lang3-3.0.jar
2015/09/12  10:49            60,686 commons-logging-1.1.1.jar
2016/06/01  09:21           544,173 core-3.2.0.jar
2016/05/31  17:14           313,898 dom4j-1.6.1.jar
2016/05/31  17:42         1,300,487 freemarker-2.3.22.jar
2016/01/18  17:35            96,369 geronimo-spec-javamail-1.3.1-rc3.jar
2016/01/18  17:35            30,838 geronimo-spec-jms-1.1-rc4.jar
2016/06/14  10:46            75,311 hibernate-commons-annotations-4.0.4.Final.jar
2016/06/14  10:46         5,230,007 hibernate-core-4.3.5.Final.jar
2016/06/14  10:22           648,087 hibernate-entitymanager-4.3.11.Final.jar
2016/06/13  17:01           113,371 hibernate-jpa-2.1-api-1.0.0.Final.jar
2016/05/31  17:50           587,762 httpclient-4.3.2.jar
2016/05/31  17:49           282,269 httpcore-4.3.2.jar
2016/05/31  17:49            37,148 httpmime-4.3.2.jar
2016/05/31  17:53            46,989 jackson-annotations-2.6.3.jar
2016/05/31  17:53           258,875 jackson-core-2.6.3.jar
2016/05/31  17:52         1,170,801 jackson-databind-2.6.3.jar
2016/06/14  10:47            76,551 jandex-1.1.0.Final.jar
2016/06/14  10:47           714,194 javassist-3.18.1-GA.jar
2016/06/14  10:46            57,183 jboss-logging-3.1.3.GA.jar
2016/06/14  10:22            11,558 jboss-logging-annotations-1.2.0.Beta1.jar
2016/06/14  10:22            27,717 jboss-transaction-api_1.2_spec-1.0.0.Final.jar
2015/12/22  12:09           414,240 jstl-1.2.jar
2016/06/01  09:20           489,884 log4j-1.2.17.jar
2016/06/01  09:21           521,157 mail-1.4.7.jar
2016/05/31  17:33           230,352 ognl-3.0.14.jar
2016/06/01  09:21           209,862 pinyin4j-2.5.0.jar
2016/05/31  17:43           648,601 postgresql-9.4.1208.jar
2016/01/18  17:35           200,759 proxool-0.9.1.jar
2016/01/18  15:35           334,469 proxool-cglib-0.9.1.jar
2016/01/18  17:35            77,977 servlet-api-2.3.jar
2016/06/14  10:20           363,974 spring-aop-4.2.0.RELEASE.jar
2016/06/14  10:20           729,550 spring-beans-4.2.0.RELEASE.jar
2016/06/14  10:21         1,084,471 spring-context-4.2.0.RELEASE.jar
2016/06/14  10:19         1,052,383 spring-core-4.2.0.RELEASE.jar
2016/06/14  10:21           262,391 spring-expression-4.2.0.RELEASE.jar
2016/06/14  10:22           421,645 spring-jdbc-4.2.0.RELEASE.jar
2016/06/14  10:52           465,589 spring-orm-4.2.0.RELEASE.jar
2016/06/14  10:22           262,961 spring-tx-4.2.0.RELEASE.jar
2016/06/14  10:19           765,877 spring-web-4.2.0.RELEASE.jar
2016/06/14  10:21           865,404 spring-webmvc-4.2.0.RELEASE.jar
2016/05/31  17:33            67,524 struts2-convention-plugin-2.3.28.1.jar
2016/05/31  17:31           874,267 struts2-core-2.3.28.1.jar
2016/05/31  17:33            21,696 struts2-spring-plugin-2.3.28.1.jar
2016/05/31  17:15           109,318 xml-apis-1.0.b2.jar
2016/05/31  17:33           677,347 xwork-core-2.3.28.1.jar


这篇关于基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之一 : Maven的pom.xml环境配置及注意方面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/190100

相关文章

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis