Created
February 12, 2017 10:21
-
-
Save redhead/13e125f2660370d3692bf43892c0cec1 to your computer and use it in GitHub Desktop.
Automatic registrar of even sourcing repository bean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mysystem; | |
import org.axonframework.commandhandling.model.Repository; | |
import org.axonframework.eventsourcing.EventSourcingRepository; | |
import org.axonframework.spring.stereotype.Aggregate; | |
import org.jetbrains.annotations.NotNull; | |
import org.springframework.beans.BeansException; | |
import org.springframework.beans.factory.BeanFactory; | |
import org.springframework.beans.factory.BeanFactoryAware; | |
import org.springframework.beans.factory.config.BeanDefinition; | |
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | |
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | |
import org.springframework.beans.factory.support.DefaultListableBeanFactory; | |
import org.springframework.beans.factory.support.RootBeanDefinition; | |
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | |
import org.springframework.core.ResolvableType; | |
import org.springframework.core.type.AnnotationMetadata; | |
public class EventSourcingRepositoryBeanDefinitionRegistrar implements | |
ImportBeanDefinitionRegistrar, BeanFactoryAware { | |
private DefaultListableBeanFactory beanFactory; | |
@Override | |
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { | |
this.beanFactory = (DefaultListableBeanFactory) beanFactory; | |
} | |
@Override | |
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { | |
String[] aggregatesBeanNames = beanFactory.getBeanNamesForAnnotation(Aggregate.class); | |
for (String aggregateBeanName : aggregatesBeanNames) { | |
BeanDefinition aggregateBeanDef = beanFactory.getBeanDefinition(aggregateBeanName); | |
try { | |
registerRepositoryBean(aggregateBeanName, aggregateBeanDef.getBeanClassName()); | |
} catch (ClassNotFoundException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
private void registerRepositoryBean(String aggregateBeanName, String aggregateClassName) throws ClassNotFoundException { | |
Class<?> aClass = Class.forName(aggregateClassName); | |
EventSourced annotation = beanFactory.findAnnotationOnBean(aggregateBeanName, EventSourced.class); | |
if (annotation != null) { | |
RootBeanDefinition repBeanDef = (RootBeanDefinition) BeanDefinitionBuilder.rootBeanDefinition(EventSourcingRepository.class) | |
.addConstructorArgValue(aClass) | |
.addConstructorArgReference("eventBus") | |
.getRawBeanDefinition(); | |
repBeanDef.setTargetType( | |
ResolvableType.forClassWithGenerics(Repository.class, aClass)); | |
String repositoryName = getRepositoryName(aggregateBeanName); | |
beanFactory.registerBeanDefinition(repositoryName, repBeanDef); | |
} | |
} | |
@NotNull | |
private String getRepositoryName(String aggregateBeanName) { | |
return aggregateBeanName.substring(0, 1).toLowerCase() | |
+ aggregateBeanName.substring(1) + "Repository"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment