我正在尝试使用两个数据源设置一个Spring Boot应用程序。我已经成功运行了第一个数据源,但是当我添加类来处理第二个数据源时,第一个数据源不再起作用,并且导致第一个数据源的DAO无法自动装配,这意味着无法将DOA加载到我的控制器中。

这是设置第一个数据源的类。

@Configuration
@PropertySource({ "classpath:application.properties" })
@EnableJpaRepositories(
        basePackages = "digital.sheppard.dao.local",
        entityManagerFactoryRef = "localEntityManager",
        transactionManagerRef = "localTransactionManager"
)
public class LocalDBConfig {
    @Autowired
    private Environment env;

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean localEntityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(localDataSource());
        em.setPackagesToScan(new String[] { "digital.sheppard.model.local" });

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.hbm2ddl.auto", env.getProperty("datasource.local.hibernate.ddl-auto"));
        properties.put("hibernate.dialect", env.getProperty("datasource.local.hibernate.dialect"));
        properties.put("hibernate.naming-strategy", env.getProperty("org.hibernate.cfg.ImprovedNamingStrategy"));
        em.setJpaPropertyMap(properties);


        return em;
    }

    @Primary
    public DataSource localDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("datasource.local.driver-class-name"));
        dataSource.setUrl(env.getProperty("datasource.local.url"));
        dataSource.setUsername(env.getProperty("datasource.local.username"));
        dataSource.setPassword(env.getProperty("datasource.local.password"));

        return dataSource;
    }

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


这是尝试设置第二个数据源的类。

    @Configuration
@PropertySource({ "classpath:application.properties" })
@EnableJpaRepositories(
        basePackages = "digital.sheppard.dao.eclipse",
        entityManagerFactoryRef = "eclipseEntityManager",
        transactionManagerRef = "eclipseTransactionManager"
)
public class EclipseDBConfig {
    @Autowired
    private Environment env;

    @Bean
    public LocalContainerEntityManagerFactoryBean eclipseEntityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(eclipseDataSource());
        em.setPackagesToScan(new String[] { "digital.sheppard.model.eclipse" });

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.hbm2ddl.auto", env.getProperty("datasource.eclipse.hibernate.ddl-auto"));
        properties.put("hibernate.dialect", env.getProperty("datasource.eclipse.hibernate.dialect"));
        properties.put("hibernate.naming-strategy", env.getProperty("org.hibernate.cfg.ImprovedNamingStrategy"));
        em.setJpaPropertyMap(properties);


        return em;
    }

    public DataSource eclipseDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("datasource.eclipse.driver-class-name"));
        dataSource.setUrl(env.getProperty("datasource.eclipse.url"));
        dataSource.setUsername(env.getProperty("datasource.eclipse.username"));
        dataSource.setPassword(env.getProperty("datasource.eclipse.password"));

        return dataSource;
    }

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


在我看来,第二个数据源正在覆盖第一个数据源,结果,应用程序不再能够找到在主数据源中定义的DOA。

我的第二个数据源为什么会停止从第一个数据源停止DAO而不再加载?

最佳答案

我设法解决了这个问题,但我不确定是什么问题。我最终取消了课堂,然后重新开始。

对于其他有类似问题的人,这是我的application.properties文件

primary.datasource.url = jdbc:mysql://192.168.1.19/ticket
primary.datasource.username = username
primary.datasource.password = password
primary.datasource.driver-class-name = com.mysql.jdbc.Driver

secondary.datasource.url = jdbc:mysql://192.168.1.19/ticket2
secondary.datasource.username = username
secondary.datasource.password = passwprd
secondary.datasource.driver-class-name = com.mysql.jdbc.Driver


定义主要数据源,事务管理器和实体管理器的类。

@Configuration
@EnableJpaRepositories(
        basePackages = "digital.sheppard.dao.local",
        entityManagerFactoryRef = "localEntityManager",
        transactionManagerRef = "localTransactionManager"
)
public class LocalDBConfig {

    @Bean
    @Primary
    @ConfigurationProperties(prefix="primary.datasource")
    public DataSource primaryDataSource() {
        DataSource ds = DataSourceBuilder.create().build();

        return ds;
    }

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean localEntityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(primaryDataSource());
        em.setPackagesToScan(new String[] { "digital.sheppard.model.local" });

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);

        return em;
    }

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

}


定义第二个数据源的类非常相似,请注意排除@Primary

@Configuration
@EnableJpaRepositories(
        basePackages = "digital.sheppard.dao.eclipse",
        entityManagerFactoryRef = "eclipseEntityManager",
        transactionManagerRef = "eclipseTransactionManager"
)
public class EclipseDBConfig {

    @Bean
    @ConfigurationProperties(prefix="secondary.datasource")
    public DataSource secondaryDataSource() {
        DataSource ds = DataSourceBuilder.create().build();

        return ds;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean eclipseEntityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(secondaryDataSource());
        em.setPackagesToScan(new String[] { "digital.sheppard.model.eclipse" });

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);

        return em;
    }

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

}


主要数据源的DOA与用于辅助数据源的DOA相同,不同之处在于,辅助数据源定义了在@Transactional批注中使用哪个事务管理器。

@Transactional(transactionManager = "eclipseTransactionManager")
@Repository
public interface Person2Dao extends CrudRepository<Person2, Integer> {

}

10-06 12:52