在进行学习配置文件之前,为了加深对框架的认识,简单的做了SSM框架的简单实验。然后画出listAll查询方法的整个过程的思维导图。 整个过程中的web.xml、SpringMVC.xml、applicationContext.xml配置文件起到关键作用,同时也是为了加深理解,在此做下记录,请看下面! github源码链接:https://github.com/Jian0110/ssm

一、SpringMVC的web.xml配置

关于SpringMVC 的web.xml详细介绍,我之前已经做过了介绍并记录,请看Spring MVC的web.xml配置详解

1.思维导图:

2.详细描述:

(1)servlet配置(即SpringMVC核心分发器Dispatcher):

  • dispatcher配置与servlet-mvc.xml配置 若没有<init-param>初始化servlet-mvc.xml,那么就会自动寻找servletname-servlet.xml并加载!
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- spring mvc的配置文件:springMVC.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
  • dispatcherer 拦截路径配置:
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

(2)applicationContext.xml的自动装配:

ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext.xml的配置信息。

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

二、SpringMVC的servlet-mvc.xml配置

servlet-mvc.xml配置文件主要是用来扫描@Controller注解并注入,还有视图解析转向等。

1.思维导图

2.详细描述:

(1)annotation-configcomponent-scan

  • servlte-mvc.xml扫描注入bean的时候只需要找到@Controller注解,同时注入bean,其他的bean的注入是依靠applicationContext.xml进行的。
  • 关于扫描包component-scan的详细介绍可以看SpringMVC扫描包使用
<!-- 1. 扫描Controller,并将其生命周期纳入Spring管理(这里只扫描@Controller) -->

<!--  <context:annotation-config/> -->
<context:component-scan base-package="com.test.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
  • annotation-config:对已被注册到Spring容器里的Bean进行操作(比如注入到某个类的内部),主要的作用是为了能够识别相应的注解(@Autowired、@Required、@Resource、@ PostConstruct、@ PreDestroy等)。
  • component-scan:不仅具有annotation-config的功能,还可以扫描指定包下的类,将拥有注解的它们注册到Spring容器中中。

(2)annotation-driven注解驱动:

SpringMVC就知道了我们启用注解驱动。然后通过context:component-scan标签的配置,会自动为我们将扫描到的@Component,@Controller,@Service,@Repository等注解标记的组件注册到工厂中,来处理我们的请求。

<!-- 注解驱动,以使得访问路径与方法的匹配可以通过注解配置 -->
<mvc:annotation-driven />

(3)default-servlet-handler静态资源:

由于在web.xml中的的</servlet-mapping>拦截了所有的url请求,那么如果请求中使用到静态资源的话,则无法加载像js,css,jgp这样格式的资源。<mvc:default-servlet-handler />就是解决这个问题的,具体请看这位博主写的,解释很清晰mvc:default-servlet-handler标签的的作用

 <!-- 3. 静态页面,如html,css,js,images可以访问 -->
<mvc:default-servlet-handler />

(4)InternalResourceViewResolver视图解析器:

Controller层视图跳转mav.setViewName("listAll");中,只需要输入listAll.jsp文件前缀listAll即可跳转。

<!-- 4. 视图定位到/WEB/INF/jsp 这个目录下 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

三、SpringMVC的applicationContext.xml配置

1.思维导图:

2.详细描述:

(1)component-scan自动扫描

这里的component-scan不同servlet-mvc.xml中的component-scan,它不光只扫描@Controller,扫描包com.test的所有都会被注入。

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

(2)dataSource数据库配置

在此之前,通常还需要引入db.properties数据库配置文件,才能使用EL表达式。

<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<value>classpath*:bd.properties</value>
</property>
</bean>

数据库简单配置如下:


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库连接基本信息 -->
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}?useUnicode=true&amp;characterEncoding=utf8&amp;" />
<property name="username" value="${username}" />
<property name="password" value="${password}" /> <!-- 数据连接池基本信息 -->
<property name="maxActive" value="${maxActive}" />
<property name="maxIdle" value="${maxIdle}" />
<property name="defaultAutoCommit" value="false" />
<property name="timeBetweenEvictionRunsMillis" value="3600000"/>
<property name="minEvictableIdleTimeMillis" value="3600000"/>
</bean>

(3)扫描mybatis.xml文件:

<!-- 3. 扫描存放SQL语句的Category.xml -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.test.pojo" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/test/mapper/*.xml"/>
</bean>

(4)配置Mapper接口   :

Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring。  详情请看文章MapperScannerConfigurer自动扫描 将Mapper接口生成代理注入到Spring

<!-- 4. 扫描Mapper,并将其生命周期纳入Spring的管理 (配置Mapper接口)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.mapper"/>
</bean>

MapperScannerConfigurer将会创建MapperFactoryBean,之后自动装配。MapperFactoryBean创建的代理类实现了 XXXMapper接口,并且注入到应用程序中。这个bean就可以直接在应用程序中使用,比如:

 public class UserServiceImpl implements UserService {

  //直接使用userMapper
private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
} public User getUser(String userId) {
return this.userMapper.getUser(userId);
}
}

(5)TransactionManager事务管理:

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>

这样就可以在在Service实现类中增加@Transactional注解即可控制事务。

SpringMVC 常用applicationContext.xml、web.xml、servlet-mvc.xml简单配置的更多相关文章

  1. java Web 请求servlet绘制验证码简单例子

    主要用来了解java代码怎么绘制验证码图片,实际开发中不会这样用 protected void doGet(HttpServletRequest request, HttpServletRespons ...

  2. Spring mvc系列一之 Spring mvc简单配置

    Spring mvc系列一之 Spring mvc简单配置-引用 Spring MVC做为SpringFrameWork的后续产品,Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块 ...

  3. Spring MVC之简单入门

    一.Spring MVC简介: 1.什么是MVC 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型(Model).视图(View)及控制器(Contr ...

  4. java之spring mvc之Controller配置的几种方式

    这篇主要讲解 controller配置的几种方式. 1. URL对应 Bean 如果要使用此类配置方式,需要在XML中做如下样式配置 <!-- 配置handlerMapping --> & ...

  5. SpringMVC内容略多 有用 熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器、过滤器等Web组件以及MVC架构模式进行Java Web项目开发的经验。

    熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器.过滤器等Web组件以及MVC架构 ...

  6. The difference between applicationContext.xml in Spring and xxx-servlet.xml in SpringMVC

    一直搞不明白两者的区别.如果使用了SpringMVC,事实上,bean的配置完全可以在xxx-servlet.xml中进行配置.为什么需要applicationContext.xml?一定必须? 因为 ...

  7. springMVC之applicationcontext.xml配置说明

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

  8. 新建structs2 web应用及structs.xml常用基础配置

    建立一个structs2 web应用程序 1. 创建一个基本的web应用程序 2. 添加structs2的jar文件到Class Path 将structs2的最小jar包拷到WEB-INF/lib目 ...

  9. 在SpringMVC框架下建立Web项目时web.xml到底该写些什么呢?

    刚刚初学Spring MVC,却连一个简单的helloworld都搞的懵懵懂懂的,配置文件搞不清,各种文件之间的逻辑关系也不懂,连续看了好些日子的Spring MVC了,今天终于下定决心,每天记录一点 ...

随机推荐

  1. OpenCV Mat与UIImage之间的转换

    UIImage 转 OpenCV cvMat: - (cv::Mat)cvMatWithImage:(UIImage *)image { CGColorSpaceRef colorSpace = CG ...

  2. [SDOI2010] 外星千足虫

    Description 公元2089年6月4日,在经历了17年零3个月的漫长旅行后,"格纳格鲁一号"载人火箭返回舱终于安全着陆.此枚火箭由美国国家航空航天局(NASA)研制发射,行 ...

  3. 深入浅出 JVM GC(2)

    # 前言 在 深入浅出 JVM GC(1) 中,限于上篇文章的篇幅,我们留下了一个问题 : 如何回收? 这篇文章将重点讲述这个问题. 在上篇文章中,我们也列出了一些大纲,今天我们就按照那个大纲来逐个讲 ...

  4. 【转】repo介绍

    Android 使用 Git 作为代码管理工具,开发了 Gerrit 进行代码审核以便更好的对代码进行集中式管理,还开发了 Repo 命令行工具,对 Git 部分命令封装,将百多个 Git 库有效的进 ...

  5. babel-polyfill的引用和使用

    前两天一个首页项目,想用vue玩耍一下,就用vue-cli搭建了一套vue的开发框架 完成开发.联调和上线后,问题来了 chrome.ff浏览器下都能正常显示的页面,在百度浏览器下愣就显示不出来了 我 ...

  6. Swagger2限定接口范围

    前面在使用Swagger2时遇到的坑中简单介绍了Swagger的使用. 不过默认情况下,Swagger2会把项目中的所有接口都展示在列表里,特别是你用了Springboot/SpringCloud之后 ...

  7. Linux 学习记录 四(Bash 和 Shell scirpt).

    一.什么是 Shell? 狭义的shell指的是指令列方面的软件,包括基本的Linux操作窗口Bash等,广义的shell则包括 图形接口的软件,因为图形接口其实也可以操作各种驱动程序来呼叫核心进行工 ...

  8. Flask 中的蓝图(BluePrint)

    蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开 怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查 ...

  9. 常见的CSS代码无效问题

    在前端开发中经常会遇到一些CSS代码设置无效的情况,下面我总结一些我遇到的情况. 1.height:100%无效 百分比的高度在设定时需要根据这个元素的父元素容器的高度.例如一个div的高度设为40% ...

  10. [Android] 设置AlertDialog中按钮的可用(Enable)状态

    弹出一个保存文件的对话框,要控制输入内容限制,同时内容为空时保存按钮不可用. 原文地址请保留http://www.cnblogs.com/rossoneri/p/4140184.html 直接上代码: ...