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 ...
随机推荐
- iOS_绘制带删除线的Label
效果图例如以下: 一个带删除线的文本标签,继承自UILabel 自绘代码过程例如以下: 1,重写控件的drawRect方法 2,首先得到上下文对象 3,设置颜色,并指定是填充(Fill)模式还是笔刷( ...
- the method getcontextpath() from the type httpservletrequest refers to the missing type string
导入项目的时候容易报出这个错误,主要因为JRE(jdk版本不一致). 解决方法:就是重新配置路径,配置你机器上安装的jdk. 右击该出错项目→ Build Path → Configure Build ...
- C++注释规范
1 源文件头部注释 列出:版权.作者.编写日期和描述. /************************************************* Copyright:bupt Auth ...
- iOS中解析Bonjour服务(转)
服务器端Bonjour服务发布成功之后,客户端可以通过NSNetService解析服务,解析成功后,可以获得通讯的数据细节,如:IP地址.端口等信息. 首先需要实例化NSNetService对象代码如 ...
- springboot解决第三方依赖jar包的问题
公司现在用的是springboot+maven,想要把一些老的项目都改成这种框架.但是一些老的项目中有好多第三方的jar包或者是自己的jar包,maven库上没有.最初的解决方案是一个个的deploy ...
- Zookeeper之ZKClient的使用
maven依赖 <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</ ...
- keypad代码分析
keypad作为input设备注册到内核,与platform总线驱动match. 1.描述一个输入设备对象 static struct input_dev *kpd_input_dev; 告知输入子系 ...
- 2017-5-14 湘潭市赛 Partial Sum 给n个数,每次操作选择一个L,一个R,表示区间左右端点,该操作产生的贡献为[L+1,R]的和的绝对值-C。 0<=L<R<=n; 如果选过L,R这两个位置,那么以后选择的L,R都不可以再选择这两个位置。最多操作m次,求可以获得的 最大贡献和。
Partial Sum Accepted : Submit : Time Limit : MS Memory Limit : KB Partial Sum Bobo has a integer seq ...
- iostat -d -k -x 1 10
iostat命令详解 转载 2011年08月03日 14:13:58 标签:磁盘 /扩展 /user 99809 iostat iostat用于输出CPU和磁盘I/O相关的统计信息. 命令格式: io ...
- postgresql学习文档
字符串函数: http://www.php100.com/manual/PostgreSQL8/functions-string.html http://gavin-chen.iteye.com/bl ...