Jag*_*ags 17 spring autowired gradle spring-boot
我试图从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')
}
Run Code Online (Sandbox Code Playgroud)
使用gradle构建后,该build-info.properties文件存在于build/resources/main/META-INF/build-info.properties中
在我,@RestController我试图自动装配构建属性bean
@Autowired
private BuildProperties buildProperties;
Run Code Online (Sandbox Code Playgroud)
我收到以下错误;
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
Run Code Online (Sandbox Code Playgroud)
我假设BuildProperties在build-info.properties存在时自动创建bean.似乎并非如此.
Bri*_*con 17
我遇到的问题可能有所不同,但是我在这里尝试搜索google解决方案,因此,如果有人遇到相同的问题,我将在此处发布。我的错误消息是:
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.info.BuildProperties' available
仅在尝试在IntelliJ中运行时,而不是从命令行使用gradle运行时。(这可能是特定于SpringBoot的)
我只需要从“构建,执行,部署->构建工具-> Gradle”中设置“委托IDE进行构建/运行操作”,然后在从IDE进行构建时让“ bootBuildInfo”任务运行。
izi*_*tti 15
对于 Maven 项目,在 IntelliJ“首选项...”中,在构建、执行、部署 > 构建工具 > Maven > Runner 下,选择选项“将 IDE 构建/运行操作委托给 Maven”。
小智 8
您需要将Intelij配置为使用Gradle Runner,以便它在out文件夹中生成build.properties文件。在“设置”(“首选项”)|“ 将代理IDE构建/运行操作启用到Gradle”选项中启用| 构建,执行,部署| 生成工具| 摇篮| 跑步者标签。
您的假设是正确的,BuildProperties当META-INF/build-info.properties存在时会自动创建Bean。
请参阅以下自动配置代码 ProjectInfoAutoConfiguration
@ConditionalOnResource(
resources = {"${spring.info.build.location:classpath:META-INF/build-info.properties}"}
)
@ConditionalOnMissingBean
@Bean
public BuildProperties buildProperties() throws Exception {
return new BuildProperties(this.loadFrom(this.properties.getBuild().getLocation(), "build"));
}
Run Code Online (Sandbox Code Playgroud)
但是,如果该bean在您的应用程序上下文中仍然不可用,请尝试以下任一方法:
buildInfogradle任务配置正确,运行gradlew bootBuildInfo --debug并验证结果out目录(在我的情况下该build-info.properties文件不存在)升级gradle插件,也许您遇到了这个问题https://github.com/spring-projects/spring-boot/issues/12266,如果您不想等待补丁,可以尝试使用以下hack即将面世
def removeBootBuildInfoWorkaround12266 = task(type: Delete, 'removeBootBuildInfoWorkaround12266') {
delete new File(buildDir, 'resources/main/META-INF/build-info.properties')
}
tasks.find { it.name == 'bootBuildInfo' }.dependsOn(removeBootBuildInfoWorkaround12266)
Run Code Online (Sandbox Code Playgroud)希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
7448 次 |
| 最近记录: |