【发布时间】:2018-03-08 10:44:43
【问题描述】:
我正在尝试从 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 中,我正在尝试自动装配构建属性 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 bean 是在 build-info.properties 存在时自动创建的。好像不是这样的。
【问题讨论】:
-
@Autowired 私有 BuildProperties buildProperties;正在工作和实例化,如果“build-info”文件是由依赖管理框架生成的。
标签: spring spring-boot gradle autowired