applicationContext.xml配置简介
这里简单介绍一下spring的配置文件applicationContext.xml中的一些配置的作用。
<context:component-scan base-package=""/>
表示启动spring的组件扫描功能(从spring2.5版本开始)。即扫描base-package包或者子包下面的Java文件,如果扫描到有@controller、@Service、@Repository、@Component等注解的java类,就会将这些bean注册到工厂中。还可以使用分号来分隔多个扫描包。
如果在配置文件中配置了<context:component-scan />,就不用在配置<context:annotation-config/>,因为前者已经包含了后者。<context:annotation-config/>的作用是向spring容器注入AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 及RequiredAnnotationBeanPostProcessor 四个beanPostProcessor。从而使得@Autowired等注解生效。
<mvc:annotation-driven />
<mvc:annotation-driven />是告知Spring,我们启用注解驱动。然后Spring会自动为我们注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter等几个Bean到工厂中,此时我们可以使用@RequestMapping、@Valid注解来处理请求,也可以使用@ResponseBody来处理返回结果。
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/mail.properties</value>
<value>classpath: conf/sqlmap/jdbc.properties</value></list>
</property>
</bean>
PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。
上面有两种value的写法,其中classpath是引用src目录下的文件写法。
<bean scope="singleton" id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
DriverManager类的主要作用是管理注册到DriverManager中的JDBC驱动程序,并根据需要使用JDBC驱动程序建立与数据服务器的网络连接。
DriverManagerDataSource在每个连接请求时都新建一个连接,但是建立与数据库的连接是一项较耗资源的工作,频繁的进行数据库连接建立操作会产生较大的系统开销,为了解决上述问题,可以采用数据库连接池技术,例如dbcp、c3p0、druid(spring配置数据库连接池druid)。
<!-- 配置mybatis的SessionFactory -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:conf/mybatis-config.xml" />
<property name="mapperLocations">
<array>
<value>classpath:com/wdcloud/fayu/mapper/*Mapper.xml</value>
</array>
</property>
<!-- 配置此项则在mapper中可以直接使用实体类名,而不需要使用全路径名 -->
<property name="typeAliasesPackage" value="com.wdcloud.fayu.entity" />
</bean> <!-- 自动扫描注册mapper接口类(接口类实现mybatis) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wdcloud.fayu.mapper" />
</bean> <!-- sqlSessionTemplate配置(模板类实现mybatis) -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
上面为mybatis在spring中的配置,其中有两种实现方式:接口类实现和模板类实现。
SqlSessionFactoryBean是mybatis的核心管理类,通过dataSource指定数据源,configLocation指定mybatis的配置文件,mapperLocations指定mapper的xml文件。
MapperScannerConfigurer的作用是自动扫描注册mapper接口类到spring工厂中,当你的mapper接口类存在于多个目录下时,basePackage的值可以配置成多个目录,中间用英文逗号隔开即可。
SqlSessionTemplate的作用是基于持久化模板类实现Mybatis(具体实现步骤)。
未完待续
applicationContext.xml配置简介的更多相关文章
- Spring的配置文件ApplicationContext.xml配置头文件解析
Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applica ...
- springmvc.xml和applicationContext.xml配置的特点
1:springmvc.xml配置要点 一般它主要配置Controller的组件扫描器和视图解析器 下为:springmvc.xml文件 <?xml version="1.0" ...
- web.xml配置错误导致applicationContext.xml配置重复加载
web.xml相关配置 <context-param><param-name>log4jRefreshInterval</param-name><param- ...
- spring的applicationContext.xml配置SessionFactory抛异常
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFa ...
- 关于Spring中applicationContext.xml配置错误“org/springframework/transaction/interceptor/TransactionInterceptor”的问题解决
问题描述: 在配置spring的applicationContext.xml中的默认事务管理器的时候可能会出现这样的错误: Error occured processing XML 'org/spri ...
- Spring学习总结(7)——applicationContext.xml 配置文详解
web.xml中classpath:和classpath*: 有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件 ...
- 第九篇:Spring的applicationContext.xml配置总结
在前面的一篇日志中,记录了web.xml配置启动的顺序,web启动到监听器ContextLoaderListener时,开始加载spring的配置文件applicationContext.xml(通常 ...
- applicationContext.xml配置AOP切面编程
Computer.java package com.wh.aop2; public class Computer { public void play01(){ System.out.println( ...
- Spring中配置文件applicationContext.xml配置详解
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
随机推荐
- Mac 上的传奇效率神器 Alfred 3
下载地址:https://www.alfredapp.com/ 第三方教程:https://www.jianshu.com/p/e9f3352c785f 一.内置快捷键介绍 1.默认快捷呼出热键是: ...
- svn Couldn't open rep-cache database
出现这样的错误原因是因为 问题一 开始出现错误:The database disk image is malformed 解决方案:http://www.cnblogs.com/chen110xi/p ...
- 完美运动框架(js)
一.前言 学习js运动时,由于在实现多种不同运动效果过程中很多代码是重复的,故将其封装达到代码重用. 二.代码封装重用 function startMove(obj, json, fnEnd){ cl ...
- __attribute__系列之aligned
__attribute__的属性aligned,作用是为了设置字节对齐. aligned是对 变量和结构体进行 字节对齐的属性设置. 通过aligned属性设置(aligned(对齐字节数)),可以显 ...
- flex与bison
flex与bison 中文版 目录: 第一章:flex和bison简介 第二章:使用flex 第三章:使用bison 第四章:分析sql 第五章:flex规范参考 第六章:bison规范参考 第七章: ...
- Tomcat运行流程
Connector介绍 1.1 Connector的种类 Tomcat源码中与connector相关的类位于org.apache.coyote包中,Connector分为以下几类: Http Conn ...
- 项模板选择器属性(ItemTemplateSelector属性)和样式选择器(ItemContainerStyleSelector)
3.4.5 共享尺寸组 样式选择器: 或者========================================
- C++中的return返回值:return0 or return -1?
C++98 中定义了如下两种 main 函数的定义方式: int main( ) int main( int argc, char *argv[] ) (参考资料:ISO/IEC 14882(19 ...
- Quartz.NET 作业调度
Quartz 简介: Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调 ...
- 使用 fastjson将字符串转为 list<map<string,object>>
//先将字符串转为list 集合 json字符串linkConfJson List<Map> linkConf= JSONArray.parseArray(linkConfJson,Map ...