Привет, мне нужно подключить две разные базы данных mongoDB при весенней загрузке.
Я упомянул в файле свойств приложения как:
primary.mongodb.database=mydb
primary.mongodb.port=*****
primary.mongodb.host=******
secondary.mongodb.database=mydb2
secondary.mongodb.port=******
secondary.mongodb.host=******
Я создал два файла конфигурации с общим классом abstarct:
package webservice.com.employee.configure;
import com.mongodb.MongoClient;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
public abstract class AbstractMongoConfig {
//Mongo DB Properties
private String host , database;
private int port;
//Setter methods go here..
/*
* Method that creates MongoDbFactoray
* Common to both of the MongoDb connections
*/
//org.springframework.data.mongodb.MongoDatabaseFactory
public MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(new MongoClient(host, port), database);
}
/*
* Factory method to create the MongoTemplate
*/
abstract public MongoTemplate getMongoTemplate() throws Exception;
package webservice.com.employee.configure;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.core.MongoTemplate;
@Configuration //Configuration class
@ConfigurationProperties(prefix = "primary.mongodb") //Defines my custom prefix and points to the primary db properties
public class CommonMongoConfig extends AbstractMongoConfig {
/**
* Implementation of the MongoTemplate factory method
* @Bean gives a name (primaryMongoTemplate) to the created MongoTemplate instance
* @Primary declares that if MongoTemplate is autowired without providing a specific name,
* this is the instance which will be mapped by default
*/
@Primary
@Override
public @Bean(name = "primaryMongoTemplate") MongoTemplate getMongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory());
}
}
package webservice.com.employee.configure;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
@Configuration //Configuration
@ConfigurationProperties(prefix = "secondary.mongodb") //Defines my custom prefix and points to the secondary db properties
public class SecondaryMongoConfig extends AbstractMongoConfig {
/**
* Implementation of the MongoTemplate factory method
* @Bean gives a name (primaryMongoTemplate) to the created MongoTemplate instance
* Note that this method doesn't have @Primary
*/
@Override public @Bean(name = "secondaryMongoTemplate")
MongoTemplate getMongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory());
}
}
Но я не понимаю, почему я получаю ошибку ниже ..
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.0.RELEASE)
2020-05-26 17:04:53.191 INFO 4940 --- [ main] w.com.employee.EmployeeApplication : Starting EmployeeApplication on SWATI with PID 4940 (C:\Users\swatiswagatika\Desktop\Study\fullstack\project-1\employee\target\classes started by swatiswagatika in C:\Users\swatiswagatika\Desktop\Study\fullstack\project-1\employee)
2020-05-26 17:04:53.191 INFO 4940 --- [ main] w.com.employee.EmployeeApplication : No active profile set, falling back to default profiles: default
2020-05-26 17:04:54.063 ERROR 4940 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalArgumentException: Could not find class [org.springframework.data.mongodb.MongoDatabaseFactory]
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:335) ~[spring-
Я везде пытался исправить эту проблему ..
Любая помощь будет принята с благодарностью
зависимость:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>webservice.com</groupId>
<artifactId>employee</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>employee</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.0.0.RELEASE</version>
<!--<version>2.2.4.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.11.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>