Java 如何在spring引导中禁用spring数据mongodb自动配置
有人试过在spring boot中禁用mongodb的自动配置吗 我正在试用SpringDataMongoDB的SpringBoot;使用基于java的配置;使用spring boot 1.2.1.RELEASE,我导入spring boot starter web及其父pom以进行依赖关系管理。我还导入了spring数据mongodb(也尝试了spring启动程序mongodb) 我需要连接到两个不同的MongoDB服务器。因此,我需要为mongo connection、MongoTemplate等配置两组实例。我还想禁用自动配置。由于我连接到多个服务器,因此不需要自动配置单个默认MongoTemplate和GridFSTemplatebean。 我的主要课程是这样的:Java 如何在spring引导中禁用spring数据mongodb自动配置,java,spring-boot,spring-data,spring-data-mongodb,Java,Spring Boot,Spring Data,Spring Data Mongodb,有人试过在spring boot中禁用mongodb的自动配置吗 我正在试用SpringDataMongoDB的SpringBoot;使用基于java的配置;使用spring boot 1.2.1.RELEASE,我导入spring boot starter web及其父pom以进行依赖关系管理。我还导入了spring数据mongodb(也尝试了spring启动程序mongodb) 我需要连接到两个不同的MongoDB服务器。因此,我需要为mongo connection、MongoTempla
@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);
}
}
@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());
}
}
我的两个mongo配置类如下所示:
@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);
}
}
@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());
}
}
及
在这种设置下,一切正常。若我从mongoDbFactory1和Template1bean中删除@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
org.springframework.context.ApplicationContextException:无法启动嵌入式容器;嵌套异常为org.springframework.boot.context.embedded.EmbeddedServletContainerException:无法启动嵌入式Tomcat
位于org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
位于org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)
位于org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
位于org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
位于org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
位于org.springframework.boot.SpringApplication.run(SpringApplication.java:961)
位于org.springframework.boot.SpringApplication.run(SpringApplication.java:950)
位于com.fourexpand.buzz.web.api.template.MainRunner.main(MainRunner.java:26)
原因:org.springframework.boot.context.embedded.EmbeddedServletContainerException:无法启动嵌入式Tomcat
位于org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:98)
位于org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.(TomcatEmbeddedServletContainer.java:75)
位于org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.gettomcatembeddedservletContainerContainer(TomcatEmbeddedServletContainerFactory.java:378)
位于org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.GetEmbeddedServletContainerFactory(TomcatEmbeddedServletContainerFactory.java:155)
位于org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:157)
位于org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
... 省略了7个公共框架
原因:org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter”的bean时出错:自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动连接字段:private org.springframework.core.io.ResourceLoader org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.ResourceLoader;嵌套异常为org.springframework.beans.factory.unsatifiedDependencyException:创建名为“gridFsTemplate”的bean时出错,该bean在类路径资源[org/springframework/boot/autoconfigure/mongo/MongoDataAutoConfiguration.class]中定义:未满足的依赖项通过索引为0的构造函数参数表示[org.springframework.data.mongodb.MongoDbFactory]::未定义类型为[org.springframework.data.mongodb.MongoDbFactory]的合格bean:应为单个匹配bean,但找到了2:mongoDbFactory2,mongoDbFactory1;嵌套异常为org.springframework.beans.factory.nonuniquebeandefinitionexception:未定义类型的合格bean[org.springframework.data.mongodb.MongoDbFactory]已定义:需要单个匹配bean,但找到了2:mongoDbFactory2,mongoDbFactory1
正如Andy Wilkinson在评论中指出的,当使用带有排除列表的EnableAutoConfiguration时,确保没有其他类用EnableAutoConfiguration或SpringBootApplication注释。我的用例略有不同。我在同一个项目中需要两个不同的数据库。我扩展了自动配置并添加了一个纵断面注释
@Profile("mongo")
@Configuration
public class CustomMongoAutoConfiguration extends MongoAutoConfiguration {
public CustomMongoAutoConfiguration(
MongoProperties properties,
ObjectProvider<MongoClientOptions> options,
Environment environment) {
super(properties,options,environment);
}
}
我就是这样做的:
@SpringBootApplication(exclude = {
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
或根据以下建议:
尝试在调试模式下运行应用程序。当依赖MongoDB的配置试图实例化但相应的bean不存在时,会发生这种情况。 在我的例子中,我已经排除了MongoDataAutoConfiguration.class和MongorPositioneSautoConfiguration.class,以运行应用程序
@springboot应用程序
@EnableAutoConfiguration(排除={MongoAtoConfiguration.class,MongoRepositoriesAutoConfiguration.class,MongoDataAutoConfiguration.class})
公共类应用程序{
//...
}
与弹簧靴2.3/Kotlin相同
@SpringBootApplication(排除=[MongoAtoConfiguration::class,MongoDataAutoConfiguration::class]
@Profile(“mongo”)
@配置
类CustomMongoutoAtoConfiguration:MongoutoAtoConfiguration(){
蒙哥(
属性:MongoProperties?,
环境:环境?,
BuilderCustomizer:对象提供程序?,
设置:ObjectProvider?
):MongoClient{
返回super.mongo(属性、环境、builderCustomizers、设置)
}
spring.autoconfigure.exclude= \
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
spring.autoconfigure.exclude[0]: org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
spring.autoconfigure.exclude[1]: org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration
spring.autoconfigure.exclude[0]: org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration
spring.autoconfigure.exclude[1]: org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration