如何在同一个项目中连接两个数据库MySQL和MongoDB?有可能吗?

时间:2017-04-30 17:55:07

标签: java mongodb hibernate spring-mvc spring-data

目前我正在使用Hibernate(MySQL)和Spring,配置运行正常,但是一旦我配置了另一个配置mongo-config.xml文件并尝试使用mongodb运行测试用例,它的显示错误从第一次配置创建名为.... 的bean。

下面是我的mongo-config.xml

<context:annotation-config />
<context:component-scan base-package="com.test.mongo" />
<context:property-placeholder location="classpath:mongo-dao.properties" />

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

</bean>
<bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.MongoFactoryBean">
    <property name="driverClassName" value="${spring.datasource.driverClassName}" />
    <property name="host" value="${spring.data.mongodb.host}" />
    <property name="port" value="${spring.data.mongodb.port}" />
    <property name="databaseName" value="${spring.data.mongodb.database}" />

,我的第一个hibernate配置看起来像

<context:component-scan base-package="com.hb.dao" />
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${db.jdbc.driverClassName}" />
    <property name="url" value="${db.jdbc.url}" />
    <property name="username" value="${db.jdbc.username}" />
    <property name="password" value="${db.jdbc.password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>com.hb..dao.domain.entity</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql:false}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql:false}</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

堆栈跟踪

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

引起:org.springframework.beans.factory.BeanCreationException:创建名为'accessProfileDaoImpl'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.hibernate.SessionFactory com.soe.dao.AbstractDao.sessionFactory;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[org.hibernate.SessionFactory]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。 依赖注释:

这是我的测试类 -

    public class MongoQuestionsTest extends BaseDaoMongoTest{   
        private static final Logger logger = LogManager.getLogger(MongoQuestionsTest.class);

        @Autowired
        private MongoTestDao mongoTestDaoImpl;
        @Test
        public void saveQuestions(){
            MongoQuestions mongoQuestions = new MongoQuestions();
            mongoQuestions.setUsername("Hi");
            mongoQuestions.setPassword("Hello");
            mongoTestDaoImpl.save(mongoQuestions);
            logger.debug("Mongo User Set with id " + mongoQuestions.getId());
        }
    and **BaseDaoMongoTest**---

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:/mongo-config-test.xml"})
    public class BaseDaoMongoTest {
    }

在MongoTestDaoImpl类中,我只是自动连接MongoTemplate并调用它的save()方法。

1 个答案:

答案 0 :(得分:3)

我不确定这是否是最佳解决方案。但是,它对我有用。 如果你有两个使用Jpa模块的关系数据库,那么我建议你创建entitytransaction经理bean来读取每个数据源配置。有关上述用例,请参阅以下链接。

springboot always read data from primary datasource

如果您想要SQLNoSQL的组合,我会为entity数据库创建transcationMySQL个经理bean,因为它运行良好与Jpa。并为Mongo保留原样配置(配置直接从application.properties读取)。

MySQLConfiguration数据源配置类:

@Configuration
@PropertySource("classpath:persistence-multiple-db.properties")
@EnableJpaRepositories(basePackages = "com.springdata.dao.mysql", entityManagerFactoryRef = "mysqlEntityManager", transactionManagerRef = "mysqlTransactionManager")
public class MySQLConfiguration {

    @Autowired
    private Environment env;

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean mysqlEntityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(myMySQLDataSource());
        em.setPackagesToScan(new String[] { "com.springdata.models" });

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
        em.setJpaPropertyMap(properties);

        return em;
    }

    @Bean
    @Primary
    public DataSource myMySQLDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("spring.mysql.jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("spring.mysql.jdbc.url"));
        dataSource.setUsername(env.getProperty("spring.mysql.user"));
        dataSource.setPassword(env.getProperty("spring.mysql.pass"));

        return dataSource;
    }

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

以上数据源配置参数从类路径中的classpath:persistence-multiple-db.properties文件中读取。

# mysql jdbc connections
spring.mysql.jdbc.driverClassName=com.mysql.jdbc.Driver
spring.mysql.jdbc.url=jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false
spring.mysql.user=root
spring.mysql.pass=password1

# hibernate.X
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

以上配置应足以处理MySQL数据源。要在项目中进行mongo配置,请将以下行添加到application.properties

# mongo configuration
spring.data.mongodb.uri=mongodb://localhost
spring.data.mongodb.database=test

Springboot将自动创建必要的mongo数据源bean,并使它们随时可供弹簧容器使用。

现在,为MySQL和Mongo数据源创建存储库接口。

MyMongoRepository界面:

@Transactional
public interface MyMongoRepository extends MongoRepository<Users, String>{

}

MySQLRepository界面:

@Transactional
public interface MySQLRepository extends JpaRepository<Users, String>{

}

Users pojo class:

@Entity
@Table(name = "users")
@Document(collection="users")
@Data
public class Users {

    @Id
    @javax.persistence.Id
    private String id;
    private String name;
    private Integer age;

}

添加了以下注释以启用mongorepositoreis并将组件扫描添加到springboot主类。

@SpringBootApplication
@ComponentScan(basePackages = { "com.springdata" })
@EnableMongoRepositories(basePackages={"com.springdata.dao.mongo"})
public class SpringbootmysqlmongoApplication {

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

最后,要测试一些代码。

MyRepositoryImpl课程:

@Service
public class MyRepositoryImpl {

    @Autowired
    private MyMongoRepository myMongoRepository;

    @Autowired
    private MySQLRepository mySQLRepository;

    @PostConstruct
    public void extractUsers(){
        myMongoRepository.findAll().forEach((user) -> System.out.println("user name from mongo is : "+user.getName()));
        mySQLRepository.findAll().forEach((user) -> System.out.println("User name from mysql is : "+user.getName()));
    }

}

users数据库中创建了mysql test表,在users数据库中创建了mongo test集合。

最后,将我的代码上传到git repository。

https://github.com/harshavmb/springbootmysqlmongo