Hat jemand versucht Autokonfiguration für mongodb im Frühjahr-Boot deaktivieren?Wie spring-data-mongodb-Autokonfiguration im Frühjahr-Boot zu deaktivieren
Ich versuche Spring-Boot mit Feder-Daten-Mongodb; Java-basierte Konfiguration verwenden Mit Spring-Boot 1.2.1.RELEASE importiere ich Spring-Boot-Starter-Web und dessen Eltern-Pom für das Abhängigkeitsmanagement. Ich importiere auch spring-data-mongodb (erprobter Spring-Boot-Starter-mongodb).
Ich muss eine Verbindung zu zwei verschiedenen MongoDB-Servern herstellen. Also muss ich zwei Sätze von Instanzen für Mongo-Verbindung, MongoTemplate etc. konfigurieren. Ich möchte auch die Autokonfiguration deaktivieren. Da ich eine Verbindung zu mehreren Servern herstelle, brauche ich keine standardmäßige MongoTemplate- und GridFsTemplate-Bean, die automatisch konfiguriert wird.
Meine Haupt Klasse sieht wie folgt 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 wie folgt aussehen:
@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 alles funktioniert. Wenn ich @Primary-Annotationen aus den MongoDbFactory1- und template1-Beans lösche, schlägt die Anwendung mit einer Ausnahme fehl, bei der die Autokonfiguration anscheinend 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
Haben Sie weitere Klassen mit Anmerkungen versehen mit 'SpringBootApplication' oder' EnableAutoConfiguration'? –
Versuchen Sie auch "MongoRepositoriesAutoConfiguration" auszuschließen. –
@AndyWilkinson peinlich Ich habe eine andere Klasse mit SpringBootApplication kommentiert. Ich hatte mehr als einen Einstiegspunkt - Haupt zum Testen und Apache Daemon + jsvc Runner für die Produktion und ich kopierte einfach alle Annotationen, anstatt sie an einen gemeinsamen Platz zu setzen ... Ausgenommen MongoRepositoriesAutoConfiguration erwies sich als nicht notwendig ... –