public class DomainScanner extends ClassPathBeanDefinitionScanner {
public DomainScanner(BeanDefinitionRegistry registry) {
super(registry, false);
}
@
Override protected boolean isCandidateComponent(MetadataReader metadataReader) {
return true;
}
@
Override protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
Set<BeanDefinitionHolder> beanDefinitionHolders = super.doScan(
basePackages);
for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
ScannedGenericBeanDefinition beanDefinition = (ScannedGenericBeanDefinition) beanDefinitionHolder
.getBeanDefinition();
String beanClassName = beanDefinition.getBeanClassName();
Class<?> entityClazz = null;
try {
entityClazz = Class.forName(beanClassName);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(beanClassName);
}
DynamicMapperCreator dynamicMapperCreator = new DynamicMapperCreator();
Class<?> mapperClazz = dynamicMapperCreator
.getOrCreateMapperClazz(entityClazz);
beanDefinition.setBeanClass(MapperFactoryBean.class);
ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
constructorArgumentValues.addIndexedArgumentValue(0, mapperClazz);
beanDefinition
.setConstructorArgumentValues(constructorArgumentValues);
beanDefinition.getPropertyValues().add("sqlSessionFactory",
new RuntimeBeanReference("sqlSessionFactory"));
}
return beanDefinitionHolders;
}
}
public class MapperRegister implements BeanDefinitionRegistryPostProcessor {
@
Override public void postProcessBeanDefinitionRegistry(
BeanDefinitionRegistry registry) throws BeansException {
new DomainScanner(registry).scan("umoo.wang.domain");
}
@
Override public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}