Hat jemand versucht, die Autokonfiguration für mongodb im Springboot zu deaktivieren?
Ich probiere Springboot mit Spring-Data-Mongodb aus; Java-basierte Konfiguration verwenden; Mit Spring-Boot 1.2.1.RELEASE importiere ich Spring-Boot-Starter-Web und das übergeordnete Pom für das Abhängigkeitsmanagement. Ich importiere auch Spring-Data-Mongodb (auch Spring-Boot-Starter-Mongodb).
Ich muss eine Verbindung zu zwei verschiedenen MongoDB-Servern herstellen. Ich muss also zwei Instanzen für Mongo-Verbindung, MongoTemplate usw. konfigurieren. Ich möchte auch die automatische Konfiguration deaktivieren. Da ich mich mit mehreren Servern verbinde, brauche ich nicht eine einzige standardmäßig konfigurierte MongoTemplate- und GridFsTemplate-Bean, die automatisch konfiguriert ist.
Meine Hauptklasse sieht so aus:
@Configuration
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@ComponentScan
//@SpringBootApplication // @Configuration @EnableAutoConfiguration @ComponentScan
public class MainRunner {
public static void main(String[] args) {
SpringApplication.run(MainRunner.class, args);
}
}
Meine zwei Mongo-Konfigurationsklassen sehen folgendermaßen aus:
@Configuration
@EnableMongoRepositories(basePackageClasses = {Test1Repository.class},
mongoTemplateRef = "template1",
includeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*Test1Repository")}
)
public class Mongo1Config {
@Bean
public Mongo mongo1() throws UnknownHostException {
return new Mongo("localhost", 27017);
}
@Primary
@Bean
public MongoDbFactory mongoDbFactory1() throws UnknownHostException {
return new SimpleMongoDbFactory(mongo1(), "test1");
}
@Primary
@Bean
public MongoTemplate template1() throws UnknownHostException {
return new MongoTemplate(mongoDbFactory1());
}
}
und
@Configuration
@EnableMongoRepositories(basePackageClasses = {Test2Repository.class},
mongoTemplateRef = "template2",
includeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*Test2Repository")}
)
public class Mongo2Config {
@Bean
public Mongo mongo2() throws UnknownHostException {
return new Mongo("localhost", 27017);
}
@Bean
public MongoDbFactory mongoDbFactory2() throws UnknownHostException {
return new SimpleMongoDbFactory(mongo2(), "test2");
}
@Bean
public MongoTemplate template2() throws UnknownHostException {
return new MongoTemplate(mongoDbFactory2());
}
}
Mit diesem Setup funktioniert alles. Wenn ich @Primary-Annotationen aus den Beans mongoDbFactory1 und template1 entferne, schlägt die Anwendung mit einer Ausnahme fehl, die wie Autokonfiguration nicht deaktiviert wurde. Ausnahmemeldung ist unten aufgeführt:
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.Java:133)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:474)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.Java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.Java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.Java:321)
at org.springframework.boot.SpringApplication.run(SpringApplication.Java:961)
at org.springframework.boot.SpringApplication.run(SpringApplication.Java:950)
at com.fourexpand.buzz.web.api.template.MainRunner.main(MainRunner.Java:26)
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
at org.springframework.boot.context.embedded.Tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.Java:98)
at org.springframework.boot.context.embedded.Tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.Java:75)
at org.springframework.boot.context.embedded.Tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.Java:378)
at org.springframework.boot.context.embedded.Tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.Java:155)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.Java:157)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.Java:130)
... 7 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.core.io.ResourceLoader org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.resourceLoader; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gridFsTemplate' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoDataAutoConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.data.mongodb.MongoDbFactory]: : No qualifying bean of type [org.springframework.data.mongodb.MongoDbFactory] is defined: expected single matching bean but found 2: mongoDbFactory2,mongoDbFactory1; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.data.mongodb.MongoDbFactory] is defined: expected single matching bean but found 2: mongoDbFactory2,mongoDbFactory1
Wie Andy Wilkinson in Kommentaren darauf hingewiesen hat, stellen Sie bei der Verwendung von EnableAutoConfiguration mit der Ausschlussliste sicher, dass keine anderen Klassen mit EnableAutoConfiguration oder SpringBootApplication kommentiert sind.
So mache ich es:
@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
Mein Use Case war etwas anders. Ich hatte eine Anforderung für 2 verschiedene Datenbanken im selben Projekt. Ich erweiterte die Autokonfigurationsklassen und fügte eine Profilanmerkung hinzu.
@Profile("mongo")
@Configuration
public class CustomMongoAutoConfiguration extends MongoAutoConfiguration {
public CustomMongoAutoConfiguration(
MongoProperties properties,
ObjectProvider<MongoClientOptions> options,
Environment environment) {
super(properties,options,environment);
}
}
Und
@Profile("mongo")
@Configuration
@EnableConfigurationProperties(MongoProperties.class)
public class CustomMongoDataAutoConfiguration extends MongoDataAutoConfiguration {
public CustomMongoDataAutoConfiguration(
ApplicationContext applicationContext,
MongoProperties properties) {
super(applicationContext,properties);
}
}
Versuchen Sie, die Anwendung im Debug-Modus auszuführen. Dies geschieht, wenn die MongoDB-abhängige Konfiguration zu instanziieren versucht, die entsprechende Bean jedoch nicht vorhanden ist. In meinem Fall habe ich MongoDataAutoConfiguration.class und MongoRepositoriesAutoConfiguration.class ausgeschlossen, um die Anwendung auszuführen.
@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoRepositoriesAutoConfiguration .class,MongoDataAutoConfiguration.class})
public class SomeApplication {
//...
}