spring-mybatis核心笔记

mybatis通过代理模式产生mapper的代理类,jdk的动态代理

spring怎么把mybatis产生的代理对象注入到容器中?

  1. @MapperScan通过@Import引入MapperScannerRegistrar,这里采用@Import注解的第三种使用方式
  2. MapperScannerRegistrar类通过实现ImportBeanDefinitionRegistrar接口的registerBeanDefinitions方法把scan扫描到的类信息解析成beanDefinition放到beanDefinitionMap中,其中包括MapperFactoryBean,此类就是mybatis用来创建bean的factoryBean
  3. getSqlSseion通过jdk的动态代理返回mapper的代理对象,MethodProxy就是InvocationHandler接口的实现类,里面实现mapper的方法的具体逻辑
  4. mybatis插件原理
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.class)
public @interface MapperScan {

}

public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T> {

  /**
   * {@inheritDoc}
   */
  @Override
  public T getObject() throws Exception {
    return getSqlSession().getMapper(this.mapperInterface);
  }


}
public class MapperProxyFactory<T> {

  private final Class<T> mapperInterface;
  private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>();

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

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

  public Map<Method, MapperMethod> getMethodCache() {
    return methodCache;
  }

  @SuppressWarnings("unchecked")
  protected T newInstance(MapperProxy<T> mapperProxy) {
    //这里即jdk动态代理的方法
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }
public class MapperProxy<T> implements InvocationHandler, Serializable {

  private static final long serialVersionUID = -6424540398559729838L;
  private final SqlSession sqlSession;
  private final Class<T> mapperInterface;
  private final Map<Method, MapperMethod> methodCache;

  public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
    this.sqlSession = sqlSession;
    this.mapperInterface = mapperInterface;
    this.methodCache = methodCache;
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      if (Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, args);
      } else if (isDefaultMethod(method)) {
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }

  • spring注入对象的方式
  1. @Bean
  2. FactoryBean spring提供的一个接口,用来手动创建bean,特点是可以自定义bean的创建过程
  3. api registrySingleton --beanFactory.registerSingleton()
  4. FactoryMethod
  • beanfactory和factorybean

beanfactory  spring的bean工厂,产生bean;

factorybean是个特殊的bean,需要实现FactoryBean接口的三个方法,getObject方法返回一个bean

  • @Import注解的三种使用方式总结

第一种用法:@Import({ 要导入的容器中的组件 } ):容器会自动注册这个组件,id默认是全类名

 

第二种用法:ImportSelector:返回需要导入的组件的全类名数组,springboot底层用的特别多【重点 

 

第三种用法:ImportBeanDefinitionRegistrar:手动注册bean到容器

  • spring加载类的大概过程:

jvm加载类信息===========>beandefinition========>put到 beandefinitionMap ==========通过beanDefinition的信息创建bean

参考Factorybean,BeanFactory,Spring扩展点和生命周期

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值