Spring中的FactoryBean及Mybatis中Mapper生成原理解析

本文解析了Spring中的FactoryBean接口及其使用示例,详细解释了FactoryBean如何创建对象并返回。同时,文章深入探讨了Mybatis中Mapper的生成原理,通过MapperFactoryBean的源码分析,揭示了如何将配置的Mapper接口转换为实际的Mapper实例。在理解这两部分内容的基础上,有助于更好地理解和应用Spring与Mybatis的整合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##一、Spring中的FactoryBean
首先,让我们先来看一看FactoryBean接口的定义,由此可见,FactoryBean也是用来创建bean的,它所创建的bean即为它后面所跟泛型对应的类型的实例。

package org.springframework.beans.factory;

public abstract interface FactoryBean<T> {
	public abstract T getObject() throws Exception;

	public abstract Class<?> getObjectType();

	public abstract boolean isSingleton();
}

其中,getObject()方法返回创建的对象,getObjectType()方法返回创建的对象的类型,isSingleton()方法表示此对象是否为单例。

举个例子来看一下:
(1)User.java
先定义一个对象,此对象即为我们即将要实现的FactoryBean要创建的对象。

package com.alan.spring.extensionpoints;

public class User {
	private String name;
	private Integer age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "name: "+name+", age: "+age;
	}
}

(2)UserFactoryBean.java
创建上述对象的FactoryBean,实现了FactoryBean、InitializingBean、DisposableBean接口,其中InitializingBean接口主要是为了在UserFactoryBean的初始化方法afterPropertiesSet()中创建User的实例,DisposableBean接口主要与InitializingBean接口配对,并打印销毁日志。

package com.alan.spring.extensionpoints;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

public class UserFactoryBean implements FactoryBean<User>, InitializingBean, DisposableBean{

	private User user;
	
	@Override
	public void destroy() throws Exception {
		System.err.println("user factory bean destroy");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		user = new User();
		user.setName("tangtong");
		user.setAge(25);
	}

	@Override
	public User getObject() throws Exception {
		return user;
	}

	@Override
	public Class<?> getObjectType() {
		return user==null?User.class:user.getClass();
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

	@Override
	public String toString() {
		return "this is a user factory bean!";
	}
}

(3)dispatcher.xml
在spring配置文件中配置这个bean,注意,我们配置的是UserFactoryBean。

<bean id="user" class="com.alan.spring.extensionpoints.UserFactoryBean"></bean>

(4)UnitTest.java
从ApplicationContext中取出user,看它到底是什么。

package com.alan.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath*:META-INF/dispatcher.xml")
public class UnitTest {
	@Resource
	private ApplicationContext ctx;
	
	@Test
	public void test() {
		System.err.println("the user factory bean create the user : " + ctx.getBean("user"));  //取出的是User的实例
		System.err.println("the user factory bean is : " + ctx.getBean("&user")); //取出的是UserFactoryBean的实例
		
	}
}

(5)查看输出
如果不出意外,输出结果应该类似下面这样:

the user factory bean create the user : name: tangtong, age: 25
the user factory bean is : this is a user factory bean!
user factory bean destroy

从上面的结果可以看到,从ApplicationContext中取出的"user"是User的实例,取出的"&user"却是UserFactoryBean的实例。

##二、Mybatis中的Mapper生成原理解析

在Mybatis中如果配置单个Mapper,我们一般使用如下这种形式:

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper">
  <property name="sqlSessionFactory" ref="sqlSessionFactory">
</property></property></bean>

可见,这里的userMapper配置的是MapperFactoryBean类的实例,但是我们实际使用的过程中明明就是这么声明的啊:

@Autowired
private UserMapper userMapper;

按照配置,它是怎么变成UserMapper的实例的呢?学习了上面关于Spring中的FactoryBean,你可能已经明白了其中一二,下面让我们来看看具体是怎么实现的。

(1)MapperFactoryBean.java
进入MapperFactoryBean源码。

package org.mybatis.spring.mapper;

import org.apache.commons.logging.Log;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.util.Assert;

public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T> {
	private Class<T> mapperInterface;
	private boolean addToConfig = true;

	public MapperFactoryBean() {
	}

	public MapperFactoryBean(Class<T> mapperInterface) {
		this.mapperInterface = mapperInterface;
	}

	protected void checkDaoConfig() {
		super.checkDaoConfig();

		Assert.notNull(this.mapperInterface, "Property 'mapperInterface' is required");

		Configuration configuration = getSqlSession().getConfiguration();
		if ((this.addToConfig) && (!configuration.hasMapper(this.mapperInterface))) {
			try {
				configuration.addMapper(this.mapperInterface);
			} catch (Exception e) {
				this.logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", e);
				throw new IllegalArgumentException(e);
			} finally {
				ErrorContext.instance().reset();
			}
		}
	}

	public T getObject() throws Exception {
		return getSqlSession().getMapper(this.mapperInterface);
	}

	public Class<T> getObjectType() {
		return this.mapperInterface;
	}

	public boolean isSingleton() {
		return true;
	}

	public void setMapperInterface(Class<T> mapperInterface) {
		this.mapperInterface = mapperInterface;
	}

	public Class<T> getMapperInterface() {
		return this.mapperInterface;
	}

	public void setAddToConfig(boolean addToConfig) {
		this.addToConfig = addToConfig;
	}

	public boolean isAddToConfig() {
		return this.addToConfig;
	}
}

可见,MapperFactoryBean有一个属性叫mapperInterface,即上面的配置中传入的UserMapper接口。然后,找到它的getObject()方法,此方法返回的即是配置中userMapper对应的真实的实例,根据getObject()方法一层层进入。

(2)DefaultSqlSession.xml

	public <T> T getMapper(Class<T> type) {
		return this.configuration.getMapper(type, this);
	}

(3)Configuration.java

	public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
		return this.mapperRegistry.getMapper(type, sqlSession);
	}

(4)MapperRegistry.java

	public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
		MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory) this.knownMappers.get(type);
		if (mapperProxyFactory == null) {
			throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
		}
		try {
			return mapperProxyFactory.newInstance(sqlSession);
		} catch (Exception e) {
			throw new BindingException("Error getting mapper instance. Cause: " + e, e);
		}
	}

(5)MapperProxyFactory.java

	protected T newInstance(MapperProxy<T> mapperProxy) {
		return Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[] { this.mapperInterface },
				mapperProxy);
	}

	public T newInstance(SqlSession sqlSession) {
		MapperProxy<T> mapperProxy = new MapperProxy(sqlSession, this.mapperInterface, this.methodCache);
		return newInstance(mapperProxy);
	}

到此为止,可见在newInstance(mapperProxy)方法中,最后调用Java原生的代理类Proxy创建了一个mapperInterface接口(即我们所传的UserMapper接口)的实例,所以,我们在代码中可以直接注入UserMapper的实例。

最后,我们可以在单元测试中打印出userMapper的路径:

System.err.println(userInfoMapper.getClass().getName());

得到的结果类似下面这样:

com.sun.proxy.$Proxy28

其中,$Proxy28表示这是一个动态生成的代理类,它继承自java.lang.reflect.Proxy类。


欢迎关注我的公众号“彤哥读源码”,查看更多“源码&架构&算法”系列文章, 与彤哥一起畅游源码的海洋。

qrcode

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值