Intellij Idea Sping Boot ,mongo DB bean error for SRV host name

eulz3vhy  于 2023-10-15  发布在  Go
关注(0)|答案(1)|浏览(75)

这是错误显示:
启动ApplicationContext时出错。要显示条件评估报告,请在启用“调试”的情况下重新运行应用程序。2023-08- 20 T13:02:09.356+05:30 ERROR 22968 - [ restartedMain] o.s. Boot . Spring应用程序:应用程序运行失败

org.springframework.beans.factory.BeanCreationException:创建在类路径资源[org/springframework/boot/autoconfigure/mongo/MongoAutoconfiguration.class]中定义的名为"mongo"的bean时出错:示例化[com. mongodb. client. MongoClient]失败:工厂方法'mongo'抛出异常,并显示消息:提供的SRV主机名"${env. MONGO_CLUSTER}"至少不包含三个部分。它必须包含主机名、域名和顶级域。在org。SpringFramework。豆子。工厂。支持。ConstructorResolver。instantiate(ConstructorResolver.java:659)~[spring-beans-6.0.11.jar:6.0.11]在org. SpringFramework。豆子。工厂。支持。ConstructorResolver。instantiateUsingFactoryMethod(ConstructorResolver.java:647)~[spring-beans-6.0.11.jar:6.0.11]在org. SpringFramework。豆子。工厂。支持。AbstractAutowireCapableBeanFactory. instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1332)~[spring-beans-6.0.11.jar:6.0.11]在org. SpringFramework。豆子。工厂。支持。AbstractAutowireCapableBeanFactory. BeanInstance(AbstractAutowireCapableBeanFactory.java:1162)~[spring-beans-6.0.11.jar:6.0.11]在org. SpringFramework。豆子。工厂。支持。AbstractAutowireCapableBeanFactory. documentBean(AbstractAutowireCapableBeanFactory.java:560)~[spring-beans-6.0.11.jar:6.0.11]在org. SpringFramework。豆子。工厂。支持。AbstractAutowireCapableBeanFactory. AbstractAutowireCapableBeanFactory.java:520)~[spring-beans-6.0.11.jar:6.0.11]在org.springframework.beans.factory.support.AbstractBeanFactory.java$doGetBean$0(AbstractBeanFactory.java:326)~[spring-beans-6.0.11.jar:6.0.11]在org. SpringFramework。豆子。工厂。支持。DefaultSingletonBeanRegistry。getSingleton(DefaultSingletonBeanRegistry.java:234)~[spring-beans-6.0.11.jar:6.0.11]在org. SpringFramework。豆子。工厂。支持。AbstractBeanFactory. doGetBean(AbstractBeanFactory.java:324)~[spring-beans-6.0.11.jar:6.0.11]在org. SpringFramework。豆子。工厂。支持。AbstractBeanFactory. getBean(AbstractBeanFactory.java:200)~[spring-beans-6.0.11.jar:6.0.11]在org. SpringFramework。豆子。工厂。支持。DefaultListableBeanFactory。preInstantiateSingletons(DefaultListableBeanFactory.java:973)~[spring-beans-6.0.11.jar:6.0.11]在org. SpringFramework。背景。支持。AbstractApplicationContext. finishBeanFactoryList(AbstractApplicationContext.java:942)~[spring-context-6.0.11.jar:6.0.11]在org. SpringFramework。背景。支持。AbstractApplicationContext. refresh(AbstractApplicationContext.java:608)~[spring-context-6.0.11.jar:6.0.11]在org. SpringFramework。 Boot 。web. servlet。背景。ServletWebServerApplicationContext。refresh(ServletWebServerApplicationContext.java:146)~[spring-boot-3.1.2.jar:3.1.2.在org. SpringFramework。 Boot 。SpringApplication. refresh(SpringApplication.java:734)~[spring-boot-3.1.2.jar:3.1.2.在org. SpringFramework。 Boot 。SpringApplication. refreshContext(SpringApplication.java:436)~[spring-boot-3.1.2.jar:3.1.2]在 www.example.com 示例化[com. mongodb. client. MongoClient]失败:org.springframework.boot.SpringApplication.run提供的SRV主机名"${env. MONGO_CLUSTER}"至少不包含三个部分。org.springframework.boot.SpringApplication.runat org. springframework. beans. factory. support. SimpleInstantiationStrategy. instantiate(SimpleInstantiationStrategy.java:171)~[spring-beans-6.0.11.jar:6.0.11] at org. springframework. beans. factory. support. ConstructorResolver. instantiate(ConstructorResolver.java:655)~[spring-beans-6.0.11.jar:6.0.11].. org.springframework.boot.devtools.restart.RestartLauncher.runjava. lang. IllegalArgumentException:提供的SRV主机名"${env. MONGO_CLUSTER}"至少不包含三个部分。它必须包含主机名、域名和顶级域。在com.mongodb.connection.budgetsSettings。(MonterSettings.java:594)~[mongodb-driver-core-4.9.1.jar:在com.mongodb.connection.configurterSettings。(MonterSettings.java:50)~[mongodb-driver-core-4.9.1.jar:n. com. mongodb.连接。配置设置$www.example.com(配置设置. java:370)~[mongodb-driver-core-4.9.1.jar:在com.mongodb.MongoClientSettings。(MongoClientSettings.java:957)~[mongodb-driver-core-4.9.1.jar:在com.mongodb.MongoClientSettings。(MongoClientSettings.java:64)~[mongodb-driver-core-4.9.1.jar:n. com. mongodb. MongoClientSettings $www.example.com(MongoClientSettings.java:600)~[mongodb-driver-core-4.9.1.jar:在org. SpringFramework。 Boot 。自动配置。mongo。MongoClientFactorySupport。安装MongoClient(MongoClientFactorySupport.java:50)~[spring-boot-autoconfigure-3.1.2.jar:3.1.2.在org. SpringFramework。 Boot 。自动配置。mongo。MongoAutoConfiguration。mongo(MongoAutoconfiguration. java:59)~[spring-boot-autoconfigure-3.1.2.jar:3.1.2在Java中。base/jdk.内部的反思。DirectMethodHandleAccessor。invoke(DirectMethodHandleController. java:104)~[na:在 java 。base/java. lang。反思。方法。invoke(Method.java:578)~[na:在org. SpringFramework。豆子。工厂。支持。SimpleInstantiationStrategy。instantiate(SimpleInstantiationStrategy.java:139)~[spring-beans-6.0.11.jar:6.0.11].省略23个公共帧

进程已完成,退出代码为0
这是我的pom.xml:
4.0.0 org.springframework. Boot spring- Boot -starter-parent 3.1.2 com.example full 0.0.1-SNAPSHOT full Demo project for Sping Boot <java.version>20</java.version>org.springframework. Boot spring- Boot -starter-data-mongodb org.springframework. Boot spring- Boot -starter-web

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>me.paulschwarz</groupId>
        <artifactId>spring-dotenv</artifactId>
        <version>4.0.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>
jfgube3f

jfgube3f1#

由于您使用的是Spring项目版本3.1.0或更高版本,并且在pom.xml文件中,spring-dotenv版本为4.0.0
在您的application.properties文件中更改语法应该是$(MONGO_****)而不是$(env.MONGO_****)
在.env文件中提及连接URI的详细信息

MONGO_DATABASE=""
MONGO_USER=""
MONGO_PASSWORD=""
MONGO_CLUSTER=""

在application.properites文件中

spring.data.mongodb.database=${MONGO_DATABASE}
spring.data.mongodb.uri=mongodb+srv://${MONGO_USER}:${MONGO_PASSWORD}@${MONGO_CLUSTER}

在进行这些更改之前,我遇到了同样的错误,并且已经解决了。希望能帮上忙!

相关问题