gpt4 book ai didi

spring - 从 Gradle Autowiring BuildProperties bean - NoSuchBeanDefinitionException

转载 作者:行者123 更新时间:2023-12-03 13:22:28 25 4
gpt4 key购买 nike

我正在尝试从 Gradle 构建文件中获取我的 Java 应用程序中的版本。我正在按照此处的说明进行操作;

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-build.html

build.gradle

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-
plugin:1.5.7.RELEASE")
}
}

project.version = '0.1.0'

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

springBoot {
buildInfo()
}

jar {
baseName = 'ci-backend'
}

war {
baseName = 'ci-backend'
}

repositories {
mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework:spring-jdbc")
compile("joda-time:joda-time")

compile("com.opencsv:opencsv:3.9")
compile("org.springframework.batch:spring-batch-core")

testCompile('org.springframework.boot:spring-boot-starter-test')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
}

使用 gradle 构建后 build-info.properties文件存在于 build/resources/main/META-INF/build-info.properties

在我的 @RestController我正在尝试 Autowiring 构建属性 bean
@Autowired
private BuildProperties buildProperties;

我收到以下错误;
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.info.BuildProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 41 more

我会假设 BuildProperties存在 build-info.properties 时会自动创建 bean。似乎并非如此。

最佳答案

对于 Maven 项目,在 IntelliJ“首选项...”中,在构建、执行、部署 > 构建工具 > Maven > Runner 下,选择选项“将 IDE 构建/运行操作委托(delegate)给 Maven”。

关于spring - 从 Gradle Autowiring BuildProperties bean - NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46439505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com