我從一個簡單的 Spring Boot 啟動專案開始。
我改名application.properties為application.yml
在application.yml檔案中,我只添加了簡單的代碼:
test:
name: myAppName
我創建了一個控制器并添加了這段測驗代碼:
@RestController
public class Controller {
@Value("${test.name}")
private String name;
@GetMapping("/test")
public String test(){
return name;
}
}
從郵遞員那里打來的,一切都很好。
然后,我添加模塊命名integration是由兩個以上的模塊user-service和communication-service
當我運行應用程式后,我會收到此錯誤:
Could not resolve placeholder 'test.name' in value "${test.name}"
父級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>
<modules>
<module>integration</module>
<module>integration/user-service</module>
<module>integration/communication-service</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.w</groupId>
<artifactId>registration-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>registration-service</name>
<description>Registration service</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.w</groupId>
<artifactId>user-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.w</groupId>
<artifactId>communication-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Integration模塊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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>registration-service</artifactId>
<groupId>com.w</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>integration</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
而user-service模塊POM是內部integration模塊如下所示:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>registration-service</artifactId>
<groupId>com.wevolv</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-service</artifactId>
<groupId>com.w</groupId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
我不確定我在這里遺漏了什么以及為什么我無法再讀取 application.yml 檔案。
uj5u.com熱心網友回復:
您的父 pom 屬于pom型別,并且不處理舞臺中src/main/resourcesmaven 資源插件下的資源process-resources。
您應該src/main/resources/application.yml從專案根目錄移動到integration/user-service/src/main/resources或integration/communication-service/src/main/resources
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/361312.html
上一篇:SVG陰影不重復元素輪廓
