`
zhangwei_david
  • 浏览: 474732 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

SqlMapClient 在Spring中创建过程

 
阅读更多

     在Spring中集成iBATIS是一件很简单的事情,只需要简单的配置即可;在Spring中使用iBATIS的配置如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/jee 
		http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
		http://www.springframework.org/schema/task  
        http://www.springframework.org/schema/task/spring-task-3.1.xsd  
		">
	<context:component-scan base-package="com.david.*" />
	<aop:aspectj-autoproxy />
	<context:property-placeholder location="classpath:META-INF/config.properties" />
	<!-- 定义数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.ams.driver}" />
		<property name="jdbcUrl" value="${jdbc.ams.url}" />
		<property name="user" value="${jdbc.ams.username}" />
		<property name="password" value="${jdbc.ams.password}" />
		<property name="initialPoolSize" value="${initialSize}" />
		<property name="minPoolSize" value="${minPoolSize}" />
		<property name="maxPoolSize" value="${maxActive}" />
		<property name="acquireIncrement" value="${acquireIncrement}" />
		<property name="maxIdleTime" value="${maxIdleTime}" />
	</bean>

	<!-- 定义jdbc模板类 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:META-INF/sqlmap/sqlmap.xml" />
	</bean>
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<tx:annotation-driven transaction-manager="transactionManager" />

 我们知道SqlMapClientImpl实例是iBTATIS的门面,所有的操作都是调用client的方法,那么SqlMapClient的实例又是如何,在什么地方被创建的呢?

   我们通过配置文件可以发现,client是通过一个工厂方法创建的,具体的流程如下图所示:


 

通过SqlMapClientFactoryBean源码可以发现,该类实现了InitializingBean

 

public class SqlMapClientFactoryBean implements FactoryBean<SqlMapClient>, InitializingBean {

 这个接口的作用又是什么呢?可以看看该接口的注释:

 

 

/**
  * Invoked by a BeanFactory after it has set all bean properties supplied
  * (and satisfied BeanFactoryAware and ApplicationContextAware).
  * <p>This method allows the bean instance to perform initialization only
  * possible when all bean properties have been set and to throw an
  * exception in the event of misconfiguration.
  * @throws Exception in the event of misconfiguration (such
  * as failure to set an essential property) or if initialization fails.
  */
 void afterPropertiesSet() throws Exception;

一个类实现了这个接口就会在所有属性初始化完成后,调用afterPropertiesSet()方法,而SqlMapClientFactoryBean正是在这个方法中进行SqlMapClientImpl的实例初始化。

 

 

 

	public void afterPropertiesSet() throws Exception {
		if (this.lobHandler != null) {
			// Make given LobHandler available for SqlMapClient configuration.
			// Do early because because mapping resource might refer to custom types.
			configTimeLobHandlerHolder.set(this.lobHandler);
		}

		try {
			this.sqlMapClient = buildSqlMapClient(this.configLocations, this.mappingLocations, this.sqlMapClientProperties);

			// Tell the SqlMapClient to use the given DataSource, if any.
			if (this.dataSource != null) {
				TransactionConfig transactionConfig = (TransactionConfig) this.transactionConfigClass.newInstance();
				DataSource dataSourceToUse = this.dataSource;
				if (this.useTransactionAwareDataSource && !(this.dataSource instanceof TransactionAwareDataSourceProxy)) {
					dataSourceToUse = new TransactionAwareDataSourceProxy(this.dataSource);
				}
				transactionConfig.setDataSource(dataSourceToUse);
				transactionConfig.initialize(this.transactionConfigProperties);
				applyTransactionConfig(this.sqlMapClient, transactionConfig);
			}
		}

		finally {
			if (this.lobHandler != null) {
				// Reset LobHandler holder.
				configTimeLobHandlerHolder.remove();
			}
		}
	}

	/**
	 * Build a SqlMapClient instance based on the given standard configuration.
	 * <p>The default implementation uses the standard iBATIS {@link SqlMapClientBuilder}
	 * API to build a SqlMapClient instance based on an InputStream (if possible,
	 * on iBATIS 2.3 and higher) or on a Reader (on iBATIS up to version 2.2).
	 * @param configLocations the config files to load from
	 * @param properties the SqlMapClient properties (if any)
	 * @return the SqlMapClient instance (never <code>null</code>)
	 * @throws IOException if loading the config file failed
	 * @see com.ibatis.sqlmap.client.SqlMapClientBuilder#buildSqlMapClient
	 */
	protected SqlMapClient buildSqlMapClient(
			Resource[] configLocations, Resource[] mappingLocations, Properties properties)
			throws IOException {

		if (ObjectUtils.isEmpty(configLocations)) {
			throw new IllegalArgumentException("At least 1 'configLocation' entry is required");
		}

		SqlMapClient client = null;
		SqlMapConfigParser configParser = new SqlMapConfigParser();
		for (Resource configLocation : configLocations) {
			InputStream is = configLocation.getInputStream();
			try {
				client = configParser.parse(is, properties);
			}
			catch (RuntimeException ex) {
				throw new NestedIOException("Failed to parse config resource: " + configLocation, ex.getCause());
			}
		}

		if (mappingLocations != null) {
			SqlMapParser mapParser = SqlMapParserFactory.createSqlMapParser(configParser);
			for (Resource mappingLocation : mappingLocations) {
				try {
					mapParser.parse(mappingLocation.getInputStream());
				}
				catch (NodeletException ex) {
					throw new NestedIOException("Failed to parse mapping resource: " + mappingLocation, ex);
				}
			}
		}

		return client;
	}

 跟踪代码就可以发现其后的初始化就是一层层new 一个新的对象。

 

  • 大小: 22.7 KB
0
0
分享到:
评论

相关推荐

    ibatis与spring的整合

    在整合iBATIS和Spring的过程中,主要目标是利用Spring的IOC(Inversion of Control)容器来管理和协调数据访问层(DAO)以及事务处理,同时利用iBATIS作为SQL映射框架,提供灵活的数据库操作。以下将详细阐述整合的...

    Spring中文帮助文档

    12.2.2. 在Spring容器中创建 SessionFactory 12.2.3. The HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. 声明式的...

    Spring与iBATIS的集成

    3. **异常处理**:Spring框架中的异常处理机制可以很好地与iBATIS相结合,统一处理数据库操作过程中可能出现的各种异常情况。 4. **DAO支持**:Spring为DAO(Data Access Object)提供了丰富的支持,包括DAO抽象层...

    Spring+iBatis整合详解

    在整合过程中,我们需要将Spring、iBatis以及MySQL数据库驱动等相关的jar包添加到项目中。具体包括: - spring-framework-1.2.7:提供Spring的核心功能。 - iBATIS_DBL-2.1.7.597:iBatis的核心库。 - mysql-...

    Spring.3.x企业应用开发实战(完整版).part2

    Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。  Spring3.0引入了众多Java开发者翘首以盼的新功能...附录B 在Spring中开发Web Service

    Spring API

    12.2.2. 在Spring容器中创建 SessionFactory 12.2.3. The HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. 声明式的...

    Spring+Webwork+iBatis 组合实例

    在这套架构中,WebWork主要负责处理前端请求并将请求转发给后端服务,而Spring则负责管理整个项目的依赖关系和服务对象。iBatis则被用来处理数据库交互。通过这种组合,可以实现一个高度解耦的系统架构,提高系统的...

    Spring3.x企业应用开发实战(完整版) part1

    Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。  Spring3.0引入了众多Java开发者翘首以盼的新功能...附录B 在Spring中开发Web Service

    spring+ibatis整合web所需ibatis包

    1. **依赖注入**:Spring可以通过配置文件或注解管理iBatis的数据源、SqlSessionFactory和SqlSessionTemplate,简化对象创建过程。 2. **事务管理**:Spring提供了一致的事务管理机制,可以轻松地处理跨多个DAO操作...

    struts+iBATIS+spring整合例子

    Struts、iBATIS和Spring是Java开发中常用的三大框架,它们各自负责不同的职责,而将它们整合在一起,可以构建出高效、灵活的企业级应用程序。本文将深入探讨这三者的核心概念、整合过程以及实际应用。 **Struts** ...

    ibatis spring

    Spring框架是一个开源的应用框架,旨在简化企业级应用开发过程中的复杂性。Spring提供了依赖注入(DI)和面向切面编程(AOP)等特性,这些特性有助于提高代码的可测试性和可维护性。 #### 集成iBatis与Spring 将...

    Spring整合ibatis

    - `sqlMapClient`:注入由Spring创建的SqlMapClient实例。 ##### 5. DAO层设计 DAO层需要继承`SqlMapClientDaoSupport`类或实现相应的接口,以便于使用SqlMapClient。 ```java public class TestDao extends ...

    spring+ibatis eclipse工程示例

    Spring 和 iBATIS 是两个非常重要的 Java 开发框架,它们在企业级应用开发中被广泛使用。本示例工程是将这两个框架集成到 Eclipse IDE 中的一个实例,旨在帮助开发者理解和学习如何在实际项目中结合使用 Spring 和 ...

    ibatis整合spring

    在Spring框架中整合iBatis,我们需要配置数据源、SqlMapClient以及SqlMapClientTemplate。以下是一个基本的配置示例: ```xml &lt;bean id="dataSource" class="org.springframework.jdbc.datasource....

    Spring 3.0 整合 iBatis 3 Beta10 配置

    在Spring 3.0中整合iBatis 3 Beta10是一个相对复杂的过程,因为官方文档并未明确提及对iBatis 3的支持。不过,通过一些自定义配置和理解两个框架的基本原理,开发者可以实现这样的整合。下面我们将详细介绍如何进行...

    ibatis+spring完全整合

    在软件开发过程中,将ORM框架(如ibatis)与应用框架(如Spring)进行整合可以极大地提高开发效率和系统的可维护性。本文将详细介绍如何实现ibatis与Spring框架的完全整合,包括数据库配置、实体类设计、DAO层实现、...

    struts+spring+ibaits配置详解

    例如,在 Service 层中,可以通过 Spring 的依赖注入机制来获取事务管理器和 SQL 映射客户端,从而实现对数据库的操作。 ```java @Service public class UserServiceImpl implements UserService { @Autowired ...

    图文搭建SSI(struts+spring+ibatis)框架

    在实际开发过程中,这样的框架组合提供了良好的分层架构,Struts2负责控制层,Spring处理依赖注入和事务管理,iBatis则作为数据访问层,使得代码更易于维护和扩展。记得在开发过程中根据项目需求进行调整,确保最佳...

    【个人经验】SSH集成步骤 SSI集成步骤

    5. 在Spring的配置文件中,定义SqlSessionTemplate或SqlSessionFactoryBean,注入Mapper接口。 6. 在Service层,通过@Autowired注入Mapper,调用其方法执行数据库操作。 缓存技术在提高系统性能方面起着关键作用。...

Global site tag (gtag.js) - Google Analytics