[英]Spring boot value injection
我創建了一個簡單的 spring 啟動應用程序,我想將字符串值設置為組件中的字段(用 @Component 注釋的類),但我得到一個異常
java.lang.IllegalArgumentException: Could not resolve placeholder 'rounding.strategy' in value "${rounding.strategy}"
我絕對確定application.properties
文件包含此值。 我還確定我的application.properties
文件位於正確的目錄( src/main/resources/application.properties
)中。
這就是我嘗試設置此值的方式:
@Value("${rounding.strategy}")
private String actualStrategyBeanName;
這就是我在application.properties
文件中設置它的方式:
rounding.strategy = sprbj_FiftyRoundingStrategy
build.gradle
plugins {
id 'org.springframework.boot' version '2.4.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
Intellij IDEA Ultimate 顯示注入值正確(請參見屏幕截圖在此處輸入圖像描述在此處輸入圖像描述),但 spring 無法將其設置為字段。 我做錯了什么?
編輯我嘗試使用@PropertySource("classpath:application.properties")
注釋注釋主 class ,然后我得到了
java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
嘗試將資源文件夾移出一層:
像這樣:
src/main/resources/application.properties
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.