Spring 启动不工作?
Spring Boot not working?
我已经使用命令
生成了一个标准的spring引导示例
spring init
结果是一个普通的 Java 项目,带有类似
的 pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.DemoApplication</start-class>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
当我打
spring run src/main/java/demo/DemoApplication.java
它用
之类的错误消息将我踢出局
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'propertySourceBootstrapConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.propertySourceLocators; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configServicePropertySource' defined in class org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration$PropertySourceLocatorConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.config.client.ConfigServicePropertySourceLocator]: Factory method 'configServicePropertySource' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/web/client/HttpServerErrorException
我无法解释。
我做错了什么?
这个问题的标签之一是 spring-mvc,因此我假设您想要使用 Web Spring 功能。
要启用 Spring Web 功能,您需要添加此 Spring 将此依赖项引导到 POM 中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
我以前没有使用过Spring CLI,所以我不明白为什么它没有生成带有Web 依赖项的项目。但我敢打赌,您需要以某种方式明确指定要生成 Web 项目。
所以您可能生成了没有 Web 依赖项的纯 Spring 引导项目,并在 application.properties 中添加了一些与 Web 相关的配置。因此 Spring 引导自动配置器正在尝试在您的类路径中查找 Web 依赖项。
尽管这已经有了答案,但同样的问题可能会随着 maven 依赖项的损坏而出现。因此,任何无法解决相同问题的人都可以尝试从
中删除工件(或完整的本地存储库)
c:\Users\<username>\.m2\repository
手动或尝试 运行 mvn dependency:purge-local-repository
并重新加载 IDE 中的项目。
我已经使用命令
生成了一个标准的spring引导示例spring init
结果是一个普通的 Java 项目,带有类似
的 pom <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.DemoApplication</start-class>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
当我打
spring run src/main/java/demo/DemoApplication.java
它用
之类的错误消息将我踢出局 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'propertySourceBootstrapConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.propertySourceLocators; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configServicePropertySource' defined in class org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration$PropertySourceLocatorConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.config.client.ConfigServicePropertySourceLocator]: Factory method 'configServicePropertySource' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/web/client/HttpServerErrorException
我无法解释。 我做错了什么?
这个问题的标签之一是 spring-mvc,因此我假设您想要使用 Web Spring 功能。 要启用 Spring Web 功能,您需要添加此 Spring 将此依赖项引导到 POM 中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
我以前没有使用过Spring CLI,所以我不明白为什么它没有生成带有Web 依赖项的项目。但我敢打赌,您需要以某种方式明确指定要生成 Web 项目。
所以您可能生成了没有 Web 依赖项的纯 Spring 引导项目,并在 application.properties 中添加了一些与 Web 相关的配置。因此 Spring 引导自动配置器正在尝试在您的类路径中查找 Web 依赖项。
尽管这已经有了答案,但同样的问题可能会随着 maven 依赖项的损坏而出现。因此,任何无法解决相同问题的人都可以尝试从
中删除工件(或完整的本地存储库)c:\Users\<username>\.m2\repository
手动或尝试 运行 mvn dependency:purge-local-repository
并重新加载 IDE 中的项目。