【记录版】SpringBoot常见配置项及其配置文件(持续更新...)

发布时间:2023年12月20日

主题:SpringBoot + Configuration

背景: SpringBoot作为整合框架,涉及到非常多的模块的配置,有时需要知道对应模块有哪些配置,做一些针对性的优化,但往往忘记其配置解析入口在哪。本篇仅做个人记录用,加强自身记忆与快速查找。

一、常见配置列表

一、server. 服务器相关配置项

1)位置:org.springframework.boot.autoconfigure.web.ServerProperties
2)核心配置:port、address、servlet、reactive、tomcat、netty
3)顶层类代码示例:

@ConfigurationProperties(
    prefix = "server",
    ignoreUnknownFields = true
)
public class ServerProperties {
    private Integer port;
    private InetAddress address;
    @NestedConfigurationProperty
    private final ErrorProperties error = new ErrorProperties();
    private ForwardHeadersStrategy forwardHeadersStrategy;
    private String serverHeader;
    private DataSize maxHttpHeaderSize = DataSize.ofKilobytes(8L);
    private Shutdown shutdown;
    @NestedConfigurationProperty
    private Ssl ssl;
    @NestedConfigurationProperty
    private final Compression compression;
    @NestedConfigurationProperty
    private final Http2 http2;
    private final Servlet servlet;
    private final Reactive reactive;
    private final Tomcat tomcat;
    private final Jetty jetty;
    private final Netty netty;
    private final Undertow undertow;
}

二、spring.web类配置

1)位置:org.springframework.boot.autoconfigure.web.WebProperties
2)核心配置:resources
3)顶层类代码示例:

@ConfigurationProperties("spring.web")
public class WebProperties {
    private Locale locale;
    private LocaleResolver localeResolver;
    private final Resources resources;
}

三、spring.mvc类配置

1)位置:org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties
2)核心配置:staticPathPattern、publishRequestHandledEvents // 接口请求响应全局日志开关【追加】
3)已知使用位置:DispatcherServletAutoConfiguration、WebMvcAutoConfiguration
4)spring.mvc.formcontent.filter=boolean[d:true] //单独使用
5)顶层类代码示例:

@ConfigurationProperties(
    prefix = "spring.mvc"
)
public class WebMvcProperties {
    private DefaultMessageCodesResolver.Format messageCodesResolverFormat;
    private final Format format = new Format();
    private boolean dispatchTraceRequest = false;
    private boolean dispatchOptionsRequest = true;
    private boolean ignoreDefaultModelOnRedirect = true;
    private boolean publishRequestHandledEvents = true;
    private boolean throwExceptionIfNoHandlerFound = false;
    private boolean logRequestDetails;
    private boolean logResolvedException = false;
    private String staticPathPattern = "/**";
    private final Async async = new Async();
    private final Servlet servlet = new Servlet();
    private final View view = new View();
    private final Contentnegotiation contentnegotiation = new Contentnegotiation();
    private final Pathmatch pathmatch = new Pathmatch();
}

四、spring.servlet.multipart类配置

1)位置:org.springframework.boot.autoconfigure.web.servlet.MultipartProperties(等同@MultipartConfig)
2)核心配置:各文件指标【DataSize封装后可直接设置单位
3)顶层类代码示例:

@ConfigurationProperties(
    prefix = "spring.servlet.multipart",
    ignoreUnknownFields = false
)
public class MultipartProperties {
    private boolean enabled = true;
    private String location;
    private DataSize maxFileSize = DataSize.ofMegabytes(1L);
    private DataSize maxRequestSize = DataSize.ofMegabytes(10L);
    private DataSize fileSizeThreshold = DataSize.ofBytes(0L);
    private boolean resolveLazily = false;
}

五、session会话配置

一、spring.session全局控制

1)位置:org.springframework.boot.autoconfigure.session.SessionProperties
2)核心配置:storeType //存储类型; timeout //保存时间【秒】; Servlet //核心为Filter顺序
3)顶层类代码示例:

@ConfigurationProperties(
    prefix = "spring.session"
)
public class SessionProperties {
    private StoreType storeType;
    @DurationUnit(ChronoUnit.SECONDS)
    private Duration timeout;
    // 与spring-security配置类似,都是设置Filter的执行顺序
    private Servlet servlet = new Servlet();
}

public enum StoreType {
    REDIS,
    MONGODB,
    JDBC,
    HAZELCAST,
    NONE;
}

二、spring.session.redis场景控制

1)位置:org.springframework.boot.autoconfigure.session.RedisSessionProperties
2)核心配置:namespace //命名空间,一定修改
3)JdbcSessionConfiguration、MongoSessionProperties、HazelcastSessionProperties同理
4)顶层类代码示例:

@ConfigurationProperties(
    prefix = "spring.session.redis"
)
public class RedisSessionProperties {
	// 每分钟执行一次
    private static final String DEFAULT_CLEANUP_CRON = "0 * * * * *";
    private String namespace = "spring:session";
    private FlushMode flushMode = FlushMode.ON_SAVE;
    private SaveMode saveMode = SaveMode.ON_SET_ATTRIBUTE;
    private ConfigureAction configureAction;
    private String cleanupCron;
}

六、spring.datasource数据源配置

1)位置:org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
2)核心配置:username + password + url(可推出driverClassName)
3)url可加参数:useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true
4)type:数据源类型,如:com.alibaba.druid.pool.DruidDataSource
5)顶层类代码示例:

@ConfigurationProperties(
    prefix = "spring.datasource"
)
public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {
    private ClassLoader classLoader;
    private boolean generateUniqueName = true;
    private String name;
    private Class<? extends DataSource> type;
    private String driverClassName;
    private String url;
    private String username;
    private String password;
    private String jndiName;
    private EmbeddedDatabaseConnection embeddedDatabaseConnection; // 未知
    private Xa xa = new Xa();
    private String uniqueName;
}

public static class Xa {
   private String dataSourceClassName;
   private Map<String, String> properties = new LinkedHashMap();
}

七、spring.cache缓存配置

1)位置:org.springframework.boot.autoconfigure.cache.CacheProperties
2)核心配置:cacheNames、redis
3)顶层类代码示例:

@ConfigurationProperties(
    prefix = "spring.cache"
)
public class CacheProperties {
    private CacheType type;
    private List<String> cacheNames = new ArrayList();
    private final Caffeine caffeine = new Caffeine();
    private final Couchbase couchbase = new Couchbase();
    private final EhCache ehcache = new EhCache();
    private final Infinispan infinispan = new Infinispan();
    private final JCache jcache = new JCache();
    private final Redis redis = new Redis();
}

public enum CacheType {
    GENERIC,
    JCACHE,
    EHCACHE,
    HAZELCAST,
    INFINISPAN,
    COUCHBASE,
    REDIS,
    CACHE2K,
    CAFFEINE,
    SIMPLE,
    NONE;
}

public static class Redis {
    private Duration timeToLive;
    private boolean cacheNullValues = true;
    private String keyPrefix;
    private boolean useKeyPrefix = true;
    private boolean enableStatistics;
}

八、spring.task.execution线程池配置

1)位置:org.springframework.boot.autoconfigure.task.TaskExecutionProperties
2)核心配置:pool // 线程池配置、threadNamePrefix // 线程名前缀
3)默认线程池Bean名:applicationTaskExecutor&taskExecutor
4)实现TaskExecutorCustomizer,并设置类似拒绝处理策略,可多个
5)顶层类代码示例:

@ConfigurationProperties("spring.task.execution")
public class TaskExecutionProperties {
    private final Pool pool = new Pool();
    private final Shutdown shutdown = new Shutdown();
    private String threadNamePrefix = "task-";
}

public static class Pool {
    private int queueCapacity = Integer.MAX_VALUE;
    private int coreSize = 8;
    private int maxSize = Integer.MAX_VALUE;
    private boolean allowCoreThreadTimeout = true;
    private Duration keepAlive = Duration.ofSeconds(60L);
}

九、spring.task.scheduling定时任务线程池配置

1)位置:org.springframework.boot.autoconfigure.task.TaskSchedulingProperties
2)核心配置:pool // 仅配置线程数、threadNamePrefix // 线程名前缀
3)顶层类代码示例:

@ConfigurationProperties("spring.task.scheduling")
public class TaskSchedulingProperties {
    private final Pool pool = new Pool();
    private final Shutdown shutdown = new Shutdown();
    private String threadNamePrefix = "scheduling-";
}

public static class Pool {
    private int size = 1;
}

二、冷门配置列表

一、spring.quartz类配置

1)位置:org.springframework.boot.autoconfigure.quartz.QuartzProperties
2)核心配置:未使用
3)顶层类代码示例:

@ConfigurationProperties("spring.quartz")
public class QuartzProperties {
    private JobStoreType jobStoreType;
    private String schedulerName;
    private boolean autoStartup;
    private Duration startupDelay;
    private boolean waitForJobsToCompleteOnShutdown;
    private boolean overwriteExistingJobs;
    private final Map<String, String> properties;
    private final Jdbc jdbc;
}

二、spring.security类配置

1)位置:org.springframework.boot.autoconfigure.security.SecurityProperties
2)核心配置:filter(优先级)、user(源码学习可用)
3)顶层类代码示例:

@ConfigurationProperties(
    prefix = "spring.security"
)
public class SecurityProperties {
    public static final int BASIC_AUTH_ORDER = 2147483642;
    public static final int IGNORED_ORDER = Integer.MIN_VALUE;
    public static final int DEFAULT_FILTER_ORDER = -100;
    private final Filter filter = new Filter();
    private final User user = new User();
}

三、spring.webflux类配置

1)位置:org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties
2)核心配置:暂未使用
3)顶层类代码示例:

@ConfigurationProperties(
    prefix = "spring.webflux"
)
public class WebFluxProperties {
    private String basePath;
    private final Format format = new Format();
    private final Session session = new Session();
    private String staticPathPattern = "/**";
}

四、spring.jta配置

1)位置:org.springframework.boot.autoconfigure.transaction.jta.JtaProperties
2)顶层类代码示例:

@ConfigurationProperties(
    prefix = "spring.jta",
    ignoreUnknownFields = true
)
public class JtaProperties {
    private String logDir;
    private String transactionManagerId;
}
文章来源:https://blog.csdn.net/src1025/article/details/135081028
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。