Spring配置汇总
现在主流的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配置汇总的更多相关文章
- Spring配置c3p0数据源时出错报:java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector
今天在使用Spring配置c3p0数据源时,使用的数据库是mysql,服务器是tomcat,运行时报了一个 java.lang.NoClassDefFoundError: com/mchange/v2 ...
- spring配置属性的两种方式
spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...
- 解决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 ...
- spring配置详解
1.前言 公司老项目的后台,均是基于spring框架搭建,其中还用到了log4j.jar等开源架包.在新项目中,则是spring和hibernate框架均有使用,利用了hibernate框架,来实现持 ...
- memcached 学习 1—— memcached+spring配置
memcached 学习目录: memcached 学习 1—— memcached+spring配置 这几天自己搭建项目环境,解决问题如下: 有关常见的配置这里没有列出,中间遇到的搭建问题比较顺利g ...
- 解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException
解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException这个问题出现的原因:一般在使用annotation的方式注入spring的bean 出现的,具体 ...
- spring配置中,properties文件以及xml文件配置问题
spring方便我们的项目快速搭建,功能强大,自然也会是体系复杂! 这里说下配置文件properties管理的问题. 一些不涉及到代码逻辑,仅仅只是配置数据,可以放在xxxx.properties文件 ...
- IntelliJ IDEA通过Spring配置连接MySQL数据库
先从菜单View→Tool Windows→Database打开数据库工具窗口,如下图所示: 点击Database工具窗口左上角添加按钮"+",选择Import from sour ...
- Dubbo中对Spring配置标签扩展
Spring提供了可扩展Schema的支持,完成一个自定义配置一般需要以下步骤: 设计配置属性和JavaBean 编写XSD文件 编写NamespaceHandler和BeanDefinitionPa ...
随机推荐
- Android volley 当用fiddler2 抓包时隔一段时间不操作,会出现 http 408错误
出现原因是由于fiddler2导致,关闭fiddler2即可...
- MAT-Java内存分析工具
对Mat工具的详细介绍,引用博文:http://my.oschina.net/biezhi/blog/286223 下载地址:http://www.eclipse.org/mat/downloads. ...
- Sublime+Golang Plugin
很喜欢在Sublime开发Golang程序,主要是要一个Sublime Golang Plugin, 可以给代码autocomplement.相当的棒! 1.安装Sublime https://www ...
- Xenu Link Sleuth-简单使用
1.工具说明 xenu link sleuth,主要用于测试网站死链接.包括图片.链接. 下载地址:http://home.snafu.de/tilman/xenulink.html#Download ...
- 简单修改cramfs
首先进入root用户,确保LINUX系统下装有cramfsprogs,没有的话get-apt install cramfsprogs, 找到.cramfs文件,输入命令cramfsck -x song ...
- 基于Axure的快速原型方法
Axure是一个专业的快速原型设计工具,让负责定义需求和规格.设计功能和界面的专家能够快速创建应用软件或Web网站的线框图.流程图.原型和规格说明文档.作为专业的原型设计工具,它能快速.高效的创建原型 ...
- arduino 入手
新买了个ardhuino 入门套件 1. Win7 Driver issue: Need to install the below drivers, because my windows is ins ...
- PHP, LDAPS and Apache
要PHP可以连接到用self-signed certificate的ldaps服务器,需要在/etc/ldap.conf中添加一行: TLS_REQCERT never 要PHP在Apache ...
- Android--短信
1.Android 提供一系列 API,可以是我们在自己的程序中发送和接收短信: 2.接收短信: 1)当手机接收到一条短信时,系统会发出一条值为 android.provider.Telephony. ...
- 道路翻新 (Revamping Trails, USACO 2009 Feb)
题意:给定m<=50000的1-n有联通的图,求最多可以使K<=20条边变为0的情况下的最短路是多少.. 思路:简单的分层图最短路,对于每个点拆成K个点.. 然后求一边最短路.. code ...