SpringMVC 常用applicationContext.xml、web.xml、servlet-mvc.xml简单配置
在进行学习配置文件之前,为了加深对框架的认识,简单的做了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-config与component-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&characterEncoding=utf8&" />
<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简单配置的更多相关文章
- java Web 请求servlet绘制验证码简单例子
主要用来了解java代码怎么绘制验证码图片,实际开发中不会这样用 protected void doGet(HttpServletRequest request, HttpServletRespons ...
- Spring mvc系列一之 Spring mvc简单配置
Spring mvc系列一之 Spring mvc简单配置-引用 Spring MVC做为SpringFrameWork的后续产品,Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块 ...
- Spring MVC之简单入门
一.Spring MVC简介: 1.什么是MVC 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型(Model).视图(View)及控制器(Contr ...
- java之spring mvc之Controller配置的几种方式
这篇主要讲解 controller配置的几种方式. 1. URL对应 Bean 如果要使用此类配置方式,需要在XML中做如下样式配置 <!-- 配置handlerMapping --> & ...
- SpringMVC内容略多 有用 熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器、过滤器等Web组件以及MVC架构模式进行Java Web项目开发的经验。
熟悉基于JSP和Servlet的Java Web开发,对Servlet和JSP的工作原理和生命周期有深入了解,熟练的使用JSTL和EL编写无脚本动态页面,有使用监听器.过滤器等Web组件以及MVC架构 ...
- The difference between applicationContext.xml in Spring and xxx-servlet.xml in SpringMVC
一直搞不明白两者的区别.如果使用了SpringMVC,事实上,bean的配置完全可以在xxx-servlet.xml中进行配置.为什么需要applicationContext.xml?一定必须? 因为 ...
- springMVC之applicationcontext.xml配置说明
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 新建structs2 web应用及structs.xml常用基础配置
建立一个structs2 web应用程序 1. 创建一个基本的web应用程序 2. 添加structs2的jar文件到Class Path 将structs2的最小jar包拷到WEB-INF/lib目 ...
- 在SpringMVC框架下建立Web项目时web.xml到底该写些什么呢?
刚刚初学Spring MVC,却连一个简单的helloworld都搞的懵懵懂懂的,配置文件搞不清,各种文件之间的逻辑关系也不懂,连续看了好些日子的Spring MVC了,今天终于下定决心,每天记录一点 ...
随机推荐
- .net 服务端 访问共享文件夹
共享文件夹所在电脑为A服务器,网站部署在B服务器 A,B服务器上拥有同名账户,且密码也要相同.如账户名share,密码123. A服务器上,共享文件夹设置share账户有读写权限 B服务器上,IIS中 ...
- MySQL学习(四) SQL连接查询
更多情况下,我们查询的数据来源于多张表,所有有必要了解一下MySQL中的连接查询. SQL中将连接查询分成四类:交叉连接,内连接,外连接和自然连接. 数据准备 student表 -- -------- ...
- (2)Jquery1.8.3快速入门_checkbox全选取消部分选中
1. jquery示例功能: checkbox多选框 全选 .全不选. 选择部分. 源码 : <!DOCTYPE html> <html> <head> <m ...
- TCP服务器/客户端代码示例
TCP服务器代码: #include <errno.h> #include <string.h> #include <stdlib.h> #include < ...
- Tests of the Equality of Two Means
Introduction In this lesson, we'll continue our investigation of hypothesis testing. In this case, w ...
- webpack4 系列教程(一): 打包JS
webpack 本身就是为了打包js所设计,作为第一节,介绍怎么打包js. 1. 检验webpack规范支持 webpack支持es6, CommonJS, AMD. 创建vendor文件夹,其中mi ...
- 【读书笔记】iOS-优化内存
imageNamed:方法创建UIImage对象,这些对象不再使用的时候 会放到应用的默认自动回收池中,而不是当前的事件循环的自动回收池中,这样的对象占用的内存只有在应用结束的时候 才会回收.如果用这 ...
- WebGIS中利用AGS JS+eCharts实现一些数据展示的探索
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 eCharts提供了迁徙图.热点图.夜视图等跟地图能够很好的 ...
- 常见聚类算法——K均值、凝聚层次聚类和DBSCAN比较
聚类分析就仅根据在数据中发现的描述对象及其关系的信息,将数据对象分组(簇).其目标是,组内的对象相互之间是相似的,而不同组中的对象是不同的.组内相似性越大,组间差别越大,聚类就越好. 先介绍下聚类的不 ...
- python txt文件数据转excel
txt content: perf.txt 2018-11-12 16:48:58 time: 16:48:58 load average: 0.62, 0.54, 0.56 mosquitto CP ...