Spring Boot如何根据传入参数读取指定配置属性?
嘿,这个需求我之前做类似功能时也遇到过,完全不用数据库就能搞定,Spring Boot本身就提供了很便捷的方式来处理这种自定义配置文件的读取,给你两个实用的方案:
方案一:绑定到实体类(推荐,类型安全)
这种方式把配置文件里的内容映射成Java实体类,用起来非常方便,而且能避免硬编码配置键名的问题。
步骤1:调整配置文件(可选,让结构更清晰)
你的Configration_Types.properties当前的写法也能兼容,但为了更贴合Spring Boot的配置绑定规则,建议把结构调整成这样(如果不想改原文件也没关系,后面会说明):
types.Type1.name=Type1 types.Type1.width=60 types.Type1.height=715 types.Type2.name=Type2 types.Type2.width=100 types.Type2.height=720 types.Type3.name=Type3 types.Type3.width=100 types.Type3.height=700 # ... 其他Type配置
注:你原配置里的
heght是拼写错误,建议改成height,否则读取时会找不到对应属性哦。
步骤2:创建配置实体类
写一个配置类,用@ConfigurationProperties把配置文件里的内容绑定进来:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Map; @Component // 如果你的配置文件不在resources根目录,这里要写对路径,比如classpath:config/Configration_Types.properties // @PropertySource("classpath:Configration_Types.properties") @ConfigurationProperties(prefix = "types") public class TypeConfig { // key是Type1、Type2这些类型标识,value是对应的属性 private Map<String, TypeProperties> types; // Getter和Setter public Map<String, TypeProperties> getTypes() { return types; } public void setTypes(Map<String, TypeProperties> types) { this.types = types; } // 内部类封装每个Type的属性 public static class TypeProperties { private String name; private Integer width; private Integer height; // Getter和Setter public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getWidth() { return width; } public void setWidth(Integer width) { this.width = width; } public Integer getHeight() { return height; } public void setHeight(Integer height) { this.height = height; } } }
如果你不想修改原配置文件的结构,只需要把
@ConfigurationProperties(prefix = "types")去掉,然后把Map的key直接对应原配置里的Type1、Type2,同时把内部类的name属性改成value(因为原配置里Type1=Type1对应的键是Type1,值会映射到value属性)。
步骤3:在业务代码中使用
在你需要读取配置的Service或者Controller里注入这个配置类,然后根据传入的参数直接获取对应配置:
import org.springframework.stereotype.Service; @Service public class TypeService { private final TypeConfig typeConfig; // 构造方法注入(推荐) public TypeService(TypeConfig typeConfig) { this.typeConfig = typeConfig; } public TypeConfig.TypeProperties getTypeConfig(String typeParam) { // 根据传入的参数(比如"Type1")获取对应配置 return typeConfig.getTypes().get(typeParam); } }
调用这个方法时,传入"Type2"就能拿到width=100、height=720的配置了。
方案二:直接用Environment读取(适合简单场景)
如果你的需求很简单,不想写实体类,也可以直接注入Environment来读取配置:
import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; @Service public class TypeService { private final Environment environment; public TypeService(Environment environment) { this.environment = environment; } public void getTypeConfig(String typeParam) { // 拼接配置键名,读取对应属性 String name = environment.getProperty(typeParam); Integer width = environment.getProperty(typeParam + ".width", Integer.class); Integer height = environment.getProperty(typeParam + ".height", Integer.class); // 这里可以把这些值封装成对象或者直接使用 } }
这种方式的优点是快速实现,但缺点是需要硬编码配置键的拼接规则,而且没有类型安全的校验,适合临时的简单场景。
额外小提示
- 如果你的Spring Boot版本是2.4及以上,还可以不用
@PropertySource,直接在application.properties里添加一行配置来导入自定义配置文件:spring.config.import=classpath:Configration_Types.properties - 记得检查配置文件的路径是否正确,确保它在项目的
resources目录下(或者你指定的路径)。
内容的提问来源于stack exchange,提问作者user1122

