使用maven创建简单的多模块 Spring Web项目

6次阅读

共计 7432 个字符,预计需要花费 19 分钟才能阅读完成。

第一次写技术文章,主要内容是使用 maven 创建一个简单的 SpringMVC WEB 项目,如有操作或理解错误请务必指出,当谦虚学习。做这一次的工作主要是因为想加强一下自己对 Spring Web 项目的理解,因为平时都是直接写业务代码,我觉得还是有必要自己了解一下创建项目的过程。后续会基于这个项目写更多的 SpringWeb 开发过程,希望能帮助到有需要的人。
总的来说是一个相当精简的多模块 springWeb 项目搭建过程,让我们进入正题吧

我们知道单体应用,就写在一个 project 里面的话,业务一旦庞大起来非常难以管理。
把各个模块单独抽出来可以方便的对 jar 包进行版本管理(尽管我还没经历过这个),维护项目,团队开发也会方便许多。
基本思想其实就是一个 java web 项目引用别的模块 Jar 包,最终 web 项目被打成 war 包发布。
** 而所有的 war 包项目,jar 包项目都是在同一个父模块下管理的(它们都是 Maven 项目)**
(如果你有 IDE,装好插件就用 IDE 创建吧,我个人不喜欢手动命令行创建)

1. 创建父项目

下图中:框起来打勾这个会让你跳过项目模式选择,勾选对于创建项目没有什么影响,以后也许会转一下 Maven 这方面的文章
POM 包才能做父项目,谨记!!!!!
2. 子项目结构和创建
以下是我的结构分层,你也可以按你的想法来,最终目的是要方便自己开发。
test_parent (父项目)
|—-test_web (web 项目)
|—-test_service (业务内容)
|—-test_framework (工具,框架封装、配置)
|—-test_dao (数据持久层,DO 也放这)
|—-test_controller (处理映射)

创建子项目直接右键父项目然后新建 maven module,也就是子模块
我们先创建 web 模块, 这里你可以勾选第一条然后创建简单项目,如果没有勾选,那么你要在下一步里选择 maven-achetype-webapp,这里以简单项目为例子
Group Id 和 version 都是继承父项目的一定要选择 war 包打包,不然要重新把他构建成 web 项目。如果你没选 war 包:https://www.cnblogs.com/leonk… 最后点 finish 完成
点击生成 Web 描述文件 (web.xml)
这样就完成了 Web 模块的创建,剩下的其他项目都是同样的步骤创建,都是选择 jar 包,参考下图:
3. 配置各模块的 pom.xml
pom.xml 记录所需的 jar 包,模块联系,包信息,打包参数等等信息,在多模块里我们要理清关系,不要重复引用
首先毫无疑问的是让 parent 加载 spring 的 jar 包是最方便开发的,因为默认所有模块都继承 parent,所以子模块引用 spring 内容也方便。
其次配置文件我们统一放在 framework 中进行管理。
那么先来写入 web.xml 配置吧

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns=”http://java.sun.com/xml/ns/javaee”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”
id=”WebApp_ID” version=”3.0″>
<display-name>test_web</display-name>

<context-param>
<!– 配置地址 –>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-*.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!– spring-mvc.xml 配置地址 –>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这里可以看到我们写了 classpath*,原因是它能搜索到项目目录以外的 Jar 包下文件相关:http://www.cnblogs.com/wlgqo/…web.xml 详解:https://blog.csdn.net/qq_3557…
web.xml 是对 WEB 项目来说是必须的配置文件,写好了 spring 配置文件的位置以后,就来新建 2 个 spring 配置文件, 新建的配置放在 test_framework 模块里,路径如下图 spring-context.xml spring-mvc.xml
一个是 spring-context.xml 也叫 applicationContext.xml,是 webApp 的上下文配置,也可以理解为配置 dao、service 通用 bean 的地方,但我们这里使用的是注解扫描方式配置 bean,所以就简单许多,即便有工具存在,写改 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:mvc=”http://www.springframework.org/schema/mvc”
xmlns:context=”http://www.springframework.org/schema/context” xmlns:tx=”http://www.springframework.org/schema/tx” xmlns:util=”http://www.springframework.org/schema/util”
xmlns:aop=”http://www.springframework.org/schema/aop”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd”>

<!– 注解注册 –>
<!– <context:annotation-config /> –>

<context:component-scan base-package=”com.test” >
<context:exclude-filter type=”annotation” expression=”org.springframework.stereotype.Controller” />
<context:exclude-filter type=”annotation” expression=”org.springframework.web.bind.annotation.RestController” />
</context:component-scan>

</beans>
这里要去掉对 controller 的扫描,applicationContext 初始化的上下文加载的 Bean 是对于整个应用程序共享的,不管是使用什么表现层技术,一般如 DAO 层、Service 层 Bean;DispatcherServlet (下一个要配置的东西) 初始化的上下文加载的 Bean 是只对 Spring Web MVC 有效的 Bean,如 Controller、HandlerMapping、HandlerAdapter 等等,该初始化上下文应该只加载 Web 相关组件。
context:component-scan 的 base-package 值用来决定我们需要扫描的包的基础名,具体配置相关可以看:https://www.cnblogs.com/exe19…
而 context:annotation-config/ 呢?其实也是 spring 为了方便我们开发者给我们提供的一个自动识别注解的配置,相关细节如下:解释说明:https://www.cnblogs.com/_popc… 两条配置的区别和诠释:https://www.cnblogs.com/leiOO…
下面是第二个配置文件 spring-mvc.xml
<?xml version=”1.0″ encoding=”UTF-8″ standalone=”no”?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:aop=”http://www.springframework.org/schema/aop” xmlns:mvc=”http://www.springframework.org/schema/mvc”
xmlns:p=”http://www.springframework.org/schema/p” 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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd”>

<!– 自动扫描的包名 –>
<context:component-scan base-package=”com.test.*.controller” >
<context:include-filter type=”annotation” expression=”org.springframework.stereotype.Controller” />
</context:component-scan>

<!– 默认的注解映射的支持 –>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class=”org.springframework.http.converter.StringHttpMessageConverter”>
<constructor-arg value=”UTF-8″ />
</bean>
<bean class=”org.springframework.http.converter.ResourceHttpMessageConverter” />
</mvc:message-converters>
</mvc:annotation-driven>

</beans>
包扫描没什么好说的,这里还强调 include 了注解 controller。mvc:annotation-driven 是 spring 默认的注解驱动,这个配置项一口气把一堆东西都给我们加进来了,但主要还是针对 controller 和处理请求的,具体的在下面文章中,因为加的内容有点多,所以这个留到后面研究,稍微理解作用就好:相关文章:https://blog.csdn.net/vicroad…
mvc:message-converters 顾名思义,就是用于处理请求消息的,request content-header 会记录请求的内容类型,根据这些类型,spring 会把内容转化成服务器操作的对象,这里的字符串转化是为了避免乱码,我们指定了编码格式。相关文章:https://www.jianshu.com/p/2f6…
以上,我们就已经把最简约的配置写好了。接下来我们随便写一个 controller 试试
4. 写个 Controller 吧
根据之前写好的 controller 的扫描包名,去我们 test_controller 模块里创建一个 controller

package com.test.hello.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(“/test”)
public class HelloController
{
@RequestMapping(“/get”)
public String helloGet(@RequestParam String str) throws Exception
{
return str;
}
}
很简单的一段返回请求字符串的代码,现在一切就绪可以启动服务器了,配置好 Tomcat 就可以启动了,右键 test_web –> run as –> run on server 选择创建好的 tomcat 容器,就可以启动了。接下来,访问:localhost:8080/test/test/get?str=helloWorld
如果你使用 eclipse 启动且没有正常启动,特别是出现严重错误时,请先检查 web.xml 配置命名有没有问题,然后再检查 test_web 项目的 assembly, 这个会影响项目的发布文件,下图所示,右键项目点 properties,没有 test_framework 的话就加入 framework 项目。网站无响应,检查一下 tomcat 的端口,默认是 8080。404 检查代码的映射路径。

5. 结束语
第一次写技术文章,记录一下自己的学习过程,日后会以当前项目作为基础,继续记录下自己遇到的问题和分享的知识,希望能帮助到一部分新手,此外,本篇文章中若有错误,欢迎指出,我会不断更新文章误点,不吝赐教。

正文完
 0