美文网首页
springmvc配置文件

springmvc配置文件

作者: 小白虫虫 | 来源:发表于2017-09-25 16:01 被阅读0次

语义结构:

schema-based xml

这也就是后来Schema-based XML开始被引入并流行开来的重要原因。从形式上看,Schema-based XML相比较Traditional XML至少有三个方面的优势:

namespace—— 拥有很明确的逻辑分类

element—— 拥有很明确的过程语义

attributes—— 拥有很简明的配置选项

由此可见,Schema-based XML中的配置节点拥有比较鲜明的功能特性,通过namespace、element和attributes这三大元素之间的配合,共同完成对一个动态过程的描述。


Mybatis 的配置文件

设置 mybatis 配置文件: Configure.xml, 在 src/config 目录下建立此文件,内容如下:

mybatis的configure.xml文件

同时建立这个 User 类对应的映射文件 User.xml,详细如下代码所示:

某一个mapping片段

下面是对这几个配置文件一点解释说明:

1、配置文件 Configure.xml 是 mybatis 用来建立 sessionFactory,里面主要包含了数据库连接相关内容,还有 java 类所对应的别名,比如: 这个别名非常重要,在具体的类的映射中,比如:User.xml 中 resultType 就是对应这个。要保持一致,这里的 resultType 还有另外单独的定义方式,后面学习到我们再详细介绍说明。

2、Configure.xml 里面 的是包含要映射的类的 xml 配置文件。

3、在User.xml 文件里面主要是定义各种 SQL 语句,以及这些语句的参数,以及要返回的类型等等。


applicationContext.xml和dispatcher-servlet.xml的区别

原文:

Spring lets you define multiple contexts in a parent-child hierarchy.

The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.

The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2).

Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.

All Spring MVC controllers must go in the spring-servlet.xml context.

In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet, then there's not really much point, unless you have a specific use for it.

可见,applicationContext.xml 和 dispatch-servlet.xml形成了两个父子关系的上下文。

1) 一个bean如果在两个文件中都被定义了(比如两个文件中都定义了component scan扫描相同的package), spring会在application context和 servlet context中都生成一个实例,他们处于不同的上下文空间中,他们的行为方式是有可能不一样的。

2) 如果在application context和 servlet context中都存在同一个 @Service 的实例, controller(在servlet context中) 通过 @Resource引用时, 会优先选择servlet context中的实例。

不过最好的方法是:在applicationContext和dispatcher-servlet定义的bean最好不要重复, dispatcher-servlet最好只是定义controller类型的bean。

结论:

ApplicationContext.xml是spring 全局配置文件,用来控制spring 特性的。

dispatcher-servlet.xml是spring mvc里面的,控制器、拦截uri转发view。

使用applicationContext.xml文件时是需要在web.xml中添加listener的:

web.xml中applicationContext.xml的监听

1)ApplicationContext.xml文件内容:

applicationContext.xml配置文件

MyBatis.xml的配置文件:

mybatis-config.xml文件

2)DispatcherServlet.xml文件

dispatcher-servlet.xml文件

3)web.xml文件具体案例:

web.xml文件

相关文章

网友评论

      本文标题:springmvc配置文件

      本文链接:https://www.haomeiwen.com/subject/qwvrextx.html