zoukankan      html  css  js  c++  java
  • Mybatis-Spring(2)MapperFactoryBean

    接上一节,BeanDefiniton设置

    //1、BeanDefiniton将自己的构造器设置为自己的className
    
    definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName());
    
    //2、BeanDefiniton将自己的BeanClass设置为:MapperFactoryBean.class
    
    definition.setBeanClass(this.mapperFactoryBean.getClass());
    
    //3、BeanDefiniton设置属性sqlSessionFactory
    
    definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName));

    实例化Bean

    1、调用构造方法实例化MapperFactoryBean

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

    2、设置属性,MapperFactoryBean的父类SqlSessionDaoSupport.setSqlSessionFactory方法

    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        if (!this.externalSqlSession) {
          this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
        }
    }

    2、调用afterPropertiesSet

    @Override
    public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException {
    	// Let abstract subclasses check their configuration.
    	checkDaoConfig();
    	// Let concrete implementations initialize themselves.
    	try {
    		initDao();
    	}
    	catch (Exception ex) {
    		throw new BeanInitializationException("Initialization of DAO failed", ex);
    	}
    }

    调用  checkDaoConfig,将mapperInterface放入configuration中,

    configuration.addMapper(this.mapperInterface) 会解析 mapperInterface (mapperInterface)对应的XML和注解。

    protected void checkDaoConfig() {
        super.checkDaoConfig();
    
        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) {
            logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", e);
            throw new IllegalArgumentException(e);
          } finally {
            ErrorContext.instance().reset();
          }
        }
      }

    因为是FactoryBean,用到的是FactoryBean.getObject()返回的数据。

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

    最终返回的是一个代理对象:mapperProxyFactory.newInstance(sqlSession);

    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.");
            } else {
                try {
                    return mapperProxyFactory.newInstance(sqlSession);
                } catch (Exception var5) {
                    throw new BindingException("Error getting mapper instance. Cause: " + var5, var5);
                }
            }
    }

     总结:

      对于一个MapperInterface,在Spring中会存在一个BeanDifition,两个实例:MapperFactory<MapperInterface.class> 和 ProxyBean。

      

  • 相关阅读:
    40岁后学习编程是否太晚了?7点技巧让学习变得轻松有趣
    Java 8五大主要功能为开发者提供了哪些便利?
    如何创建Vim Dotfile?
    程序员:我们为什么爱上直播编程?
    10个最好用的HTML/CSS 工具、插件和资料库
    如何选择PHP框架?
    编程语言五花八门,哪种可以让程序员赚到更多钱?
    安卓项目中使用JSON引发的一个小错误 Multiple dex files define Lorg/apache/commons/collections/Buffer
    (转)获取当前应用的版本号和当前android系统的版本号
    Android访问网络,使用HttpURLConnection还是HttpClient?
  • 原文地址:https://www.cnblogs.com/sleepingDogs/p/11102335.html
Copyright © 2011-2022 走看看