Java 如何在 spring-boot 中禁用 spring-data-mongodb 自动配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28747909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable spring-data-mongodb autoconfiguration in spring-boot
提问by im__
Has anyone tried disabling autoconfiguration for mongodb in spring-boot?
有没有人试过在 spring-boot 中禁用 mongodb 的自动配置?
I am trying out spring-boot with spring-data-mongodb; Using java based configuration; Using spring-boot 1.2.1.RELEASE, I import spring-boot-starter-web and its' parent pom for dependency management. I also import spring-data-mongodb (tried spring-boot-starter-mongodb as well).
我正在尝试使用 spring-data-mongodb 的 spring-boot;使用基于java的配置;使用 spring-boot 1.2.1.RELEASE,我导入 spring-boot-starter-web 及其父 pom 以进行依赖项管理。我还导入了 spring-data-mongodb(也尝试了 spring-boot-starter-mongodb)。
I need to connect to two different MongoDB servers. So I need to configure two sets of instances for mongo connection, MongoTemplate etc. I also want to disable auto-configuration. Since I am connecting to multiple servers, I don't need to have a single default MongoTemplate and GridFsTemplate bean autoconfigured.
我需要连接到两个不同的 MongoDB 服务器。所以我需要为 mongo 连接配置两组实例,MongoTemplate 等。我也想禁用自动配置。由于我连接到多个服务器,因此我不需要自动配置单个默认的 MongoTemplate 和 GridFsTemplate bean。
My main class looks like this:
我的主类看起来像这样:
@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);
}
}
My two mongo configuration classes look like this:
我的两个 mongo 配置类如下所示:
@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());
}
}
and
和
@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());
}
}
With this setup everything works. If I remove @Primary annotations from mongoDbFactory1 and template1 beans, application will fail with an exception that seems like autoconfiguration hasn't been disabled. Exception message is listed below:
使用此设置,一切正常。如果我从 mongoDbFactory1 和 template1 bean 中删除 @Primary 注释,应用程序将失败,并出现一个似乎没有禁用自动配置的异常。下面列出了异常消息:
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
采纳答案by im__
As pointed out by Andy Wilkinson in comments, when using EnableAutoConfiguration with exclude list make sure there are no other classes annotated with EnableAutoConfiguration or SpringBootApplication.
正如 Andy Wilkinson 在评论中指出的那样,当使用带有排除列表的 EnableAutoConfiguration 时,请确保没有其他类使用 EnableAutoConfiguration 或 SpringBootApplication 进行注释。
回答by Miguel Pereira
My use case was slightly different. I had a requirement for 2 different databases in the same project. I extended the auto configuration classes and added a profile annotation.
我的用例略有不同。我需要在同一个项目中使用 2 个不同的数据库。我扩展了自动配置类并添加了配置文件注释。
@Profile("mongo")
@Configuration
public class CustomMongoAutoConfiguration extends MongoAutoConfiguration {
public CustomMongoAutoConfiguration(
MongoProperties properties,
ObjectProvider<MongoClientOptions> options,
Environment environment) {
super(properties,options,environment);
}
}
And
和
@Profile("mongo")
@Configuration
@EnableConfigurationProperties(MongoProperties.class)
public class CustomMongoDataAutoConfiguration extends MongoDataAutoConfiguration {
public CustomMongoDataAutoConfiguration(
ApplicationContext applicationContext,
MongoProperties properties) {
super(applicationContext,properties);
}
}
回答by user5365075
This is how I do it:
这就是我的做法:
@SpringBootApplication(exclude = {
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
or as suggested by Dan Oak:
或者按照Dan Oak 的建议:
spring.autoconfigure.exclude= \
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
回答by Shrinivas
Try to Run application in Debug mode. It happens when MongoDB dependant configaration is trying to instantiate but respective bean not present. In My case I have excluded MongoDataAutoConfiguration.class and MongoRepositoriesAutoConfiguration.class, to get the application run.
尝试在调试模式下运行应用程序。当 MongoDB 相关配置尝试实例化但相应的 bean 不存在时,就会发生这种情况。在我的情况下,我排除了 MongoDataAutoConfiguration.class 和 MongoRepositoriesAutoConfiguration.class,以使应用程序运行。
@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoRepositoriesAutoConfiguration .class,MongoDataAutoConfiguration.class})
public class SomeApplication {
//...
}
@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoRepositoriesAutoConfiguration .class,MongoDataAutoConfiguration.class})
public class SomeApplication {
//...
}