news 2026/6/10 15:49:41

SpringBoot3自定义配置实战指南

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringBoot3自定义配置实战指南

前言

如今,Spring Boot3 强势登场,带来了诸多令人眼前一亮的新特性与改进,其中自定义配置更是为开发者们提供了更为灵活且强大的能力。那么,究竟该如何在 Spring Boot3 中实现自定义配置呢?在深入探究自定义配置之前,先来简单回顾下 Spring Boot3 的自动配置原理。

当我们引入各种 Starter 依赖时,Spring Boot 会依据类路径下的相关资源以及一系列条件注解,自动配置应用程序所需的各类组件。例如,引入 spring-boot-starter-web,Spring Boot 便能自动配置好 Web 开发所需的基础组件,像 Tomcat 服务器、Spring MVC 框架等,让开发者可以迅速投身业务逻辑开发,无需在基础配置上耗费过多精力。这种自动配置机制极大地提升了开发效率,然而,在实际项目中,有时官方默认的配置难以完全契合复杂多变的业务需求,此时,自定义配置便派上了用场。

修改默认配置

Spring Boot3 为每个 Starter 都提供了默认配置,这些配置在大多数常规场景下能够满足功能开发需求。但倘若我们想要修改某些默认配置,操作其实相当简便。以修改 Tomcat 启动端口为例,在 Spring Boot 开发 Web 应用时,Tomcat 默认启动端口为 8080,若要将其修改为 8888,只需在 application.properties 文件中添加如下记录:

server.port=8888

修改完成后,重启项目,从启动日志中便可清晰看到:Tomcat started on port (s): 8888 (http),此时通过浏览器访问http://localhost:8888,就能正常访问应用了。

再比如配置数据库连接信息 (这里以使用 com.alibaba 的 Druid 连接池为例),同样在 application.properties 文件中添加如下配置:

druid.url=jdbc:mysql://192.168.0.20:3306/test druid.driver-class=com.mysql.jdbc.Driver druid.username=root druid.password=123456 druid.initial-size=1 druid.min-idle=1 druid.max-active=20 druid.test-on-borrow=true

如此一来,就完成了对 Druid 连接池相关默认配置的修改。从这两个简单示例可以看出,若要修改 Starter 模块中的默认配置,只需在 application.properties 文件中添加相应的修改配置即可。

自定义属性配置

在 application.properties 中添加自定义属性

在 application.properties 文件里,我们不仅能够修改默认配置,还可以自定义属性。例如,添加如下自定义属性配置:

myapp.custom.property1=value1 myapp.custom.property2=100

编写 Bean 类加载属性

接下来,我们需要编写一个 Bean 类,用于加载这些自定义属性。有两种常见方式:

使用 @Value 注解加载

创建一个 Sam 类,并添加 @Component 注解,以便 Spring 在启动时扫描到该类并将其纳入 Spring 容器管理。通过 @Value 注解来加载属性,示例代码如下:

import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Sam { @Value("${myapp.custom.property1}") private String property1; @Value("${myapp.custom.property2}") private int property2; // 省略getter和setter方法 }

需注意,使用 @Value 注解时,所在类必须被 Spring 容器管理,且注解需传入完整的配置项 Key 值。此外,@Value 注解默认读取 application.properties 配置文件,若要读取其他配置文件,可借助 @PropertySource 注解指定。

使用 @ConfigurationProperties 注解加载

同样创建 Sam 类,通过 @ConfigurationProperties (prefix = "myapp.custom") 设置前缀,属性上无需再添加额外注解,示例代码如下:

import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "myapp.custom") public class Sam { private String property1; private int property2; // 省略getter和setter方法 }

这种方式更为简洁,尤其适用于批量加载具有相同前缀的多个属性。

在 Controller 中注入并使用 Bean

在 Controller 中注入上述 Sam Bean,即可使用自定义属性。例如:

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class SamController { @Autowired private Sam sam; @GetMapping("/sam") public String getSam() { return "property1: " + sam.getProperty1() + " , property2: " + sam.getProperty2(); } }

通过浏览器访问http://localhost:8080/sam,便能看到控制台正常打印出 Sam 类中自定义属性的值。

自定义自动配置类

当默认配置与自定义属性配置都无法满足复杂业务需求时,我们可以创建自定义自动配置类。

创建自定义自动配置类

首先,创建一个类,并使用 @Configuration 和 @ConditionalOnClass 注解。其中,@Configuration 表明这是一个配置类,@ConditionalOnClass 用于指定该自动配置类生效的条件,只有当类路径下存在特定类时,该配置类才会生效。假设我们要创建一个自定义的 Redis 自动配置类,示例代码如下:

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @ConditionalOnClass(RedisTemplate.class) @EnableConfigurationProperties(RedisProperties.class) public class CustomRedisAutoConfiguration { @Autowired private RedisProperties redisProperties; @Bean public RedisConnectionFactory redisConnectionFactory() { // 根据自定义的RedisProperties配置创建RedisConnectionFactory实例 // 具体实现需根据实际情况编写 return null; } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } }

在上述代码中,@ConditionalOnClass (RedisTemplate.class) 表示只有当项目中引入了 RedisTemplate 类,即项目依赖了 Redis 相关库时,该自动配置类才会生效。@
EnableConfigurationProperties (RedisProperties.class) 用于启用对 RedisProperties 配置属性类的支持,通过 @Autowired 注入 RedisProperties,以便在配置类中获取自定义的 Redis 配置属性。

编写配置属性类

接着,编写与自定义配置相关的属性类。例如,上述自定义 Redis 自动配置类中用到的 RedisProperties 类,代码如下:

import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "custom.redis") public class RedisProperties { private String host; private int port; private String password; // 省略getter和setter方法 }

在这个类中,通过 @ConfigurationProperties (prefix = "custom.redis") 指定了属性前缀为 custom.redis,意味着在 application.properties 文件中,以 custom.redis 开头的属性将被绑定到该类的对应字段上。比如,在 application.properties 中添加如下配置:

custom.redis.host=192.168.0.21 custom.redis.port=6379 custom.redis.password=123456

Spring Boot 在启动时会自动将这些属性值绑定到 RedisProperties 类的 host、port 和 password 字段上。

注册自动配置类

在 Spring Boot3 中,注册自动配置类的方式与之前版本有所不同。在 Spring Boot2.x 中,我们通常在 Starter 的 resources/META - INF/spring.factories 中注册自动配置类,但在 Spring Boot3.x 中,该机制已被官方弃用。取而代之的是,在 src/main/resources/META -
INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件中注册自动配置类。假设我们的自定义 Redis 自动配置类全限定名为 com.example.config.CustomRedisAutoConfiguration,那么在 AutoConfiguration.imports 文件中添加如下内容:

com.example.config.CustomRedisAutoConfiguration

完成上述步骤后,当项目引入包含该自定义自动配置类的 Starter 时,Spring Boot 会自动加载并应用该配置类,实现自定义的 Redis 配置功能。

总结

通过以上对 Spring Boot3 中自定义配置的全面解析,相信大家已经掌握了修改默认配置、自定义属性配置以及创建自定义自动配置类的方法。合理运用这些自定义配置手段,能够让我们的 Spring Boot 项目更加贴合业务需求,提升开发效率与代码的可维护性,为打造高质量的互联网软件项目奠定坚实基础。在实际项目开发中,不妨大胆尝试这些自定义配置技巧,让 Spring Boot3 发挥出更大的效能。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!