![](/img/trans.png)
[英]Spring Boot - inject from application.yml and application.properties
[英]Java Spring Boot custom properties dont work in application.yml
我正在關注 Java Spring Boot 上的教程,當前主題是可以同時使用application.properties
和application.yml
所以我的application.properties
看起來像這樣:
spring.profiles.active=EN, cat
custom.username=user
custom.password=pass
我刪除了它並創建了一個application.yml
。 IntelliJ 甚至用綠色的開始按鈕小圖標來標記它。 application.yml
看起來像這樣:
spring:
profiles:
active: EN, cat
custom:
username: user
password: pass
但是當我執行custom
屬性時,不再被識別。 IDE 將它們標記為紅色並顯示此錯誤:“此處不期望密鑰‘自定義’”
我不確定那是否正確所以我嘗試了 IDE 在使用自動完成編寫spring.profiles.active
時建議的內容,它正在編寫這樣的列表元素:
spring:
profiles:
active:
- EN
- cat
custom:
username: user
password: pass
但這也沒有幫助。
我不確定從這里到 go 的哪里。 我試圖用谷歌搜索這個問題,但是我得到的唯一點擊率有點接近所有只是提到它可以使用yml
而不是properties
,有些甚至使用我上面顯示的自定義屬性。
這可能是版本問題嗎? 如果有幫助,我的 pom 看起來像這樣:
<?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 https://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>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>myID</groupId>
<artifactId>myArtifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myName</name>
<description>myDescription</description>
<properties>
<java.version>11</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
回答 Mark B 在評論中提出的問題。 這是錯誤消息:
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-01-05T08:58:07.711+01:00 ERROR 5026 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in my.package.controllers.I18nController required a bean of type 'my.package.services.GreetingService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Qualifier("i18nService")
The following candidates were found but could not be injected:
- User-defined bean
- User-defined bean
- User-defined bean method 'primaryGreetingService' in 'GreetingServiceConfig'
- User-defined bean method 'propertyInjectedGreetingService' in 'GreetingServiceConfig'
- User-defined bean method 'setterInjectedGreetingService' in 'GreetingServiceConfig'
- User-defined bean
Action:
Consider revisiting the entries above or defining a bean of type 'my.package.services.GreetingService' in your configuration.
Process finished with exit code 1
乍一看,這似乎是我在 bean 的定義或配置中弄亂了某些東西。 但是,當我將我的存儲庫恢復到 state 時,我如上所述使用了application.properties
文件,一切都按預期工作。
我重新采取了盡可能隔離問題的步驟。
我所做的只是:
application.properties
application.yml
我沒有改變任何其他東西。
On Branch master
Your Branch is in the same state as 'origin/master'.
Staged changes:
deleted: src/main/resources/application.properties
new file: src/main/resources/application.yml
Unstaged changes::
changed: src/main/resources/application.yml
src/main/resources/application.yml
作為新文件暫存,然后有未暫存的更改,因為 IDE 在創建時暫存新文件。 只是為了消除任何混亂。 雖然這不應該影響結果。
正如評論中所建議的那樣,我嘗試添加@ConfigurationProperties(prefix = "custom")
,但所做的只是稍微更改了錯誤:
Error creating bean with name 'i18nController' defined in file
[/path/target/classes/my/package/controllers/I18nController.class]:
Unsatisfied dependency expressed through constructor parameter 0:
No qualifying bean of type 'my.package.services.GreetingService' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations:
{@org.springframework.beans.factory.annotation.Qualifier("i18nService")}
當您使用 spring-boot-starter SnakeYAML 庫時,應該在您的類路徑中,但請仔細檢查mvn dependency:list
,在 output 中搜索一行[INFO] org.yaml:snakeyaml:jar:1.33:compile -- module org.yaml.snakeyaml [auto]
。
如果缺少,請添加 SnakeYAML 依賴: https://mvnrepository.com/artifact/org.yaml/snakeyaml
Eclipse IDE 具有將屬性轉換為 yaml 格式的自動轉換機制。 只需右鍵單擊該文件,然后單擊“轉換”,它就像一個魅力。 我使用的是 Eclipse STS 版本。
我猜您沒有共享完整的屬性文件,並且存在語法錯誤或 yaml 配置中缺少某些內容。 請嘗試使用適用於 InteliJ IDEA 的插件: https://plugins.jetbrains.com/plugin/8000-properties-to-yaml-converter或在線轉換器,例如: https://www.javainuse.com/app2yaml .
我誤解了問題的重點嗎?
我測試ok 我不能重復錯誤信息。
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── example
│ └── demo
│ ├── DemoApplication.java
│ └── Hello.java
└── resources
└── application.yaml
spring:
profiles:
active:
- EN
- cat
custom:
username: userAAA
password: passBBB
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
@RestController
@Configuration
@ConfigurationProperties(prefix = "custom")
public class Hello {
private String username;
private String password;
@RequestMapping("/hello")
public String hello() {
return "OK - ****>>>" + getUsername()+ "<<<****>>>>" + getPassword()+"<<<****";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<?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 https://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>2.7.6</version>-->
<!--<version>3.0.1</version>-->
<version>2.2.2.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>17</java.version>-->
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
mvn clean package spring-boot:run
$ curl http://localhost:8080/hello
OK - ****>>>userAAA<<<****>>>>passBBB<<<****
我不添加這個,也許你可以將它添加到你的 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.