maven项目ssh框架的整合
Posted 洞若观火
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了maven项目ssh框架的整合相关的知识,希望对你有一定的参考价值。
1.环境
eclipse版本:Eclipse Mars2 4.5
jdk版本:1.8
maven版本:apache-maven 3.3.9zhnegs
这是主要的开发工具版本,ssh的各种jar包版本就不列举了。
2.依赖传递与依赖冲突的解决
在整合开始前需要先介绍依赖传递与依赖冲突的问题;
依赖传递:
如图。可以看到在pom文件中只是导入了struts的核心包,在项目中去将核心包依赖的jar包全部都导入进来,这种依赖关系就会产生依赖传递。
依赖冲突:那如何会冲突呢,在整合ssh框架中,项目采用的包会有重复,那么就会产生问题,你希望导入的包并没有导入。
jar包
解决方案:
第一声明原则:先导入有高版本的jar包
1 <dependencies> 2 <!-- spring-beans-4.2.4 --> 3 <dependency> 4 <groupId>org.springframework</groupId> 5 <artifactId>spring-context</artifactId> 6 <version>4.2.4.RELEASE</version> 7 </dependency> 8 9 10 <!-- spring-beans-3.0.5 --> 11 <dependency> 12 <groupId>org.apache.struts</groupId> 13 <artifactId>struts2-spring-plugin</artifactId> 14 <version>2.3.24</version> 15 </dependency>
路径最短优先原则:即放弃依赖导入,直接单独导入该包
1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-beans</artifactId> 4 <version>4.2.4.RELEASE</version> 5 </dependency>
排除原则:
1 <dependency> 2 <groupId>org.apache.struts</groupId> 3 <artifactId>struts2-spring-plugin</artifactId> 4 <version>2.3.24</version> 5 <exclusions> 6 <exclusion> 7 <groupId>org.springframework</groupId> 8 <artifactId>spring-beans</artifactId> 9 </exclusion> 10 </exclusions> 11 </dependency>
版本锁定原则:
1 <properties> 2 <spring.version>4.2.4.RELEASE</spring.version> 3 <hibernate.version>5.0.7.Final</hibernate.version> 4 <struts.version>2.3.24</struts.version> 5 </properties> 6 7 <!-- 锁定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 --> 8 <dependencyManagement> 9 <dependencies> 10 <dependency> 11 <groupId>org.springframework</groupId> 12 <artifactId>spring-context</artifactId> 13 <version>${spring.version}</version> 14 </dependency> 15 </dependencies> 16 </dependencyManagement>
3.pom文件整合
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>com.lsd.maven-web</groupId> 4 <artifactId>maven-ssh</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 8 9 <!-- 属性 --> 10 <properties> 11 <spring.version>4.2.4.RELEASE</spring.version> 12 <hibernate.version>5.0.7.Final</hibernate.version> 13 <struts.version>2.3.24</struts.version> 14 </properties> 15 16 <!-- 锁定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 --> 17 <dependencyManagement> 18 <dependencies> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-context</artifactId> 22 <version>${spring.version}</version> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-aspects</artifactId> 27 <version>${spring.version}</version> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-orm</artifactId> 32 <version>${spring.version}</version> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework</groupId> 36 <artifactId>spring-test</artifactId> 37 <version>${spring.version}</version> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-web</artifactId> 42 <version>${spring.version}</version> 43 </dependency> 44 <dependency> 45 <groupId>org.hibernate</groupId> 46 <artifactId>hibernate-core</artifactId> 47 <version>${hibernate.version}</version> 48 </dependency> 49 <dependency> 50 <groupId>org.apache.struts</groupId> 51 <artifactId>struts2-core</artifactId> 52 <version>${struts.version}</version> 53 </dependency> 54 <dependency> 55 <groupId>org.apache.struts</groupId> 56 <artifactId>struts2-spring-plugin</artifactId> 57 <version>${struts.version}</version> 58 </dependency> 59 </dependencies> 60 </dependencyManagement> 61 <!-- 依赖管理 --> 62 <dependencies> 63 <!-- spring --> 64 <dependency> 65 <groupId>org.springframework</groupId> 66 <artifactId>spring-context</artifactId> 67 </dependency> 68 <dependency> 69 <groupId>org.springframework</groupId> 70 <artifactId>spring-aspects</artifactId> 71 </dependency> 72 <dependency> 73 <groupId>org.springframework</groupId> 74 <artifactId>spring-orm</artifactId> 75 </dependency> 76 <dependency> 77 <groupId>org.springframework</groupId> 78 <artifactId>spring-test</artifactId> 79 </dependency> 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-web</artifactId> 83 </dependency> 84 <!-- hibernate --> 85 <dependency> 86 <groupId>org.hibernate</groupId> 87 <artifactId>hibernate-core</artifactId> 88 </dependency> 89 90 <!-- 数据库驱动 --> 91 <dependency> 92 <groupId>mysql</groupId> 93 <artifactId>mysql-connector-java</artifactId> 94 <version>5.1.6</version> 95 <scope>runtime</scope> 96 </dependency> 97 <!-- c3p0 --> 98 99 <dependency> 100 <groupId>c3p0</groupId> 101 <artifactId>c3p0</artifactId> 102 <version>0.9.1.2</version> 103 </dependency> 104 105 106 <!-- 导入 struts2 --> 107 <dependency> 108 <groupId>org.apache.struts</groupId> 109 <artifactId>struts2-core</artifactId> 110 </dependency> 111 <dependency> 112 <groupId>org.apache.struts</groupId> 113 <artifactId>struts2-spring-plugin</artifactId> 114 </dependency> 115 116 <!-- servlet jsp --> 117 <dependency> 118 <groupId>javax.servlet</groupId> 119 <artifactId>servlet-api</artifactId> 120 <version>2.5</version> 121 <scope>provided</scope> 122 </dependency> 123 <dependency> 124 <groupId>javax.servlet</groupId> 125 <artifactId>jsp-api</artifactId> 126 <version>2.0</version> 127 <scope>provided</scope> 128 </dependency> 129 <!-- 日志 --> 130 <dependency> 131 <groupId>org.slf4j</groupId> 132 <artifactId>slf4j-log4j12</artifactId> 133 <version>1.7.2</version> 134 </dependency> 135 <!-- junit --> 136 <dependency> 137 <groupId>junit</groupId> 138 <artifactId>junit</artifactId> 139 <version>4.9</version> 140 <scope>test</scope> 141 </dependency> 142 <!-- jstl --> 143 <dependency> 144 <groupId>javax.servlet</groupId> 145 <artifactId>jstl</artifactId> 146 <version>1.2</version> 147 </dependency> 148 </dependencies> 149 150 <build> 151 <plugins> 152 <!-- 设置编译版本为1.7 --> 153 <plugin> 154 <groupId>org.apache.maven.plugins</groupId> 155 <artifactId>maven-compiler-plugin</artifactId> 156 <configuration> 157 <source>1.7</source> 158 <target>1.7</target> 159 <encoding>UTF-8</encoding> 160 </configuration> 161 </plugin> 162 163 <!-- maven内置 的tomcat6插件 --> 164 <plugin> 165 <groupId>org.codehaus.mojo</groupId> 166 <artifactId>tomcat-maven-plugin</artifactId> 167 <version>1.1</version> 168 <configuration> 169 <!-- 可以灵活配置工程路径 --> 170 <path>/ssh</path> 171 <!-- 可以灵活配置端口号 --> 172 <port>8080</port> 173 </configuration> 174 </plugin> 175 176 </plugins> 177 </build> 178 179 180 </project>
建议整合时先手动确定版本在进行导入
4.配置ssh项目
注意:有些配置文件需要在接口或者类同级目录下,那么需要在maven项目的resource文件夹下创建一个相同路径的目录
dao(这里将model层类一起包含,可以再细分):domain模型类、dao接口、dao实现类、hibernate核心配置文件、对应的映射关系文件、将dao配置进spring管理
service:service接口、service实现类、spring管理注册service
action:action类、struts2配置文件、web.xml配置
都是ssh基本整合,这里不过多解释,主要介绍maven项目的导包。
以上是关于maven项目ssh框架的整合的主要内容,如果未能解决你的问题,请参考以下文章
Eclipse下面的Maven管理的SSH框架整合(Struts,Spring,Hibernate)