如何从单个Spring Boot应用程序处理多个MySQL数据库?

时间:2019-01-11 15:57:32

标签: mysql spring spring-boot javafx maven-3

我阅读了许多文章,但不幸的是,它不能帮助我理解问题的根本原因。

出于法规和安全方面的原因,我不得不将应用程序的逻辑分为两个工具:一个用于管理,另一个用于“实际”用户应用程序。 因此,我在服务器版本5.7上有两个MySQL数据库实例。尽管用户工具仅访问一个数据库,但管理工具需要访问两个数据库中的实体。

这些工具都是基于JavaFX和Spring Boot的。由于这种架构设置,我有了三个Maven软件包:一个用于管理工具,一个用于所有与管理相关的实体, 服务等),一个用于用户工具以及所有与该用户工具相关的实体,服务等,第三种用于这两个工具共享的所有实体。 实际上,我想将这些共享的实体存储在用户数据库中,因为此工具将更频繁地使用此数据,并且我相信对于db-admin来说会更容易 维护。

我尝试遵循https://www.baeldung.com/spring-data-jpa-multiple-databases第6部分中建议的设置。Spring Boot中的多个数据库。但到目前为止,它只是部分 成功。根据本文,我在带有管理工具类的目录中具有以下配置类:

package com.agiletunes.security;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class MultipleDbConfiguration {

    /*
     * Persistence of com.agiletunes.security
     */
    @Bean
    @Primary
    @ConfigurationProperties(prefix="security.datasource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    /*
     * 
     * Persistence of com.agiletunes.domain.organization
     */
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

我的application.properties:

# Employee database
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.jdbcUrl=jdbc:mysql://127.0.0.1/agiletunesdb?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&characterSetResults=utf-8
spring.datasource.username=[aUsername]
spring.datasource.password=[aPassword]

# Account database
security.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
security.datasource.jdbcUrl=jdbc:mysql://127.0.0.1/authenticationdb?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&characterSetResults=utf-8
security.datasource.username=[aUsername]
security.datasource.password=[aPassword]

# create db schema
spring.jpa.hibernate.ddl-auto=create
#spring.jpa.hibernate.ddl-auto=update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

第一个限制是仅生成security.datasource数据库模式,而不会生成spring.datasource模式。

另一个问题是,当尝试通过管理工具的Controller类自动连接共享Maven包中的服务类时, org.springframework.beans.factory.NoSuchBeanDefinitionException:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.agiletunes.domain.organization.OrganizationService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1506)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581)
    ... 21 more

相关的控制器代码为:

package com.agiletunes.security.server;

// imports

import com.agiletunes.domain.organization.Employee;
import com.agiletunes.domain.organization.OrganizationService;
import com.agiletunes.security.account.Account;
import com.agiletunes.security.account.AccountService;
...


@Controller
public class AuthServerCtrl implements Initializable {


    @Autowired protected AccountService accountService;     // works fine
    @Autowired protected OrganizationService orgService;    // throws Exception

    ...
}

代码可以编译,因此Maven软件包的可见性不是问题。

如果有什么问题的帮助,或者提供描述我需要的文档的链接,我将不胜感激。

0 个答案:

没有答案