当前位置:首页 > Java 框架原理百科 > 正文

Java优学网SpringBoot自定义配置教程:轻松掌握个性化配置技巧,告别繁琐开发

@ConfigurationProperties(prefix = "app.datasource") @Component public class DataSourceConfig {

private String url;
private String username;
private int maxPoolSize;

// 标准的getter和setter方法

}

Java优学网SpringBoot自定义配置教程:轻松掌握个性化配置技巧,告别繁琐开发

@ConfigurationProperties(prefix = "app.mail") @Component public class MailConfig {

private Server server;
private Template template;

// 静态内部类定义复杂对象
public static class Server {
    private String host;
    private int port;
    private String username;
    private String password;
    // getters and setters
}

public static class Template {
    private String welcomeSubject;
    private String resetPasswordBody;
    private boolean htmlEnabled;
    // getters and setters
}

// getters and setters

}

Java优学网SpringBoot自定义配置教程:轻松掌握个性化配置技巧,告别繁琐开发

spring: profiles:

active: dev  # 默认开发环境

app: name: "Java优学网" version: "1.0.0"

Java优学网SpringBoot自定义配置教程:轻松掌握个性化配置技巧,告别繁琐开发

@Configuration @ConditionalOnProperty(name = "app.feature.cache.enabled", havingValue = "true") public class CacheConfig {

@Bean
public CacheManager cacheManager() {
    return new RedisCacheManager();
}

}

app: database-config:

connection-timeout: 5000

spring: datasource:

hikari:
  maximum-pool-size: 20
  minimum-idle: 5
  connection-timeout: 30000
  idle-timeout: 600000
  max-lifetime: 1800000
  connection-test-query: SELECT 1

你可能想看:

相关文章:

  • Java优学网SpringBoot配置绑定解析:轻松掌握配置注入技巧,告别配置错误烦恼2025-10-23 23:12:16
  • 文章已关闭评论!