spring mvc项目搭建过程

spring mvc 架构目前是主流的java后台架构,本文讲解搭建过程以及如何整合mybatis以及mysql,这里项目的结构采用的是多module的结构,如果不知道怎么创建多module的项目结构,可以阅读我之前的一篇文章(如何使用idea创建多module项目,https://blog.csdn.net/T2080305/article/details/80545880)。

1. 首先创建多module项目工程,我们先看ghub这个父pom.xml中的结构

    

父pom.xml中 <modules>中一共包含五个模块,其中ghub-common是公共模块,也就是基础模块,主要是一些基础的配置,activemq,dubbo,jdbc,redis

剩下的ghub-log,ghub-activemq,ghub-user都是业务模块,ghub-web是web工程,由于ghub-log,ghub-activemq,ghub-user三个模块的结构差不多,因此这里就以ghub-user举例来说,下面来看他的结构,ghub-user包含三个子模块:user-dao,user-service,user-service-impl.

user-dao的项目结构

user-service的项目结构

user-service-impl的结构

下面来重点介绍ghub-web的项目结构

2. 下面依次分析ghub-web中几个重要的配置文件,同时也是spring项目搭建成功与否的重中之重

  2.1 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>ghub web project</display-name>

    <!-- 部署applicationContext的xml文件 -->
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- 部署applicationContext的xml文件 -->

    <!-- 测试环境的spring配置 -->
    <context-param>
        <param-name>spring.profiles.active</param-name>
        <!-- dev 表示开发环境 -->
        <param-value>dev</param-value>
        <!-- prod表示线上环境 -->
        <!--<param-value>prod</param-value>-->
    </context-param>
    <!-- 测试环境的spring配置 -->

    <!-- 加载log4j配置文件 -->
    <context-param>
      <param-name>log4jConfigLocation</param-name>
      <param-value>classpath:log4j/log4j.properties</param-value>
    </context-param>

    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>

    <listener>
      <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <!-- 加载log4j配置文件 -->

    <!-- Spring监听器 通过ContextLoaderListener 启动ApplicationContext ,实现Bean的实例化和装配-->
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Spring监听器 -->

    <!-- 防止spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <!-- 防止spring内存溢出监听器 -->

    <!-- 统一编码过滤器 -->
    <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>
    <!-- 统一编码过滤器 -->

    <!-- 自定义过滤器 -->
    <filter>
      <filter-name>ghubFilter</filter-name>
      <filter-class>com.ctp.ghub.filter.GhubFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>ghubFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 自定义过滤器 -->

    <!-- Spring MVC初始化 -->
    <servlet>
      <servlet-name>dispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring-servlet.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>dispatcherServlet</servlet-name>
        <!-- 这里面注意不是/* -->
      <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- Spring MVC初始化 -->

    <!-- shiro 安全过滤器 -->
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <!-- shiro 安全过滤器 -->

    <!-- 首页 -->
    <welcome-file-list>
        <welcome-file>/WEB-INF/views/index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 错误页 404-->
    <error-page>
        <error-code>404</error-code>
        <location>/404</location>
    </error-page>

    <!-- 错误页 500-->
    <error-page>
        <error-code>500</error-code>
        <location>/500</location>
    </error-page>

    <!-- 错误页 401-->
    <error-page>
        <exception-type>org.apache.shiro.authz.AuthorizationException</exception-type>
        <location>/401</location>
    </error-page>

    <!-- session 过期时间-->
    <session-config>
      <session-timeout>30</session-timeout>
    </session-config>
    <!-- session 过期时间-->
</web-app>

可以看见这里有引用两个另外的xml文件,applicationContext.xml 和 spring-servlet.xml 

2.2 applicationContext.xml,这个配置文件的作用是spring容器启动的时候,加载项目中所需的相关配置,包括mybatis,数据库,redis以及注解的bean加入到容器中。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!-- 服务层的配置文件 导入进来 在web.xml中context-param 初始化-->
    <!-- common xml -->
    <import resource="classpath:redis/common-redis.xml"/>
    <import resource="classpath:jdbc/common-jdbc.xml"/>
    <import resource="classpath:activemq/common-activemq.xml"/>
    <import resource="classpath:dubbo/common-dubbo.xml"/>
    <!-- service xml -->
    <import resource="classpath:spring-user-service-impl.xml"/>
    <import resource="classpath:spring-log-service-impl.xml"/>
    <!-- activemq service xml -->
    <import resource="classpath:spring-activemq-service.xml"/>
    <!-- dubbo provider xml -->
    <import resource="classpath:spring-user-dubbo-provider.xml"/>
    <import resource="classpath:spring-log-dubbo-provider.xml"/>
    <!-- 服务层的配置文件 导入进来 在web.xml中context-param 初始化-->

    <!-- shiro 安全配置 -->
    <import resource="classpath:applicationContext-shiro.xml"/>
</beans>

2.3 spring-servlet.xml,这个文件的作用是spring mvc层面的,例如请求路径的映射,页面的渲染,以及返回json数据的转化

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
       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.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task.xsd">

    <!-- 注解扫描 -->
    <context:component-scan base-package="com.ctp.ghub"/>

    <!-- 静态资源(js、image等)的访问 -->
    <mvc:default-servlet-handler/>

    <!-- 启用定时任务 -->
    <task:annotation-driven/>

    <!-- 启动对@AspectJ注解的支持 proxy-target-class设置为true表示通知spring使用
    cglib而不是jdk的来生成代理方法,这样AOP可以拦截到Controller -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- 使用fastjson 配置 ,支持转化的请求数据类型为三种 -->
    <!-- mvc:annotation-driven 会自动注册RequestMappingHandlerMapping与
    RequestMappingHandlerAdapter两个Bean,这是Spring MVC为@Controller分发请求所必需的 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json</value>
                        <value>application/xml;charset=UTF-8</value>
                    </list>
                </property>
                <property name="features">
                    <list>
                        <!-- 默认的意思就是不配置这个属性 -->
                        <!-- 是否输出值为null的字段 ,默认是false-->
                        <value>WriteMapNullValue</value>
                        <value>WriteNullNumberAsZero</value>
                        <value>WriteNullListAsEmpty</value>
                        <value>WriteNullStringAsEmpty</value>
                        <value>WriteNullBooleanAsFalse</value>
                        <value>WriteDateUseDateFormat</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 配置页面渲染 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 配置springMVC处理上传文件的信息 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
        <property name="maxUploadSize" value="10485760000"/>
        <property name="maxInMemorySize" value="40960"/>
    </bean>

    <!-- 配置全局异常处理器 -->
    <bean id="exceptionResolver" class="com.ctp.ghub.exception.SpringGlobalExceptionHandler"/>
</beans>

想要源码的,可以访问我的ghub地址:https://github.com/Ellistp/ghub

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值