当前位置: 首页 > news >正文

spring mvc 配置文件加载

Spring中有两种上下文环境-"Application Context和Web Application Context",他们分别对应ContextLoaderListener和ServletDispatcher,且都可以用来配置bean的注入,装配,和AOP。

以下信息引用以下博文

http://simone-folino.blogspot.com/2012/05/dispatcherservlet-vs.html

http://syntx.co/languages-frameworks/difference-between-loading-context-via-dispatcherservlet-and-contextloaderlistener/

http://www.cnblogs.com/zemliu/p/3201112.html

1. ContextLoaderListener

ContextLoaderListener通过读取contextConfigLocation参数来读取配置参数,一般来说它配置的是Spring项目的中间层,服务层组件的注入,装配,AOP.

对应到Spring的自动装配机制<context:component-scan>就是以下几种注解的装配

  • DAO: such as @Repository bean
  • Entity: such as @Entity bean
  • Service: such as @Service bean

(以上内容来自引用博文)

下面是一个典型web.xml中加载ContextLoaderListener的代码片段

复制代码
    <!-- 定义参数 -->
    <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/config/applicationContext.xml</param-value> </context-param> <!-- 定义Listener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
复制代码

以及 applicationContext.xml

复制代码
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd 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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:annotation-config /> <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" /> <context:component-scan base-package="com.simonefolinoblogspot.dao" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> ... </beans>
复制代码

可以看到ContextLoaderListener的配置文件都是中间层,数据层,DAO之类的

2. ServletDispatcher

ServletDispatcher通过读取<servlet-name>-servlet.xml文件来读取配置,如果文件不存在,则可以通过servlet标签内的

        <init-param>
            <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/config/dispatcher-servlet.xml</param-value> </init-param>

来指定配置文件,它配置的是Web层组件的注入,装配和AOP

  • Controllers
  • ViewResolvers
  • LocaleResolvers
  • ThemeResolvers

下面是一个典型配置web.xml

复制代码
<servlet>
 <servlet-name>addressbook</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
复制代码

 

spring-mvc.xml

复制代码
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <context:annotation-config /> <context:component-scan base-package="com.simonefolinoblogspot.controller" > <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/pages/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
复制代码

 

说一个我的例子,我在Conroller类上加上了@Controller自动注册注释,然后把aop的配置引入放到了applicationContext.xml里,这个配置由ContextLoaderListener读取的

    <import resource="classpath*:spring/aop/aopConfig.xml" />

结果AOP方法就不执行了,解决方法就是需要将aopConfig.xml放到ServletDispatcher的配置xml文件里.

而且这理有一个默认的dispacherServlet中引用的配置文件在webapp/WEB-INF下的rest-servlet.xml

转载于:https://www.cnblogs.com/yangfei-beijing/p/7701046.html

相关文章:

  • 用canvas实现一个colorpicker
  • JDK8新特性(2):Stream API常用操作
  • BZOJ 2457 [BeiJing2011] 双端队列
  • 如何用TensorFlow生成令人惊艳的分形图案
  • Hive SQL 练习(这个秒退是怎么回事啊?写了半天 东西都没了,瞬间整个人都凌乱了)...
  • SylixOS之TFTP使用
  • mysql初探
  • 洛谷——P2862 [USACO06JAN]把牛Corral the Cows
  • web开发经验
  • Zookeeper+ActiveMQ 集群实现
  • Android 使用DDMS查看内存使用情况
  • 新品牌如何开展网络营销?
  • 什么是自动化运维 ? 自动化运维的设计思路以及实战
  • 1.3给定两个字符串,请编写程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。...
  • html+css+JavaScript例题
  • 2017 前端面试准备 - 收藏集 - 掘金
  • Bootstrap JS插件Alert源码分析
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • iBatis和MyBatis在使用ResultMap对应关系时的区别
  • JavaScript标准库系列——Math对象和Date对象(二)
  • Java多线程(4):使用线程池执行定时任务
  • leetcode388. Longest Absolute File Path
  • Spring-boot 启动时碰到的错误
  • Spring思维导图,让Spring不再难懂(mvc篇)
  • 翻译--Thinking in React
  • 如何在 Tornado 中实现 Middleware
  • 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP
  • MyCAT水平分库
  • ​【C语言】长篇详解,字符系列篇3-----strstr,strtok,strerror字符串函数的使用【图文详解​】
  • (007)XHTML文档之标题——h1~h6
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • (理论篇)httpmoudle和httphandler一览
  • (论文阅读40-45)图像描述1
  • (每日持续更新)jdk api之FileFilter基础、应用、实战
  • (转)MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
  • (转)Oracle 9i 数据库设计指引全集(1)
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • .NET MVC、 WebAPI、 WebService【ws】、NVVM、WCF、Remoting
  • .NET/C# 编译期间能确定的相同字符串,在运行期间是相同的实例
  • @RequestParam详解
  • []我的函数库
  • [AIGC 大数据基础]hive浅谈
  • [Angular] 笔记 7:模块
  • [C++打怪升级]--学习总目录
  • [C语言]——函数递归
  • [daily][archlinux][game] 几个linux下还不错的游戏
  • [Google Guava] 2.1-不可变集合
  • [hdu 3746] Cyclic Nacklace [kmp]
  • [hive]中的字段的数据类型有哪些
  • [Latex学习笔记]数学公式基本命令
  • [LeetCode 127] - 单词梯(Word Ladder)
  • [LeetCode]—Permutations 求全排列
  • [LeetCode]剑指 Offer 40. 最小的k个数
  • [Linux]如何理解kernel、shell、bash