@Configuration @ConfigurationProperties(prefix = "myapp") public class MyAppProperties { private String name; private String version; // getters and setters }在上述示例中,@ConfigurationProperties 注解将以 myapp 前缀开头的属性映射到 MyAppProperties 类的字段中。
@Configuration @PropertySources({ @PropertySource("classpath:application.properties"), @PropertySource("classpath:custom.properties") }) public class AppConfig { // 这里可以注入和使用从属性文件加载的配置属性 }在上述示例中,@PropertySources 注解指定了两个属性源,分别是 application.properties 和 custom.properties。
@PropertySources 主要用于指定多个属性源,以在 Spring 应用程序中加载配置属性,适用于标准的 Spring 应用程序。
@ConfigurationProperties(prefix = "myapp") public class MyAppProperties { private String name; private String version; // 堆代码 duidaima.com // 省略构造函数、getter 和 setter 方法 }在上面的示例中,@ConfigurationProperties 注解指定了一个前缀 "myapp",这意味着配置文件中的属性键应以 "myapp" 开头。例如,myapp.name 和 myapp.version。
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; @Configuration @EnableConfigurationProperties(MyAppProperties.class) public class AppConfig { }@EnableConfigurationProperties 注解用于启用对 MyAppProperties 类的配置属性绑定。
myapp.name=My Spring Boot App myapp.version=1.0或者,使用 YAML 格式:
myapp: name: My Spring Boot App version: 1.0步骤 4: 注入配置属性
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MyService { private final MyAppProperties myAppProperties; @Autowired public MyService(MyAppProperties myAppProperties) { this.myAppProperties = myAppProperties; } public void doSomething() { String appName = myAppProperties.getName(); String appVersion = myAppProperties.getVersion(); // 使用配置属性进行操作 System.out.println("App Name: " + appName); System.out.println("App Version: " + appVersion); } }现在,您的 MyService Bean 可以访问通过 @ConfigurationProperties 注解绑定的配置属性。
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySources; @Configuration @PropertySources({ @PropertySource("classpath:default.properties"), // 默认属性源 @PropertySource("classpath:custom.properties") // 自定义属性源 }) public class AppConfig { }在上述示例中,我们在 AppConfig 类上使用 @PropertySources 注解,并指定了两个属性源,分别是 default.properties 和 custom.properties。这两个属性源位于类路径下,可以包含不同的配置属性。
app.name=My Spring Boot App (Default) app.version=1.0 (Default)示例 custom.properties 文件内容如下:
app.name=My Custom Spring Boot App创建一个服务类,例如 MyService.java,并使用 @Value 注解来注入属性。
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service public class MyService { @Value("${app.name}") private String appName; @Value("${app.version}") private String appVersion; public String getAppInfo() { return "App Name: " + appName + ", App Version: " + appVersion; } }在上述示例中,我们使用 @Value 注解来注入属性 app.name 和 app.version,这些属性的值将从属性源中获取。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { private final MyService myService; @Autowired public MyController(MyService myService) { this.myService = myService; } @GetMapping("/app-info") public String getAppInfo() { return myService.getAppInfo(); } }启动应用程序,并访问 /app-info 路径,您将看到根据不同属性源获取的应用程序信息。在这个示例中,我们使用了 @PropertySources 注解来指定多个属性源,并在服务类中使用 @Value 注解来注入属性。这使得应用程序能够根据属性源的不同而获取不同的属性值。
属性源优先级: 如果存在多个属性源,Spring 将按照它们在 @PropertySources 注解中的顺序来解析属性。这意味着后面的属性源可以覆盖前面的属性源中的属性值。
@ConfigurationProperties 主要用于将配置属性绑定到 Java Bean 上。它通常与 @Configuration 或 @Component 注解一起使用,以将外部属性文件中的属性值映射到一个特定的 Java Bean。
通常情况下,您会使用 @PropertySources 来定义属性源,然后使用 @ConfigurationProperties 来将属性源中的属性值绑定到 Java Bean,以便在整个应用程序中使用这些属性。这两个注解可以协同工作,但它们的功能不同,各自有其用途。