简体   繁体   English

Spring Boot无法正常工作?

[英]Spring Boot not working?

I have generate a standard spring boot sample using command 我已经使用命令生成了标准的Spring Boot示例

spring init

The outcome is a plain Java project with a pom like 结果是一个简单的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>

When I hit 当我打

spring run src/main/java/demo/DemoApplication.java

It kicks me out with an error message such as 它以错误消息将我踢了出去,例如

 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

That I cannot explain. 我无法解释。 What am I doing wrong? 我究竟做错了什么?

One of tags on this question is spring-mvc, so I assume your want to use Web Spring features. 这个问题的标签之一是spring-mvc,所以我假设您想使用Web Spring功能。 To enable Spring web features you need to add this Spring Boot this dependency into POM: 要启用Spring Web功能,您需要将此Spring Boot这个依赖项添加到POM中:

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

I haven't use Spring CLI before, therefore I am not familiar why it didn't generate project with Web dependencies. 我以前没有使用过Spring CLI,因此我不熟悉为什么它没有生成具有Web依赖关系的项目。 But I would bet you need to explicitly specify somehow that to want to generate web project. 但是我敢打赌,您需要明确指定要生成Web项目的方式。

So you probably generated plain Spring Boot project without web dependencies and added some web related configuration into application.properties. 因此,您可能会生成没有Web依赖项的纯Spring Boot项目,并将一些与Web相关的配置添加到application.properties中。 Therefore Spring Boot auto configurer is trying to find web dependencies on your classpath. 因此,Spring Boot自动配置器试图在类路径上查找Web依赖项。

Even though this already have an answer, this same issue can come up with corrupted maven dependencies. 即使已经有了答案,但同样的问题也可能导致损坏的Maven依赖项。 So anyone who could not solve the same issue try deleting the artifacts (or the full local repo) from 因此,任何无法解决同一问题的人都可以尝试从中删除工件(或完整的本地存储库)

c:\\Users\\<username>\\.m2\\repository by hand or try running mvn dependency:purge-local-repository c:\\Users\\<username>\\.m2\\repository或尝试运行mvn dependency:purge-local-repository

and reload the projects in the IDE. 并在IDE中重新加载项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM