繁体   English   中英

在Spring Data JPA存储库中使用@Primary

[英]Using @Primary in Spring Data JPA repositories

如果有人需要在Spring Data存储库上使用@Primary:看起来Spring Data JPA会忽略存储库上的@Primary注释。

作为一种解决方法,我创建了BeanFactoryPostProcessor ,它检查给定的存储库是否有@Primary注释并将该bean设置为主要。

这是代码:

@Component
public class SpringDataPrimaryPostProcessor implements BeanFactoryPostProcessor {
    public static final String REPOSITORY_INTERFACE_PROPERTY = "repositoryInterface";


    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        makeRepositoriesPrimary(getRepositoryBeans(beanFactory));
    }

    protected List<BeanDefinition> getRepositoryBeans(ConfigurableListableBeanFactory beanFactory) {
        List<BeanDefinition> springDataRepositoryDefinitions = Lists.newArrayList();
        for (String beanName : beanFactory.getBeanDefinitionNames()) {
            BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);

            String beanClassName = beanDefinition.getBeanClassName();
            try {
                Class<?> beanClass = Class.forName(beanClassName);
                if (isSpringDataJpaRepository(beanClass)) {
                    springDataRepositoryDefinitions.add(beanDefinition);
                }
            } catch (ClassNotFoundException e) {
                throw new ApplicationContextException(String.format("Error when trying to create instance of %s", beanClassName), e);
            }
        }

        return springDataRepositoryDefinitions;
    }

  protected void makeRepositoriesPrimary(List<BeanDefinition> repositoryBeans) {
    for (BeanDefinition repositoryBeanDefinition : repositoryBeans) {
        String repositoryInterface = (String) repositoryBeanDefinition.getPropertyValues().get(REPOSITORY_INTERFACE_PROPERTY);
            if (isPrimary(repositoryInterface)) {
                log.debug("Making site repository bean primary, class: {}", repositoryInterface);
                repositoryBeanDefinition.setPrimary(true);
            }
    }
}

protected boolean isSpringDataJpaRepository(Class<?> beanClass) {
    return RepositoryFactoryInformation.class.isAssignableFrom(beanClass);
}

private boolean isPrimary(String repositoryInterface) {
    return AnnotationUtils.findAnnotation(getClassSafely(repositoryInterface), Primary.class) != null;
}

    private Class<?> getClassSafely(String repositoryInterface) {
        try {
            return Class.forName(repositoryInterface);
        } catch (ClassNotFoundException e) {
            throw new ApplicationContextException(String.format("Error when trying to create instance of %s", repositoryInterface), e);
        }
    }

我尝试将解决方案应用于具有两个Mongo存储库的spring boot应用程序。 但它无法在propertyValues中找到repositoryInterface 进一步的调查显示,有一个属性可以识别存储库接口factoryBeanObjectType

所以改变方法makeRepositoriesPrimary()的代码来自:

String repositoryInterface = (String) repositoryBeanDefinition.getPropertyValues().get(REPOSITORY_INTERFACE_PROPERTY);

至:

String repositoryInterface = (String) repositoryBeanDefinition.getAttribute("factoryBeanObjectType");

按预期工作。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM