本文主要是介绍springboot的controller中如何获取applicatim.yml的配置值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《springboot的controller中如何获取applicatim.yml的配置值》本文介绍了在SpringBoot的Controller中获取application.yml配置值的四种方式,...
在Spring Boot的Controller中获取application.yml配置值,主要有以下几种方式:
1. 使用@Value注解(最常用)
application.yml 配置
app: name: "我的应用" version: "1.0.0" max-users: 100 enabled: true timeout: 30000
Controller 中使用
@RestController
@RequestMapping("/api")
public class ConfigController {
@Value("${app.name}")
private String appName;
@Value("${app.version}")
private String appVersion;
@Value("${app.max-users}")
private int maxUsers;
@Value("${app.enabled}")
private boolean enabled;
@Value("${app.timeout}")
privphpate long timeout;
@GetMapping("/config")
public Map<String, Object> getConfig() {
Map<String, Object> config = new HashMap<>();
config.put("name", appName);
config.put("version", appVersion);
config.put("maxUsers", maxUsers);
config.put("enabled", enabled);
config.put("timeout", timeout);
return config;
}
}2. 使用@ConfigurationProperties(推荐用于复杂配置)
创建配置类
@Configuration
@ConfigurationProperties(prefix = "app")
@Data
public class AppConfig {
private String name;
private String version;
private int maxUsers;
private boolean enabled;
private long timeout;
private List<String> whitelist;
private Map<String, String> endpoints;
}在Controller中注入使用
@RestController
@RequestMapping("/api")
public class ConfigController {
@Autowired
private AppConfig appConfig;
@GetMapping("/config")
public AppConfig getConfig() {
return appConfig;
}
@GetMapping("/info")
public String getAppInfo() {
return appConfig.getName() + " v" + appConfig.getV编程ersion();
}
}3. 使用Environment接口
@RestController
@RequestMapping("/api")
public class ConfigController {
@Autowired
private Environment environment;
@GetMapping("/env-config")
public Map<String, Object> getEnvConfig() {
Map<String, Object> config = new HashMap<>();
config.put("name", environment.getProperty("app.name"));
config.put("version", environment.getProperty("app.version"));
config.put("maxUsers", environment.getProperty("app.max-users", Integer.class));
config.put("timeout", environment.getProperty("app.timeout", Long.class, 5000L));
return config;
}
}4. 复杂配置示例
application.yml
app: name: "用户管理系统" version: "2.1.0" database: url: "jdbc:mysql://localhost:3306/mydb" username: "admin" pool-size: 20 security: jwt-secret: "my-secret-key" token-expire: 3600 cors-origins: - "http://localhost:3000" - "https://example.com" features: enable-cache: true enable-notification: false
对应的配置类
@Configuration
@ConfigurationProperties(prefix = "app")
@Data
public class AppConfig {
private String name;
private String version;
private DatabaseConfig database;
private SecurityConfig security;
private FeaturesConfig features;
@Data
public static class DatabaseConfig {
private String url;
private String username;
private int poolSize;
}
@Data
public static class SecurityConfig {
private Striphpng jwtSecret;
private int tokenExpire;
private List<String> corsOrigins;
}
@Data
public static class FeaturesConfig {
private boolean enableCache;
private boolean enableNotification;
}
}Controller中使用
@RestController
@RequestMapping("/api")
public class SystemController {
@Autowired
javascriptprivate AppConfig appConfig;
@GetMapping("/system-info")
public Map<String, Object> getSystemInfo() {
return Map.of(
"appName", appConfig.getName(),
"databaseUrl", appConfig.getDatabase().getUrl(),
"corsOrigins", appConfig.getSecurity().getCorsOrigins(),
"enableCache", appConfig.getFeatures().isEnableCache()
);
}
}5. 带默认值的配置
@RestController
@RequestMapping("/api")
public class ConfigController {
// 使用默认值
@Value("${app.unknown-property:default-value}")
private String unknownProperty;
@Value("${app.retry-count:3}")
private int retryCount;
@GetMapping("/default-values")
public Map<String, Object> getWithDefaults() {
return Map.of(
"unknownProperty", unknownProperty,
"retryCount", retryCount
);
}
}主要区别和选择建议
方式 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
| 简单的单个配置项 | 简单直接 | 配置分散,不易管理 |
| 复杂的配置组 | 类型安全,集中管理 | 需要创建配置类 |
| 动态获取配置 | 灵活,可动态获取 | 类型转换需要手动处理 |
推荐使用 @ConfigurationProperties,特别是当配置项较多或有关联时,这样代码更清晰、易于维护。
到此这篇关于springboot的controller中如何获取applicatim.yml的配置值的文章就介绍到这了,更多相关springboot controller applicatim.yml配置值内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程China编程(www.chinasem.cn)!
这篇关于springboot的controller中如何获取applicatim.yml的配置值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!