1. Web.xml

<!--配置页面控制器-->

<servlet>

<servlet-name>spring</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/spring-servlet.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:config/applicationContext.xml</param-value>

</context-param>

<!-- 设置字符编码方式-->

<filter>

<filter-name>setcharacter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>utf-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>setcharacter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

备注:

关于ContextLoaderListener

部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。

采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。

由此可见applicationContext.xml的文件位置就可以有两种默认实现:

第一种:直接将之放到/WEB-INF下,之在web.xml中声明一个listener

第二种:将之放到classpath下,但是此时要在web.xml中加入<context-param>,用它来指明你的

  1. spring - servlet.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:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config />

<!-- 把标记了@Controller注解的类转换为bean -->

<context:component-scan base-package="com.mvc.controller" />

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"

p:prefix="/WEB-INF/view/"

p:suffix=".jsp" />

<bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver"

p:defaultEncoding="utf-8" />

</beans>

  1. ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:annotation-config />

<context:component-scan base-package="com.mvc" />  <!-- 自动扫描所有注解该路径 -->

<context:property-placeholder location="classpath:/hibernate.properties" />

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">${dataSource.dialect}</prop>

<prop key="hibernate.hbm2ddl.auto">${dataSource.hbm2ddl.auto}</prop>

<prop key="hibernate.hbm2ddl.auto">update</prop>

</props>

</property>

<property name="packagesToScan">

<list>

<value>com.mvc.entity</value><!-- 扫描实体类,也就是平时所说的model -->

</list>

</property>

</bean>

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

<property name="dataSource" ref="dataSource" />

</bean>

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="${dataSource.driverClassName}" />

<property name="url" value="${dataSource.url}" />

<property name="username" value="${dataSource.username}" />

<property name="password" value="${dataSource.password}" />

</bean>

<!-- Dao的实现 -->

<bean id="entityDao" class="com.mvc.dao.EntityDaoImpl">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<tx:annotation-driven mode="aspectj"/>

<aop:aspectj-autoproxy/>

</beans>

<!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->

<aop:config proxy-target-class="true">

<aop:pointcut id="serviceMethod"

expression=" execution(* com.service..*(..))" />

<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />

</aop:config>

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="*" />

</tx:attributes>

</tx:advice>

<!-- 配置Jdbc模板 -->

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

<property name="dataSource" ref="dataSource"></property>

</bean>

<!-- 配置数据源 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<property name="driverClass">

<value>${jdbc.driverClassName}</value>

</property>

<property name="jdbcUrl">

<value>${jdbc.url}</value>

</property>

<property name="user">

<value>${jdbc.username}</value>

</property>

<property name="password">

<value>${jdbc.password}</value>

</property>

<!--连接池中保留的最小连接数。 -->

<property name="minPoolSize">

<value>5</value>

</property>

<!--连接池中保留的最大连接数。Default: 15 -->

<property name="maxPoolSize">

<value>30</value>

</property>

<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->

<property name="initialPoolSize">

<value>10</value>

</property>

<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->

<property name="maxIdleTime">

<value>60</value>

</property>

<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->

<property name="acquireIncrement">

<value>5</value>

</property>

<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。

如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->

<property name="maxStatements">

<value>0</value>

</property>

<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->

<property name="idleConnectionTestPeriod">

<value>60</value>

</property>

<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->

<property name="acquireRetryAttempts">

<value>30</value>

</property>

<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试

获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->

<property name="breakAfterAcquireFailure">

<value>true</value>

</property>

<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable

等方法来提升连接测试的性能。Default: false -->

<property name="testConnectionOnCheckout">

<value>false</value>

</property>

</bean>

[备注2] hibernate.properties数据库连接配置

dataSource.password=123

dataSource.username=root

dataSource.databaseName=test

dataSource.driverClassName=com.mysql.jdbc.Driver

dataSource.dialect=org.hibernate.dialect.MySQL5Dialect

dataSource.serverName=localhost:3306

dataSource.url=jdbc:mysql://localhost:3306/test

dataSource.properties=user=dataSource.username;databaseName=dataSource.username;databaseName={dataSource.databaseName};serverName=dataSource.serverName;password=dataSource.serverName;password={dataSource.password}

dataSource.hbm2ddl.auto=update

关于spring mvc的配置文件的更多相关文章

  1. spring mvc改动配置文件路径

    1.1.  Classpath project文件夹 在web.xml文件例如以下配置: <!-- 配置spring mvc 的核心servlet --> <servlet> ...

  2. maven 项目 spring mvc + jdbc 配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. Spring MVC的配置文件(XML)的几个经典案列

    1.既然是配置文件版的,那配置文件自然是必不可少,且应该会很复杂,那我们就以一个一个的来慢慢分析这些个经典案列吧! 01.实现Controller /* * 控制器 */ public class M ...

  4. 玩转Spring MVC(三)----spring基本配置文件

    这篇文章总结一下spring mvc的基本配置,首先贴一张我的项目的目录截图,有一些多余的文件,大家不必在意: 用到的一些jar包在这:<a>http://download.csdn.ne ...

  5. Spring MVC篇一、搭建Spring MVC框架

    本项目旨在搭建一个简单的Spring MVC框架,了解Spring MVC的基础配置等内容. 一.项目结构 本项目使用idea intellij创建,配合maven管理.整体的目录结构如图: 其中ja ...

  6. Spring MVC拦截器+注解方式实现防止表单重复提交

    原理:在新建页面中Session保存token随机码,当保存时验证,通过后删除,当再次点击保存时由于服务器端的Session中已经不存在了,所有无法验证通过. 注,如果是集群的方式,则需要将token ...

  7. spring 整合 spring mvc

    需要进行 Spring 整合 SpringMVC 吗 ? 还是否需要再加入 Spring 的 IOC 容器 ? 是否需要再 web.xml 文件中配置启动 Spring IOC 容器的 Context ...

  8. [Spring MVC] - JSON

    Spring MVC中使用JSON,先必需引用两个包:jackson-core-asl-1.9.13.jar.jackson-mapper-asl-1.9.13.jar 因为需要使用到jquery测试 ...

  9. Spring MVC 学习总结(三)——请求处理方法Action详解

    Spring MVC中每个控制器中可以定义多个请求处理方法,我们把这种请求处理方法简称为Action,每个请求处理方法可以有多个不同的参数,以及一个多种类型的返回结果. 一.Action参数类型 如果 ...

随机推荐

  1. 深入理解JVM - 虚拟机类加载机制 - 第七章

    类加载的时机类从被加载到虚拟机内存开始,到卸载出内存为止,它的整个生命周期包括了:加载/验证/准备/解析/初始化/使用/卸载七个阶段.其中验证/准备和解析统称为连接(Linking). 加载.验证.准 ...

  2. Cocos2d-x中锚点的介绍

    什么是锚点?  只需要记住一句话就可以,锚点就是你指定的那个坐标究竟是图像的哪个点,也就是你setPosition的坐标 eg: 新建工程:在HelloWorld中写上如下代码: CCSprite * ...

  3. 单元测试:TESTNG和powermock的使用

    pom文件: <properties>        <testng.version>6.8</testng.version>        <powermo ...

  4. L93

    Three Days to See(Excerpts) All of us have read thrilling stories in which the hero had only a limit ...

  5. homebrew cask安装launch rocket【转】

    简介 brew cask是一个用命令行管理Mac下应用的工具,它是基于homebrew的一个增强工具. homebrew可以管理Mac下的命令行工具,例如imagemagick, nodejs,如下所 ...

  6. 每天一个Linux命令:目录

    版权声明 更新:2017-04-19博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 在使用Linux的过程中总是发现有一些L ...

  7. bzoj 1369: Gem 树形dp

    题目大意 给出一棵树,要求你为树上的结点标上权值,权值可以是任意的正整数 唯一的限制条件是相临的两个结点不能标上相同的权值,要求一种方案,使得整棵树的总价值最小.N<=10000 题解 我们可以 ...

  8. python爬虫知识点总结(九)Requests+正则表达式爬取猫眼电影

    一.爬取流程 二.代码演示 #-*- coding: UTF-8 -*- #_author:AlexCthon #mail:alexcthon@163.com #date:2018/8/3 impor ...

  9. HTML5设计原理

    HTML5是Web标准的巨大飞跃,它为什么要包含那些东西,它背后的设计原则是什么? <JavaScript DOM编程艺术>和<HTML5 For Web Designer>作 ...

  10. GridView中数据行的操作

    一个是直接动态绑定gridview 用3楼的办法就可以了 int j=1;//j的数值表示你要取的那一列的索引,要取第二列,j就设为1for (int i = 0; i < this.GridV ...