String factoryBeanId = directive.getFactoryId();
// By default, use dest object class name for factory bean id
String beanId = !MappingUtils.isBlankOrNull(factoryBeanId) ? factoryBeanId : classToCreate.getName();
BeanFactory factory = factoryCache.get(factoryName);
if (factory == null) {
Class<?> factoryClass = MappingUtils.loadClass(factoryName);
if (!BeanFactory.class.isAssignableFrom(factoryClass)) {
MappingUtils.throwMappingException("Custom bean factory must implement "
+ BeanFactory.class.getName() + " interface : " + factoryClass);
}
factory = (BeanFactory) ReflectionUtils.newInstance(factoryClass);
// put the created factory in our factory map
factoryCache.put(factoryName, factory);
}
Object result = factory.createBean(directive.getSrcObject(), directive.getSrcClass(), beanId);
log.debug("Bean instance created with custom factory -->\n Bean Type: {}\n Factory Name: {}",
result.getClass().getName(), factoryName);
if (!classToCreate.isAssignableFrom(result.getClass())) {
MappingUtils.throwMappingException("Custom bean factory (" + factory.getClass() +
") did not return correct type of destination data object. Expected : "
+ classToCreate + ", Actual : " + result.getClass());
}
return result;
}