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 ...
随机推荐
- Log4j 与 Logback的ConversionPattern对比
为了能将log4j的配置无缝转到logback,需要了解其中ConversionPattern的差异,以下是对比表格,内容来自: log4j官网 logback官网 其中可能需要转换的地方主要有两块: ...
- 【原创】JAVA并发编程——Callable和Future源码初探
JAVA多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多线程.其中前两种方式线程执行完后都没 ...
- getIdentifier()获取资源Id
工作需要使用getIdentifier()方法可以方便的获各应用包下的指定资源ID.主要有两种方法:(1)方式一Resources resources = context.getResources() ...
- LintCode 111 Climbing Stairs
这道题参考了这个网址: http://blog.csdn.net/u012490475/article/details/48845683 /* 首先考虑边界情况,当有1层时,有一种方法. 然后再看2层 ...
- spring MVC原理
spring MVC原理 Spring MVC工作流程图 图一 图二 Spring工作流程描述 1. 用户向服务器发送请求,请求被Spring 前端控制Servelt D ...
- 【leetcode】Pow(x,n)
马上各种校招要开始了,怎么也得准备一下,之前一直在看看机器学习,NLP方面的东西,收获很多.最近换换脑子,回过头来做做leetcode,感觉还是蛮有意思的.今天刷了个水题,AC不高,然而难度也不高.. ...
- linux-9基本命令之-查看系统负载 uptime
uptime 查看系统负载 uptime @1.查看每一秒的刷新一次系统负载运行情况 [root@localhost /]# watch -n uptime 系统当前时间 系统已运行时间 ...
- 跟visual studio 集成的git插件
目前有三个,git extension,微软的 visual studio tools for git extension,还有git source control provider 经测试,最好用的 ...
- Magicodes.Shop——版本历史
Magicodes.Shop为湖南心莱信息科技有限公司(xin-lai.com)Magicodes系列产品之一. 产品中引用的Magicodes系列Nuget包的开源库地址为:https://gith ...
- Lucene 查询工具 LQT
Lucene Query Tool (lqt) 是一个命令行工具用来执行 Lucene 查询并对结果进行格式化输出. 使用方法: 01 $ ./lqt 02 usage: LuceneQueryToo ...