现在主流的JavaWeb应用几乎都会用到Spring,以下是Spring的配置,以及结合Web的SpringMVC配置的汇总。

jar包的引入

使用maven引入(pom.xml)

    <properties>
<!-- 统一指定版本 -->
<spring.version>4.1.7.RELEASE</spring.version>
</properties>
<!-- 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> <!-- SpringMVC 需要添加如下依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency> <!-- sqlserver 驱动 -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency> <!-- c3p0 连接池支持-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>

与Web项目集成

配置在web.xml文件中

    <!-- Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 指定spring配置文件(classpath*: 包括jar包中配置) -->
<param-value>classpath*:spring/spring-*.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- SpringMVC配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/springmvc.xml</param-value>
</init-param>
<!-- 服务器启动时启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Spring配置文件

Spring配置文件路径:classpath:spring/spring-context.xml

    <!-- 指定扫描的包 -->
<context:component-scan base-package="com.menng.service.impl"/> <!--(根据需要配置)以下是连接池配置,使用c3p0-->
<!-- 引入数据库配置 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.sqlserver.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/> <!-- 连接池初始化连接数 -->
<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
<!-- 连接池中保留的最小连接数。-->
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
<!-- 连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<!-- 连接被断开的时间,Default:0 即永不过期 -->
<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />
<!-- 最大statements数量 -->
<property name="maxStatements" value="${jdbc.maxStatements}" />
<!-- 定义在从数据库获取新连接失败后重复尝试的次数。Default: 30-->
<property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}" />
<!-- 每120秒检查所有连接池中的空闲连接。Default: 0-->
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />
<property name="breakAfterAcquireFailure" value="true" />
</bean>

jdbc配置文件:classpath:jdbc.properties

    jdbc.sqlserver.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver

    jdbc.url=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test
jdbc.user=root
jdbc.password=123456 jdbc.initialPoolSize=20
jdbc.minPoolSize=20
jdbc.maxPoolSize=120
jdbc.maxIdleTime=600
jdbc.acquireIncrement=10
jdbc.maxStatements=20
jdbc.acquireRetryAttempts=3
jdbc.idleConnectionTestPeriod=120

SpringMVC配置文件

SpringMVC配置文件路径:classpath:spring/springmvc.xml

    <!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 指定扫描的包 -->
<context:component-scan base-package="com.menng.controller"/> <!-- (根据需要配置)静态资源处理 -->
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/css/" mapping="/css/**"/> <!-- (根据需要配置)使用容器默认servlet处理没有映射的资源(配置了这个就不用配置 静态资源处理了) -->
<mvc:default-servlet-handler/>

集成Mybatis

  • classpath:spring/spring-context.xml新增如下配置
    <!-- spring与MyBatis整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
</bean> <!-- 配置扫描包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- xml与接口文件在同一目录下,注意:maven打包时把xml mapper文件打包到war包中 -->
<property name="basePackage" value="com.menng.mapper"/>
</bean>
  • pom.xml新增如下依赖以及对xml mapper文件的过滤
    <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency> <!-- myBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency> <!-- myBatis-spring 集成-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency> <build>
<resources>
<!-- 用于mybatis配置文件打包到war包中 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<!-- 是否替换资源中的属性-->
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
  • mybatis的配置(根据需要配置,可选),文件路径为:classpath:mybatis/mybatis-config.xml
    <settings>
<!-- 指定日志为log4j2 -->
<setting name="logImpl" value="LOG4J2"/>
<!-- 查询时,关闭关联对象即时加载以提高性能 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
<setting name="multipleResultSetsEnabled" value="true"/>
<!-- 允许使用列标签代替列名 -->
<setting name="useColumnLabel" value="true"/>
<!-- 不允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->
<setting name="useGeneratedKeys" value="false"/>
<!-- 给予被嵌套的resultMap以字段-属性的映射支持 FULL,PARTIAL -->
<setting name="autoMappingBehavior" value="PARTIAL"/>
</settings>

Spring配置汇总的更多相关文章

  1. Spring配置c3p0数据源时出错报:java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

    今天在使用Spring配置c3p0数据源时,使用的数据库是mysql,服务器是tomcat,运行时报了一个 java.lang.NoClassDefFoundError: com/mchange/v2 ...

  2. spring配置属性的两种方式

    spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...

  3. 解决eclipse spring配置报错:cvc-elt.1: Cannot find the declaration of element

    解决eclipse spring配置报错:cvc-elt.1: Cannot find the declaration of element 'beans'.Referenced file conta ...

  4. spring配置详解

    1.前言 公司老项目的后台,均是基于spring框架搭建,其中还用到了log4j.jar等开源架包.在新项目中,则是spring和hibernate框架均有使用,利用了hibernate框架,来实现持 ...

  5. memcached 学习 1—— memcached+spring配置

    memcached 学习目录: memcached 学习 1—— memcached+spring配置 这几天自己搭建项目环境,解决问题如下: 有关常见的配置这里没有列出,中间遇到的搭建问题比较顺利g ...

  6. 解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException

    解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException这个问题出现的原因:一般在使用annotation的方式注入spring的bean 出现的,具体 ...

  7. spring配置中,properties文件以及xml文件配置问题

    spring方便我们的项目快速搭建,功能强大,自然也会是体系复杂! 这里说下配置文件properties管理的问题. 一些不涉及到代码逻辑,仅仅只是配置数据,可以放在xxxx.properties文件 ...

  8. IntelliJ IDEA通过Spring配置连接MySQL数据库

    先从菜单View→Tool Windows→Database打开数据库工具窗口,如下图所示: 点击Database工具窗口左上角添加按钮"+",选择Import from sour ...

  9. Dubbo中对Spring配置标签扩展

    Spring提供了可扩展Schema的支持,完成一个自定义配置一般需要以下步骤: 设计配置属性和JavaBean 编写XSD文件 编写NamespaceHandler和BeanDefinitionPa ...

随机推荐

  1. 20151012 C# 第一篇 字符与字符串

    20151012 字符与字符串: Char.String等类来表示 字符类Char 1. 字符类Char 表示一个 Unicode 字符,(Unicode字符是计算机通用的字符编码,对不同语言中的每个 ...

  2. Python 5 —— OOP

    OOP class MyClass: y = None def __init__(self,x,y): self.__x = x self.y = y def getx(self): return s ...

  3. Chrome: Resource interpreted as Font but transferred with MIME type font/x-woff

    最近,项目中加入了Bootstrap进行界面优化,但是,项目加载运行之后,控制台总是提示以下错误信息: GET http://localhost:8080/.../fonts/fontawesome- ...

  4. boost的编译

    第1步: 先从官网(www.boost.org)下载最新版的BOOST源码,如图所示 我这里下的是zip的那个第2步:编译源代码(放心.这里是傻瓜式的操作,很容易操作)(1)先把源代码放在E盘,例如 ...

  5. linux ssh config

    Host code.engineering.redhat.com    HostName code.engineering.redhat.com    Port 29418    User jiall ...

  6. [Linux-shell] AWK

    Go to the first, previous, next, last section, table of contents. Printing Output One of the most co ...

  7. 【leetcode】Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. Disabling default console handler in Java Logger by codes

    The open source packages usu. relies on log4j or Java Logger to print logs, by default the console h ...

  9. 分享一个批量导出当前实例下的所有linkedserver脚本

    分享一个批量导出当前实例下的所有linkedserver脚本 很多时候,我们都需要导出实例下面的登录用户,job,linkedserver等等 导出job比较复杂,下午写了一个脚本把所有的linked ...

  10. Javascript原型模式总结梳理

    在大多数面向对象语言中,对象总是由类中实例化而来,类和对象的关系就像模具跟模件一样.Javascript中没有类的概念,就算ES6中引入的class也不过是一种语法糖,本质上还是利用原型实现.在原型编 ...