现在主流的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. nodejs之异步思想

    nodejs的精髓就是"异步",但什么是异步呢?我们来看一个例子: var start =new Date; setTimeout(function(){ var end =new ...

  2. bond配置

    Bonding的模式一共有7种: #defineBOND_MODE_ROUNDROBIN       0   (balance-rr模式)网卡的负载均衡模式 #defineBOND_MODE_ACTI ...

  3. protobuf坑1

    或者说特性吧,没仔细看protobuf代码,也许我还理解不够. 按照陈硕大牛的这篇帖子的介绍,“它包含了程序编译的时候所链接的全部 protobuf Message types” 实际开发过程中发现必 ...

  4. matlab linux 安装

    1. 获取matlab的iso文件,并挂载到目录下2. sudo sh $(matlab_path)/matlab安装(破解,相关见说明)3. 加入系统路径,将$(matlab_install_pat ...

  5. MySql数据源配置

    1.tomcat的config/server.xml中将以下代码写到 </Host>前: <Context docBase="struts1" path=&quo ...

  6. Java 后台获取当前时间

    Calendar c = Calendar.getInstance();//可以对每个时间域单独修改 int year = c.get(Calendar.YEAR); int month = c.ge ...

  7. Unity and C#: Game Loop (Awake, Start, Update)

    Introduction The central component of any game, from a programming standpoint, is the game loop. It ...

  8. 定时自动关闭messagebox

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. Linux内核--网络栈实现分析(九)--传输层之UDP协议(下)

    本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7549340 更多请查看专栏,地 ...

  10. mysql 备份软件 Xtrabackup 的 xtrabackup_binlog_pos_innodb和xtrabackup_binlog_info 文件区别

    今天在操作 innobackupex 的时候,执行 change master to 的时候发现  xtrabackup_binlog_pos_innodb   xtrabackup_binlog_i ...