当前位置: 首页 > 知识库问答 >
问题:

JPA数据存储库-找到EntityManager类型的2个bean

彭雨华
2023-03-14

我正在尝试在Spring应用程序中设置第二个数据源。以下是两个数据源的两个配置类:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.XYXYale.persistence.XY",
        entityManagerFactoryRef = "awEntityManagerFactory",
        transactionManagerRef= "awTransactionManager"
)
public class Datasource1DataSourceConfig {

    @Value("${spring.first-datasource.url}")
    private String url;

    @Value("${spring.first-datasource.username}")
    private String username;

    @Value("${spring.first-datasource.password}")
    private String pw;

    @Value("${spring.first-datasource.driver-class-name}")
    private String driver;


    @Bean
    @Primary
    @ConfigurationProperties("spring.first-datasource")
    public DataSourceProperties awDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    @Qualifier("awEntityManagerFactory")
    public DataSource awDataSource() {

        DriverManagerDataSource dataSource
                = new DriverManagerDataSource();
        dataSource.setDriverClassName(
                driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(pw);

        return dataSource;
    }

    @Primary
    @Bean(name = "awEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean awEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(awDataSource());
        em.setPersistenceUnitName("XY");



");
        em.setPackagesToScan(new String[] { "com.XY.XY.domain.XY" });
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
        em.setJpaVendorAdapter(vendorAdapter);
        return em;
    }

    @Primary
    @Bean
    public PlatformTransactionManager awTransactionManager() {
        JpaTransactionManager transactionManager
                = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(
                awEntityManagerFactory().getObject());
        return transactionManager;
    }

}

第二个配置类:


@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.XYXYale.persistence.YX",
        entityManagerFactoryRef = "sndEntityManagerFactory",
        transactionManagerRef= "sndTransactionManager"
)
public class SndDataSourceConfig {

    @Value("${spring.second-datasource.jdbcUrl}")
    private String url;

    @Value("${spring.second-datasource.username}")
    private String username;

    @Value("${spring.second-datasource.password}")
    private String pw;

    @Value("${spring.second-datasource.driver-class-name}")
    private String driver;

    @Bean
    @ConfigurationProperties("spring.second-datasource")
    public DataSourceProperties sndDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Qualifier("sndEntityManagerFactory")
    public DataSource sndDataSource() {
        DriverManagerDataSource dataSource
                = new DriverManagerDataSource();
        dataSource.setDriverClassName(
                driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(pw);

        return dataSource;
    }

    @Bean(name = "sndEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean sndEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(sndDataSource());
        em.setPersistenceUnitName("snd");
        em.setPackagesToScan(new String[] { "com.XY.XY.domain.YX" });
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
        em.setJpaVendorAdapter(vendorAdapter);
        return em;
    }

    @Bean
    public PlatformTransactionManager sndTransactionManager() {
        JpaTransactionManager transactionManager
                = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(
                sndEntityManagerFactory().getObject());
        return transactionManager;
    }

}

我在com上看到过。XYXYale。坚持不懈XY a Spring数据JPA回购协议定义如下

德莫雷波


@Repository
public interface DemoRepo extends CrudRepository<Demo, String>, DemoRepoCustom{
}

人口学

@NoRepositoryBean
public interface DemoRepoCustom {
    Demo returnDemoContent();
}

DemoRepoImpl

public class DemoRepoImpl extends QuerydslRepositorySupport implements DemoRepoCustom {

    public DemoRepoImpl() {
        super(Demo.class);
    }

    public Demo returnDemoContent(){
        return something;
    }

}

回购协议的使用方式如下:

@Autowired
DemoRepo demoRepo;

我得到一个例外:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoRepositoryImpl': Unsatisfied dependency expressed through method 'setEntityManager' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected single matching bean but found 2: org.springframework.orm.jpa.SharedEntityManagerCreator#0,org.springframework.orm.jpa.SharedEntityManagerCreator#1
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:678)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:376)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:307)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1255)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1175)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:595)
    ... 55 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected single matching bean but found 2: org.springframework.orm.jpa.SharedEntityManagerCreator#0,org.springframework.orm.jpa.SharedEntityManagerCreator#1
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:221)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1233)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1175)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:670)
    ... 70 more

有人对如何解决这个问题有建议吗?我可以想到为每个回购注入正确的实体经理,但是我不知道如何做到这一点。

提前感谢。在这里或其他网站上找不到任何解决方案。

暂时还没有答案

 类似资料:
  • 我是Spring Data JPA的新手。我试图为存储库创建一个自定义方法,但是它确实抛出了一个异常。以下是我目前的实现: 这是我启动应用程序时发生的异常(我正在使用 Spring 启动)。

  • 我试图实现一个自定义Spring存储库。我有接口: 实施: 和“主”存储库,扩展我的自定义存储库: 我使用的是Spring Boot,根据文档: 默认情况下,Spring Boot将启用JPA存储库支持,并查看@SpringBootApplication所在的包(及其子包)。 当我运行应用程序时,出现以下错误: 组织。springframework。数据映射。PropertyReferenceEx

  • 它是否将其存储在缓存中?我有一个应用程序,但应用程序中没有任何地方。属性是提到的db详细信息。我可以通过邮递员存储数据和查询它。

  • 我使用的是Spring Data JPA 1.4.3.RELEASE和Hibernate 4.2.7.Final,我成功地创建了一个基本存储库类,类似于:http://docs . Spring . io/Spring-Data/JPA/docs/1 . 4 . 2 . release/reference/html/repositories . html # repositories . cust

  • 我突然想到,可以用三个基于泛型的类来替换每个对象类型的多个类,从而节省大量的样板代码。我不是很清楚该怎么做,事实上这是不是一个好主意?