手机版
你好,游客 登录 注册
背景:
阅读新闻

Spring+SpringMVC企业快速开发架构搭建

创建项目

[日期:2015-09-12] 来源:Linux社区  作者:muruiheng [字体: ]

从2014年12月开始,一直在研究Spring的框架;并且在我们新的开发框架中得到应用;这篇文章记录了这段时间的成长记录,仅此先给共同成长的小白。

为了简单,我们选择使用maven来构建项目,通过maven可以控制引入jar的版本,大大简化了jar包的管理工作,废话不多说,创建一个项目

一、创建一个新项目

1、创建maven项目

2、选择 maven-archetype-webapp

3、填写groupId、artifactId、package

项目目录:

二、项目创建完成后修改默认的jdk、编译器等默认属性,我的项目选择的是jdk7

3、 引入Spring的jar

Xml代码

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <spring.version>3.2.12.RELEASE</spring.version>
  <tddl.version>3.0.1.5.taobaocode-SNAPSHOT</tddl.version>
  <druid.version>0.2.3</druid.version>
  <cglib.version>2.1_3</cglib.version>
  <aspectj.version>1.6.6</aspectj.version>
  <jms.version>2.0</jms.version>
  <jackson.version>1.9.5</jackson.version>
  <poi.version>3.10.1</poi.version>
  <mail.version>1.4.3</mail.version>
  <atomikos.version>3.9.3</atomikos.version>
  <mysql.version>5.1.34</mysql.version>
  <redis.version>2.1.0</redis.version>
  <jta.version>1.1</jta.version>
  <activemq.version>5.10.0</activemq.version>
  <servlet-api.version>5.5.23</servlet-api.version>
  <fileupload.version>1.3.1</fileupload.version>
  <fastjson.version>1.1.15</fastjson.version>
  <json-lib.version>2.4</json-lib.version>
  <jaxb-api.version>2.2.11</jaxb-api.version>
  <jaxb-impl.version>2.1</jaxb-impl.version>
  <woodstox.version>4.1.5</woodstox.version>
  <activation.version>1.1.1</activation.version>
  <testng.version>6.8.13</testng.version>
  <mockito.version>1.10.18</mockito.version>
  <unitils.version>3.4.2</unitils.version>
  <Java.version>1.7</java.version>
  <jodd.version>3.3.8</jodd.version>
  <jstl.version>1.2</jstl.version>
  <file.encoding>utf-8</file.encoding>
 </properties>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.11</version>
   <scope>test</scope>
  </dependency>
  <!-- JSTL -->
  <dependency>
   <groupId>jstl</groupId>
   <artifactId>jstl</artifactId>
   <version>${jstl.version}</version>
  </dependency>
 
  <!-- Spring -->
  <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>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-orm</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jms</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>${aspectj.version}</version>
  </dependency>
  <dependency>
   <groupId>javax.jms</groupId>
   <artifactId>javax.jms-api</artifactId>
   <version>${jms.version}</version>
  </dependency>
  <dependency>
   <groupId>javax.mail</groupId>
   <artifactId>mailapi</artifactId>
   <version>${mail.version}</version>
  </dependency>

  <!-- json -->
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>${fastjson.version}</version>
  </dependency>
  <dependency>
   <groupId>net.sf.json-lib</groupId>
   <artifactId>json-lib</artifactId>
   <version>${json-lib.version}</version>
   <classifier>jdk15</classifier>
  </dependency>

  <!-- MQ-->
  <dependency>
   <groupId>org.apache.activemq</groupId>
   <artifactId>activemq-all</artifactId>
   <version>${activemq.version}</version>
  </dependency>
  <dependency>
   <groupId>tomcat</groupId>
   <artifactId>servlet-api</artifactId>
   <version>${servlet-api.version}</version>
  </dependency>

  <!-- 分布式事务处理 -->
  <dependency>
   <groupId>com.atomikos</groupId>
   <artifactId>transactions-jta</artifactId>
   <version>${atomikos.version}</version>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>com.atomikos</groupId>
   <artifactId>transactions-jdbc</artifactId>
   <version>${atomikos.version}</version>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>javax.transaction</groupId>
   <artifactId>jta</artifactId>
   <version>${jta.version}</version>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.9</version>
   <optional>true</optional>
  </dependency>

  <!-- poi -->
  <dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>${poi.version}</version>
  </dependency>
  <dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>${poi.version}</version>
  </dependency>

  <!-- jackson -->
  <dependency>
   <groupId>org.codehaus.jackson</groupId>
   <artifactId>jackson-mapper-asl</artifactId>
   <version>${jackson.version}</version>
  </dependency>
      <dependency> 
          <groupId>cglib</groupId>
          <artifactId>cglib-nodep</artifactId>
          <version>${cglib.version}</version>
      </dependency>
 <dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>${fileupload.version}</version>
 </dependency>   
 <dependency>
  <groupId>org.jodd</groupId>
  <artifactId>jodd</artifactId>
  <version>${jodd.version}</version>
 </dependency>
</dependencies>

四、 创建Spring的配置文件

1、 启动Spring需要在web.xml中配置监听,使用SpringMVC需要配置Spring的servlet

Web.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
 <display-name>Archetype Created Web Application</display-name>
 <error-page>
  <exception-type>java.lang.Throwable</exception-type>
  <location>/500.jsp</location>
 </error-page>
 <error-page>
  <error-code>500</error-code>
  <location>/500.jsp</location>
 </error-page>
 <error-page>
  <error-code>404</error-code>
  <location>/404.jsp</location>
 </error-page>
 <!-- 装载spring 父上下文 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext*.xml</param-value>
 </context-param>
  <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
 <!-- 支持MVC 装载子上下文 -->
 <servlet>
  <servlet-name>springMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <description>Spring MVC Configuration Location</description>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    classpath:spring-*.xml
   </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 <filter>
  <filter-name>encode</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>ssojcFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

2、Spring上下文配置文件ApplicationContext.xml用于配置Spring的通用配置

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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd ">

 <context:property-placeholder location="classpath:*.properties" />
 
 <!--dataSource-->
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  <!-- 基本属性 url、user、password -->
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.userName}" />
  <property name="password" value="${jdbc.passWord}" />
  <!-- 配置初始化大小、最小、最大 -->
  <property name="initialSize" value="${jdbc.initialSize}" />
  <property name="minIdle" value="${jdbc.minIdle}" />
  <property name="maxIdle" value="maxIdle" />
  <property name="maxActive" value="${jdbc.maxActive}" />
  <!-- 配置获取连接等待超时的时间 -->
  <property name="maxWait" value="60000" />
  <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
  <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />
  <property name="validationQuery" value="${jdbc.validationQuery}" />
  <property name="testWhileIdle" value="${jdbc.testWhileIdle}" />
  <property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
  <property name="testOnReturn" value="${jdbc.testOnReturn}" />
  <property name="filters" value="${jdbc.filters}" />
 </bean>
 <!--daoSupport-->
 <bean id="jldaoSupport" class="com.jl.net.framework.dao.support.JLDeDaoSupport">
  <property name="dataSource" ref="dataSource"/>
 </bean>
 
    <bean id = "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource" />
    </bean>
 
 <!-- 声明式事务配置 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
   <tx:method name="begin*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
   <tx:method name="end*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
   <tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
   <tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
   <tx:method name="do*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
   <tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
   <tx:method name="modify*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
   <tx:method name="query*" propagation="NOT_SUPPORTED"/>
   <tx:method name="get*" propagation="NOT_SUPPORTED"/>
   <tx:method name="load*" propagation="NOT_SUPPORTED"/>
   <tx:method name="find*" propagation="NOT_SUPPORTED"/>
   <tx:method name="*" read-only="true" propagation="SUPPORTS" />
  </tx:attributes>
 </tx:advice>
 <!-- 内部服务 -->
 <aop:config>
  <aop:pointcut expression="execution(* com.jl.net..service.*.*(..))" id="pointCut" />
  <aop:advisor  pointcut-ref="pointCut" advice-ref="txAdvice"/>
 </aop:config>
 <!-- 对外服务 -->
 <aop:config>
  <aop:pointcut expression="execution(* com.jl.net..service.soa.*.*(..))" id="pointCut_soa" />
  <aop:advisor  pointcut-ref="pointCut_soa" advice-ref="txAdvice"/>
 </aop:config>
 <!-- JMS -->
 <aop:config>
  <aop:pointcut expression="execution(* com.jl.net..service.jms.receiver.*.*(..))" id="pointCut_jms" />
  <aop:advisor  pointcut-ref="pointCut_jms" advice-ref="txAdvice"/>
 </aop:config>

 <!-- 定义过滤器 -->
 <bean id="encode" class="com.jl.net.framework.web.filter.JLEncoderFilter">
  <property name="encode" value="UTF-8" />
 </bean>
</beans>

3、SpringMVC配置

Springmmc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 
 <context:component-scan base-package="com.jl.net.*.web.controller"/>
 <!-- mvc -->
 <context:annotation-config />
 <!-- 使用ConfigurableWebBindingInitializer注册conversionService -->
 <bean id="conversionService" class="com.jl.net.framework.converter.suport.JLConversionService" init-method="init">
        <property name="converters">
            <set>
                <bean class="com.jl.net.framework.converter.JLStringToTimestampConverter">
                 <property name="formarts">
                  <props>
                   <prop key="^\\d{4}-\\d{1,2}$">yyyy-MM</prop>
                   <prop key="^\\d{4}-\\d{1,2}-\\d{1,2}$">yyyy-MM-dd</prop>
                   <prop key="^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$">yyyy-MM-dd hh-mm</prop>
                   <prop key="^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$">yyyy-MM-dd hh-mm-ss</prop>
                  </props>
                 </property>
                </bean>
            </set>
        </property>
    </bean>
    <bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> 
        <property name="conversionService" ref="conversionService"/> 
    </bean>
 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
  <property name="webBindingInitializer" ref="webBindingInitializer"/> 
 </bean>
   
 <!-- 根据后缀 支持多视图处理器 -->
  <!-- 根据客户端的不同的请求决定不同的view进行响应, 如 /blog/1.json /blog/1.xml -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <!-- 设置为true以忽略对Accept Header的支持 -->
  <property name="ignoreAcceptHeader" value="true" />
  <!-- 在没有扩展名时即: "/blog/1" 时的默认展现形式 -->
  <property name="defaultContentType" value="application/json" />

  <!-- 扩展名至mimeType的映射,即 /blog.json => application/json -->
  <property name="mediaTypes">
   <map>
    <entry key="html" value="text/html" />
    <entry key="jsp" value="text/html" />
    <entry key="pdf" value="application/pdf" />
    <entry key="xsl" value="application/vnd.ms-excel" />
    <entry key="xslx" value="application/vnd.ms-excel" />
    <entry key="xml" value="application/xml" />
    <entry key="json" value="application/json" />
   </map>
  </property>

  <!-- 用于开启 /blog/123?format=json 的支持 -->
  <property name="favorParameter" value="true" />
  <!-- 视图解析器 -->
  <property name="viewResolvers">
   <list>
    <!-- jsp解析 -->
    <ref bean="jspRseolver"/>
    <ref bean="beanRseolver"/>
   </list>
  </property>
  <property name="defaultViews">
   <list>
    <!-- for application/json -->
    <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
   </list>
  </property>
 </bean>
 
 <!-- 声明视图解析的对象 -->
 <bean id="beanRseolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" />
 <bean id="multipartRseolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <property name="maxUploadSize" value="5000000" />
 </bean>
 <bean id="jspRseolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp"></property>
 </bean>
 
 
 <!-- 转向异常处理页面 -->
 <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
        <!-- 定义默认的异常处理页面,当该异常类型的注册时使用 --> 
        <property name="defaultErrorView" value="error/error" /> 
        <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception --> 
        <property name="exceptionAttribute" value="ex" /> 
        <!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常也页名作为值 --> 
        <property name="exceptionMappings"> 
            <props>
              <!-- 这里还可以继续扩展对不同异常类型的处理 -->
                <prop key="com.jl.net.framework.exception.ValidateException">/error/error</prop> 
                <prop key="com.jl.net.framework.exception.SessionException">/error/login</prop> 
            </props> 
        </property> 
    </bean> 
 
 

 <!-- 输出对象转String支持 -->
 <bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
  <property name="supportedMediaTypes">
   <list>
    <value>text/plain;charset=UTF-8</value>
   </list>
  </property>
 </bean>
 <!-- 输出对象转JSON支持 -->
 <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
 <bean id="byteArrayConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
 <bean id="resourceConverter" class="org.springframework.http.converter.ResourceHttpMessageConverter" />
 <bean id="sourceConverter" class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
 <bean id="xmlAwareFormConverter" class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
 <bean id="jaxb2RootElementConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
 
 
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  <property name="messageConverters">
   <list>
    <ref bean="stringConverter" />
    <ref bean="jsonConverter" />
    <ref bean="byteArrayConverter" />
    <ref bean="resourceConverter" />
    <ref bean="sourceConverter" />
    <ref bean="xmlAwareFormConverter" />
    <ref bean="jaxb2RootElementConverter" />
   </list>
  </property>
 </bean>
 <!-- URL映射管理器 -->
 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
   <property name="order" value="0"/> 
        <property name="useDefaultSuffixPattern" value="true"/>
        <!-- 拦截器 -->
  <property name="interceptors">
   <list>
    <!--会话管理的拦截器 -->
    <ref bean="userInfoInterceptor"/>
   </list>
  </property>
 </bean>
 <bean id="userInfoInterceptor" class="com.jl.net.framework.web.interceptor.UserInfoInterceptor" />
 <!--
 <mvc:interceptors>
  <mvc:interceptor>
   <mvc:mapping path="/*"/>
   <ref bean="securityInterceptor"/>
  </mvc:interceptor>
  <mvc:interceptor>
   <mvc:mapping path="/*"/>
   <ref bean="userInfoInterceptor"/>
  </mvc:interceptor>
 </mvc:interceptors>
  -->
</beans>

完成以上操作一个完整的SpringMVC项目就创建完成了!

更多详情见请继续阅读下一页的精彩内容http://www.linuxidc.com/Linux/2015-09/122942p2.htm

--------------------------------------分割线 -------------------------------------- 

Spring中如何配置Hibernate事务 http://www.linuxidc.com/Linux/2013-12/93681.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

基于 Spring 设计并实现 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm

Spring-3.2.4 + Quartz-2.2.0集成实例 http://www.linuxidc.com/Linux/2013-10/91524.htm

使用 Spring 进行单元测试 http://www.linuxidc.com/Linux/2013-09/89913.htm

运用Spring注解实现Netty服务器端UDP应用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm

Spring 3.x 企业应用开发实战 PDF完整高清扫描版+源代码 http://www.linuxidc.com/Linux/2013-10/91357.htm

linux
【内容导航】
第1页:创建项目 第2页:Web端封装
相关资讯       Spring  SpringMVC搭建 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款