![](/img/trans.png)
[英]@value in spring boot not giving value from application.properties
[英]Spring Boot project not binding @Value from application.properties
I am in the process of migrating an existing multi-module maven project to spring boot 2.6.1 and I've populated application.properties in the resources folder of one of the modules (the one with the SpringBootApplication entry point and with the spring boot maven 插件)。 我可以通過應用程序環境從項目中的任何模塊訪問這些屬性。 但是使用 @Value 綁定屬性不起作用
以下是我的項目結構
PROJ
└── pom.xml
└── ModuleA
└── src
└── main
└── java
└── com.example
└── abc
└── configuration
└── TestConfig.java
└── MyApplication.java
└── resources
└── application.properties
└── pom.xml
└── ModuleB
為什么@ConfigurationProperties 或@Value 的屬性綁定會失敗? (我正在嘗試使用 @ConfigurationProperties 將所有數據源屬性綁定到 ModuleB 中的 DataSourceProperties bean,如文檔中所述- 但這也不起作用。)
我嘗試過的事情:
編碼:
pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>PROJ</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>PROJ</name>
<modules>
<module>ModuleA</module>
<module>ModuleB</module>
<!-- Other modules -->
</modules>
<profiles>
<profile>
<id>dev</id>
<properties>
<develop.mode>true</develop.mode>
</properties>
</profile>
<profile>
<id>prod</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<develop.mode>false</develop.mode>
</properties>
</profile>
</profiles>
<properties>
<!-- Project Properties -->
</properties>
<dependencyManagement>
<dependencies>
<!-- In House Modules -->
<dependency>
<groupId>com.example</groupId>
<artifactId>ModuleB</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>ModuleA</artifactId>
<version>1.0</version>
</dependency>
<!-- Spring -->
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>${spring-plugin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin</artifactId>
<version>${spring-plugin.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-saml2-service-provider</artifactId>
<version>5.5.0-BETA</version>
<classifier>BETA</classifier>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth</artifactId>
<version>${spring-security-oauth.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>${spring-security-oauth.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Various other dependencies -->
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<!-- Various plugins -->
</plugins>
</pluginManagement>
</build>
</project>
模塊A/pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>PROJ</artifactId>
<groupId>com.example</groupId>
<version>1.0</version>
</parent>
<artifactId>ModuleA</artifactId>
<packaging>jar</packaging>
<name>ModuleA</name>
<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>
</dependency>
<!-- In House Modules -->
<dependency>
<groupId>com.example</groupId>
<artifactId>ModuleB</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
應用程序屬性
# My datasource properties
app.datasource.myds.username=TestName
MyApplication.java
@SpringBootApplication
public class WSMBootApp {
public static void main(String[] args) {
SpringApplication.run(WSMBootApp.class, args);
}
}
TestConfig.java
@Configuration
public class TestConfig {
@Value("${app.datasource.myds.username}")
public String un;
@Bean(name = "testbean1")
@Primary
public Boolean testbean1() {
System.out.println("Value un: " + un); // prints null
return Boolean.FALSE;
}
@Bean(name = "testbean2")
public Boolean testbean2(Environment env) {
System.out.println("Env property: " + env.getProperty("app.datasource.myds.username")); // prints TestName
return Boolean.TRUE;
}
}
我找到了 Spring Boot 沒有使用 @Value 或 @ConfigurationProperties 自動裝配我的屬性的原因。事實證明,我嘗試遷移的項目實現了一個無法正確初始化屬性的 PropertyPlaceholderConfigurer bean。 我已經取消了那個 bean,應用程序現在正在加載和自動裝配來自 application.properties 的所有屬性(來自所有模塊)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.