Mybatis-Spring(2)MapperFactoryBean

Connect one, BeanDefiniton set

// 1, BeanDefiniton own configured to set its className 

definition.getConstructorArgumentValues () addGenericArgumentValue (definition.getBeanClassName ());. 

// 2, BeanDefiniton own set BeanClass: MapperFactoryBean.class 

definition.setBeanClass (the this .mapperFactoryBean.getClass ()); 

//. 3, BeanDefiniton set properties SqlSessionFactory 

definition.getPropertyValues () the Add ( "SqlSessionFactory", new new RuntimeBeanReference (this.sqlSessionFactoryBeanName.));

Bean instantiation

1, examples of method call the constructor MapperFactoryBean

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

2, setting properties, The MapperFactoryBean parent class method SqlSessionDaoSupport.setSqlSessionFactory

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

2, call 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);
	}
}

Call checkDaoConfig, will mapperInterface into the configuration, the

configuration.addMapper (this.mapperInterface) resolves mapperInterface (mapperInterface) corresponding XML and annotations.

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();
      }
    }
  }

Because it is FactoryBean, it is used FactoryBean.getObject () returns the data.

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

The final returns a proxy object: 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);
            }
        }
}

 to sum up:

  For a MapperInterface, there will be a Spring BeanDifition in two instances: MapperFactory <MapperInterface.class> and ProxyBean.

  

 

Guess you like

Origin www.cnblogs.com/sleepingDogs/p/11102335.html