从Spring Boot写入2个数据库的问题

时间:2019-03-29 20:40:53

标签: spring-boot

我正在从github尝试这段代码:https://github.com/kodinor/spring-data-many-dbs
这是一个如何在Spring-boot应用程序中使用多个数据库的示例。它工作正常,但我添加了Spring-boot-starter-web依赖关系,现在出现错误:

>Method requestMappingHandlerMapping in >org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$En>ableWebMvcConfiguration required a single bean, but 2 were found:
>   - productDSEmFactory: defined by method 'productDSEmFactory' in class >path resource [com/kodinor/configuration/ProductDBConfiguration.class]
>   - userDSEmFactory: defined by method 'userDSEmFactory' in class path >resource [com/kodinor/configuration/UserDBConfiguration.class]
>
>
>Action:
>
>Consider marking one of the beans as @Primary, updating the consumer to >accept multiple beans, or using @Qualifier to identify the bean that should >be consumed<br>

我有两个配置文件:

    @Configuration
    @EnableTransactionManagement 
    @EnableJpaRepositories(basePackageClasses = UserRepository.class,  entityManagerFactoryRef = "userDSEmFactory", transactionManagerRef = "userDSTransactionManager")
    public class UserDBConfiguration {

        @Primary
        @Bean
        @ConfigurationProperties("spring.datasource1")
        public DataSourceProperties userDSProperties() {
        return new DataSourceProperties();
        }

        @Primary
        @Bean
        public DataSource userDS(@Qualifier("userDSProperties") DataSourceProperties userDSProperties) {
            return userDSProperties.initializeDataSourceBuilder().build();
        }

        @Bean
        public LocalContainerEntityManagerFactoryBean     userDSEmFactory(@Qualifier("userDS") DataSource userDS, EntityManagerFactoryBuilder builder) {
            return builder.dataSource(userDS).packages(User.class).build();
    }
        @Primary
        @Bean
        public PlatformTransactionManager userDSTransactionManager(EntityManagerFactory userDSEmFactory) {
        return new JpaTransactionManager(userDSEmFactory);
        }

    }
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(basePackageClasses = ProductRepository.class,     entityManagerFactoryRef = "productDSEmFactory", transactionManagerRef = "productDSTransactionManager")
    public class ProductDBConfiguration {

        @Bean
        @ConfigurationProperties("spring.datasource2")
        public DataSourceProperties productDSProperties() {
        return new DataSourceProperties();
        }

        @Bean
        public DataSource productDS(@Qualifier("productDSProperties")  DataSourceProperties productDSProperties) {
        return productDSProperties.initializeDataSourceBuilder().build();
        }

        @Bean
        public LocalContainerEntityManagerFactoryBean productDSEmFactory(@Qualifier("productDS") DataSource productDS, EntityManagerFactoryBuilder builder) {
            return builder.dataSource(productDS).packages(Product.class).build();
        }

        @Bean
        public PlatformTransactionManager productDSTransactionManager(EntityManagerFactory productDSEmFactory) {
        return new JpaTransactionManager(productDSEmFactory);
        }

    }

Application.properties:

    spring.jpa.hibernate.ddl-auto=create
    spring.jpa.generate-ddl=true

    spring.datasource1.url=jdbc:mysql://localhost:3306/userdb?autoReconnect=true&useSSL=false
    spring.datasource1.username=user
    spring.datasource1.password=pass

    spring.datasource2.url=jdbc:mysql://localhost:3306/productdb?autoReconnect=true&useSSL=false
    spring.datasource2.username=user
    spring.datasource2.password=pass

还有一个简单的主应用程序,它添加了一些初始化数据:

    @SpringBootApplication
    public class SpringDataManyDbsApplication {

        public static void main(String[] args) {
        SpringApplication.run(SpringDataManyDbsApplication.class, args);
        }

        @Autowired
        private UserRepository userRepository;

        @Autowired
        private ProductRepository productRepository;

        @PostConstruct
        void init () {

            User user = new User("john", "doe");
            userRepository.save(user);

            Product product = new Product("chair", 5);
            productRepository.save(product);
        }
}

我尝试将@Primary批注添加到userDSEmFactory。错误消失了,但产品数据不再保存。有什么想法可以解决这个问题吗?我没有使用Spring-boot的丰富经验,并且阅读了数十篇文章,但是许多文章似乎都以不同的方式做事。非常感谢您的帮助!

更新

我添加了@Primary注释,如下所示:

        @Primary  
        @Bean
        public LocalContainerEntityManagerFactoryBean     userDSEmFactory(@Qualifier("userDS") DataSource userDS, EntityManagerFactoryBuilder builder) {
            return builder.dataSource(userDS).packages(User.class).build();
    }

没有更多错误了,但是只有用户被保存在主数据库中,而没有任何东西被保存到产品(第二个)数据库中。 因此,如果有人提出建议,可能是什么原因。

1 个答案:

答案 0 :(得分:0)

您在@Primary上缺少userDSEmFactory注释

@Primary
@Bean
public LocalContainerEntityManagerFactoryBean userDSEmFactory(@Qualifier("userDS") DataSource userDS, EntityManagerFactoryBuilder builder) {
    return builder.dataSource(userDS).packages(User.class).build();
}

在主班上一次@EnableTransactionManagement也很有趣。