springmvc.xml和applicationContext.xml配置的特点
1:springmvc.xml配置要点
一般它主要配置Controller的组件扫描器和视图解析器
下为:springmvc.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd"> <!-- 使用注解开发,不用配置controller,需要配置一个组件扫描器 -->
<context:component-scan base-package="com.edu.test.controller"/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置从项目根目录到指定目录一端路径 ,建议指定浅一点的目录-->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 文件的后缀名 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

2:applicationContext.xml配置要点(在web.xml文件需要加<listener>)
下为:applicationContext.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 配置组件扫描器,使用注解方式开发,不用配置dao和service -->
<!-- 在springmvc.xml文件中也可以配置这个属性 -->
<context:component-scan base-package="com.edu.test"/> <!-- 数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="" />
</bean> <!-- 配置session工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean> <!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 配置AOP通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事务属性 -->
<tx:attributes>
<!-- 添加事务管理的方法 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="select*" read-only="true"/>
</tx:attributes>
</tx:advice> <!-- 配置AOP,为添加事务管理的操作配置AOP -->
<aop:config>
<!-- 引入的Spring定义的事务通知,需要使用aop:advisor -->
<!-- 下面难 -->
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.edu.test.service.*.*(..))"
/>
</aop:config>
</beans>

3:在web.xml文件中,将springmvc.xml和applicationContext.xml一起引入
下为:web.xm文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 中央控制器 -->
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- 配置Spring提供的字符编码过滤器 -->
<filter>
<filter-name>SpringCharacterEncodingFilter</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>SpringCharacterEncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>

springmvc.xml和applicationContext.xml配置的特点的更多相关文章
- 浅谈配置文件:spring-servlet.xml(spring-mvc.xml) 与 applicationContext.xml
在搭建 spring mvc 的框架时,会有2个配置文件必不可少: spring-servlet.xml 和applicationContext.xml.第一次接触spring mvc的工程师可能会对 ...
- spring-mvc.xml 和 application-context.xml的配置与深入理解
在java框架这个话题,前几篇文章是基于搭建ssm项目框架,以及web.xml的配置讲解,本篇主要就ssm框架的其他配置文件进行深入讲解,他们分别是:1.application-context.xml ...
- spring或springmvc自动生成applicationcontext.xml或springmvc文件(此文转载和借鉴多篇文章)
在用spring或者springmvc框架进行开发时,编辑applicationcontext.xml等配置文件是必不可少的,在eclipse中打开applicationcontext.xml通常是这 ...
- spring-mvc.xml 和 application-context.xml的区别
转自:https://www.cnblogs.com/binlin1987/p/7053016.html application-context.xml是全局的,应用于多个serverlet,配合li ...
- xxx-servlet.xml vs applicationContext.xml
Spring lets you define multiple contexts in a parent-child hierarchy. The applicationContext.xml def ...
- 在web.xml注册applicationContext.xml配置文件
概要: Spring配置文件是集成了Spring框架的项目的核心,引擎的开始是:容器先是加载web.xml,接着是applicationContext.xml在web.xml里的注册.以下我们将介绍a ...
- spring +springmvc+mybatis组合applicationContext.xml文件配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- SpringMVC 常用applicationContext.xml、web.xml、servlet-mvc.xml简单配置
在进行学习配置文件之前,为了加深对框架的认识,简单的做了SSM框架的简单实验.然后画出listAll查询方法的整个过程的思维导图. 整个过程中的web.xml.SpringMVC.xml.applic ...
- 【转载】Spring中的applicationContext.xml与SpringMVC的xxx-servlet.xml的区别
一直搞不明白两者的区别. 如果使用了SpringMVC,事实上,bean的配置完全可以在xxx-servlet.xml中进行配置.为什么需要applicationContext.xml?一定必须? 一 ...
随机推荐
- 如何自定义JSTL标签与SpringMVC 标签的属性中套JSTL标签报错的解决方法
如何自定义JSTL标签 1.创建一个类,从SimpleTagSupport继承 A) 通过继承可以获得当前JSP页面上的对象,如JspContext I) 实际上可以强转为PageContext II ...
- 2017 Multi-University Training Contest - Team 2——HDU6050 Funny Function
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6050 题意:题目很短自己看吧, 就是这个递推式子,说的很清楚了,让你求F(m,1).题解里面分什么奇偶 ...
- w[wi].getAttribute('wattr')
w 将数据库的不同数值寄存到自定义的不同属性上,避免通过寄存到同一id属性上的字符串处理.
- Linux常用软件(以及特殊命令)清单(ubuntu)
LibreOffice 解压缩命令 unar .tar 创建新文档命令:touch.vi/vim 浏览器:google chrome.firefox
- post 传递参数中包含 html 代码解决办法,js加密,.net解密
今天遇到一个问题,就是用post方式传递参数,程序在vs中完美调试,但是在iis中,就无法运行了,显示传递的参数获取不到,报错了,查看浏览器请求情况,错误500,服务器内部错误,当时第一想法是接收方式 ...
- LeetCode之Symmetric Tree
</pre>Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ce ...
- Visual Studio 2017 新特性
1. out-variables(Out变量) 以前,我们使用out变量的时候,需要在外部先申明,然后才能传入方法,类似如下: string ddd = ""; //先申明变量 c ...
- Redis for Python开发手册
redis基本命令 String Set set(name, value, ex=None, px=None, nx=False, xx=False) 在Redis中设置值,默认,不存在则创建,存在则 ...
- 跨域问题session不一致问题的解决(前端和后台在不同的服务器)
场景:首先调用发送短信接口,然后调用注册接口,结果后台的session不一致获取不到之前验证码. 实质:每次ajax跨域访问都是新的session ,每次的sessionID都不一样 解决: pack ...
- git mv与直接mv的区别
git mv 行为: 1.创建一个和之前文件内容一样的文件,文件名为新的文件名 2.将原来的文件删除 3.将删除的文件添加到暂存区 4.将新建的文件添加到暂存区 $ git mv a a1 $ git ...