【问题标题】:Error on Spring Boot App Startup when adding spring-boot-starter-web添加 spring-boot-starter-web 时 Spring Boot App Startup 出错
【发布时间】:2019-11-14 01:53:40
【问题描述】:

使用spring-cloud-starter-aws-secrets-manager-config 启动一个非常基本的Spring boot 应用程序来检索AWS sectrets 可以正常工作。但是,当我将spring-boot-starter-web dependency 添加到项目中以获得Web 服务支持时,the spring-cloud-starter-aws-secrets-manager-config errors 并且应用程序无法启动。

pom.xml

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
<!-- if I remove spring-boot-starter-web it all starts properly -->
                <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

java

package com.secrets.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class App
{

    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }

}

bootstrap.yml

spring:
  application:
    name: app_name
aws:
  secretsmanager:
    prefix: /secret
    defaultContext: application
    profileSeparator: _
    failFast: true
    name: app_name
    enabled: true

应用程序应该可以正常启动,但是应用程序出现以下错误:

启动 ApplicationContext 时出错。显示自动配置 报告启用“调试”后重新运行您的应用程序。 2019-07-03 11:57:16.563 错误 73839 --- [主要] os.s.boot.SpringApplication : 应用启动失败

org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“propertySourceBootstrapConfiguration”的 bean 时出错: 通过字段表示的不满足的依赖关系 'propertySourceLocators';嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“awsSecretsManagerPropertySourceLocator”的 bean 时出错 定义在 org.springframework.cloud.aws.autoconfigure.secretsmanager.AwsSecretsManagerBootstrapConfiguration: 通过方法表达的不满足的依赖关系 “awsSecretsManagerPropertySourceLocator”参数 1;嵌套异常 是 org.springframework.beans.factory.BeanCreationException: 错误 用名字创建bean 'aws.secretsmanager-org.springframework.cloud.aws.secretsmanager.AwsSecretsManagerProperties': 无法将属性绑定到 AwsSecretsManagerProperties (前缀=aws.secretsmanager,ignoreInvalidFields=false, ignoreUnknownFields=true,ignoreNestedProperties=false);嵌套的 异常是 java.lang.NullPointerException 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:187) [spring-cloud-context-1.3.2.RELEASE.jar:1.3.2.RELEASE] 在 org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:102) [spring-cloud-context-1.3.2.RELEASE.jar:1.3.2.RELEASE] 在 org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:68) [spring-cloud-context-1.3.2.RELEASE.jar:1.3.2.RELEASE] 在 org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:296) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 com.secrets.demo.App.main(App.java:13) [classes/:na]

【问题讨论】:

  • 您可以添加一个示例控制器并使用 @RestController 进行注释吗?比尝试

标签: java spring-boot spring-cloud aws-secrets-manager


【解决方案1】:

您正在混合 1.5.x 和 2.x 依赖项。由于这两个版本之间存在重大更改,因此在同一项目中使用两者时经常会出现兼容性问题。

由于 spring-cloud-starter-aws-secrets-manager-config 仅支持 2.x,您需要将 spring boot starter parent 升级到 2.x 版本。

【讨论】:

    【解决方案2】:

    @Michael McFadyen 的回复是正确的;但是同样值得注意的是,手动添加 Spring Cloud 依赖项及其版本并不是一个好习惯。应该使用 dependencyManagement 插件和 Spring Cloud Release trains 来确保使用的所有 Spring Cloud 库都是兼容版本,请参阅project page 中的“Release Trains”部分。它还包含有关哪些发布火车对应于哪些 Spring Boot 版本的信息。

    如果您想确保在构建文件中正确设置 Spring 依赖项,可以转到 start.spring.io 以生成具有正确构建文件的项目存根。

    【讨论】:

    • 谢谢 - 这很好,也很有意义。
    • 谢谢 - 这很好,也很有意义 - 一切正常运行,但是作为另一个方面,我对这个库的使用感到困惑。有限的文档指出“Spring Cloud 提供对集中式配置的支持,可以在应用程序启动时作为常规 Spring PropertySource 读取并提供该配置。”我期待看到 AWS 机密可用作属性,以便能够使用${prop_name} 类型语法。我看不到任何 AWS 机密是自动读取的....我误解了吗?
    猜你喜欢
    • 2015-12-28
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2017-02-19
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    相关资源
    最近更新 更多