好的,我想通了:)
此外,需要强调的是,要使用构造函数绑定,我们需要使用@EnableConfigurationProperties 或@ConfigurationPropertiesScan 显式启用我们的配置类。
下面使用 SpringBoot 2.5.4 和 Java 17 的工作示例:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
@SpringBootApplication
@ConfigurationPropertiesScan
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.boot.context.properties.ConstructorBinding;
@ConfigurationProperties("cache")
@ConstructorBinding
public record ExampleRecordConfig(int validity) {
}
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ServiceWhichUsesConfig {
@Autowired
private ExampleRecordConfig exampleRecordConfig;
public void justChecking(){
System.out.println(exampleRecordConfig.validity());
}
}
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private ServiceWhichUsesConfig serviceWhichUsesConfig;
@Test
void contextLoads() {
serviceWhichUsesConfig.justChecking();
}
}
<?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.5.4</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>
</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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
是什么使它起作用:@ConfigurationPropertiesScan
=== 已编辑 ===
只是为了好玩,回答你关于默认值的问题。您可以通过以下方式实现它:
这样做的缺点是您默认使用代码,而不是配置。
@ConfigurationProperties("cache")
@ConstructorBinding
public record ExampleRecordConfig(Integer validity) {
public Integer validity(){ <-- notice the name..it cant be getValidity()
if(validity != null)
return validity;
return 500;
}
}
如果您根本没有cache.validity
在属性文件中定义,那么它将返回500
对于配置文件默认值,您需要以不同的方式进行:
somePropertyEitherFromEnvironmentOrAlreadyDefinedUpInProperitiesFile=5
server.port=8060
cache.validity=${somePropertyEitherFromEnvironmentOrAlreadyDefinedUpInProperitiesFile:5000000}
然后它会打印5
,因为 firstsomePropertyEitherFromEnvironmentOrAlreadyDefinedUpInPrope
有一个值。
server.port=8060
cache.validity=${somePropertyEitherFromEnvironmentOrAlreadyDefinedUpInProperitiesFile:5000000}
然后它会打印5000000
哪个是默认值,如果没有somePropertyEitherFromEnvironmentOrAlreadyDefinedUpInPrope
定义;