springboot启动时不加载数据库

项目中引用了mongo驱动

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.9.0.RELEASE</version><!--$NO-MVN-MAN-VER$-->
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.6.0</version>
        </dependency>

 

启动时出现报错信息

com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:62)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java

原因
spring boot默认会加载
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration类,
DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean。
因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错。
例如:
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
解决方法
同理:mongo启动时 org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
在Application类上增加:
@EnableAutoConfiguration(exclude{MongoAutoConfiguration.class})

 或启动类上

@Configuration
@SpringBootApplication(scanBasePackages = "com.test",exclude={MongoAutoConfiguration.class})
public class WebApplication extends SpringBootServletInitializer {

 




posted @ 2020-12-11 15:46  桥头堡洗脚城  阅读(4114)  评论(0编辑  收藏  举报