无论我怎么尝试,我都无法禁用spring-data-mongodb-reactive的自动配置。
属性
spring:
profiles: dev
data:
mongodb:
uri: "mongodb://user:[email protected]:27017/my-db"
repositories:
type: reactive
authentication-database: admin
仓库
@Repository
interface IMembersRepository: ReactiveMongoRepository<Member, String> {}
MongoConfig
@Configuration
@EnableReactiveMongoRepositories(basePackages = ["com.my.package.repository"])
class MongoConfig : AbstractReactiveMongoConfiguration() {
override fun reactiveMongoClient(): MongoClient = mongoClient()
override fun getDatabaseName(): String = "my-db"
@Bean()
fun mongoClient() = MongoClients.create()
@Bean()
override fun reactiveMongoTemplate() = ReactiveMongoTemplate(mongoClient(), databaseName)
}
AppConfig
@Configuration
@EnableWebFlux
@ComponentScan("com.my.package")
class AppConfig: WebFluxConfigurer {
override fun addCorsMappings(registry: CorsRegistry) {
registry.addMapping("api/**")
}
}
SpringBootApplication
@SpringBootApplication(exclude = [
MongoReactiveAutoConfiguration::class,
MongoReactiveDataAutoConfiguration::class,
MongoReactiveRepositoriesAutoConfiguration::class,
MongoAutoConfiguration::class,
MongoDataAutoConfiguration::class,
MongoRepositoriesAutoConfiguration::class,
EmbeddedMongoAutoConfiguration::class
])
class AstridServerApplication
fun main(args: Array<String>) {
runApplication<AstridServerApplication>(*args)
}
正如你所看到的,我甚至禁用了所有可用的MongoDB自动配置,而 Boot 仍然试图建立到本地示例的连接,而我没有。但我也尝试了不同的组合。
[localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.AsynchronousSocketChannelStream$OpenCompletionHandler.failed(AsynchronousSocketChannelStream.java:117) ~[mongodb-driver-core-3.11.2.jar:na]
at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:129) ~[na:na]
at java.base/sun.nio.ch.Invoker.invokeDirect(Invoker.java:158) ~[na:na]
at java.base/sun.nio.ch.Invoker.invoke(Invoker.java:186) ~[na:na]
at java.base/sun.nio.ch.Invoker.invoke(Invoker.java:298) ~[na:na]
at java.base/sun.nio.ch.WindowsAsynchronousSocketChannelImpl$ConnectTask.failed(WindowsAsynchronousSocketChannelImpl.java:308) ~[na:na]
at java.base/sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:389) ~[na:na]
at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
Caused by: java.io.IOException: The remote computer refused the network connection
at java.base/sun.nio.ch.Iocp.translateErrorToIOException(Iocp.java:299) ~[na:na]
... 5 common frames omitted
2条答案
按热度按时间s4n0splo1#
客户端正在连接到默认地址。我会把重点放在理解为什么你的配置文件不生效,而不是“禁用自动配置”。您看到的行为与客户端不接收任何外部配置并使用其内置默认值的情况一致。
zed5wv102#
在spring-autoconfigure-metadata.properties中,我找到了一个可以指定的字段列表。
因为我需要覆盖AbstractReactiveMongoConfiguration中的代码(如果不使用reactive,还有其他类)
我只是扩展了这个类,并像这样指向它org.springframework.boot.autoconfigure.mongo。MongoReactiveAutoConfiguration=“mypackage.MyCustomAbstractReactiveMongoConfiguration”
不需要排除任何东西(这是不可能的)