Spring Boot自定义Info Contributor如何加载构建生成的.properties文件
自定义Actuator Info Contributor读取构建生成Properties实现方案
你疑惑的GitProperties注入逻辑本质是Spring Boot Actuator自动配置完成的:启动时自动扫描classpath下的git.properties文件,绑定为GitProperties实例,再通过构造器传入GitInfoContributor注册为容器Bean。参照这套逻辑实现自定义组件的步骤如下:
1. 定义自定义Properties承接类
参照GitProperties的实现,继承Actuator提供的InfoProperties基类,构造器接收加载到的Properties实例即可,也可以在类中添加自定义属性的快捷读取方法:
import org.springframework.boot.actuate.info.InfoProperties; import java.util.Properties; public class CustomBuildProperties extends InfoProperties { public CustomBuildProperties(Properties entries) { super(processEntries(entries)); } private static Properties processEntries(Properties entries) { // 此处可做属性名修正、值预处理,无特殊需求直接返回原对象即可 return entries; } // 示例:自定义快捷读取方法,对应properties中的app.version字段 public String getAppVersion() { return get("app.version"); } }
2. 实现自定义InfoContributor
参照GitInfoContributor的写法,继承InfoPropertiesInfoContributor,泛型传入上一步定义的Properties类,实现核心的内容暴露逻辑:
import org.springframework.boot.actuate.info.Info; import org.springframework.boot.actuate.info.InfoPropertiesInfoContributor; import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertySource; import java.util.Map; import java.util.Properties; public class CustomInfoContributor extends InfoPropertiesInfoContributor<CustomBuildProperties> { public CustomInfoContributor(CustomBuildProperties properties) { // 默认使用SIMPLE模式,和官方GitInfoContributor逻辑对齐 super(properties, Mode.SIMPLE); } public CustomInfoContributor(CustomBuildProperties properties, Mode mode) { super(properties, mode); } @Override public void contribute(Info.Builder builder) { // 自定义/info端点下的节点名,此处示例为custom builder.withDetail("custom", generateContent()); } @Override protected PropertySource<?> toSimplePropertySource() { Properties props = new Properties(); // 将需要在SIMPLE模式下暴露的属性通过copyIfSet方法拷贝 copyIfSet(props, "app.name"); copyIfSet(props, "app.version"); copyIfSet(props, "build.time"); // 支持自定义值处理,比如和短commitId类似的短构建ID逻辑 String fullBuildId = getProperties().get("build.id"); if (fullBuildId != null && fullBuildId.length() >= 8) { props.put("build.id-short", fullBuildId.substring(0, 8)); } return new PropertiesPropertySource("custom", props); } @Override protected void postProcessContent(Map<String, Object> content) { // 和官方逻辑对齐,将时间字段转换为Instant类型,避免序列化格式不一致问题 replaceValue(content, "build.time", getProperties().getInstant("build.time")); } }
3. 注册自动配置类加载目标properties文件
这一步对应官方加载git.properties的逻辑,编写自动配置类,从classpath下读取构建阶段生成的properties文件(构建后文件存于target/classes目录,可直接通过类路径资源加载),完成相关Bean的注册:
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnResource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.support.PropertiesLoaderUtils; import java.io.IOException; import java.util.Properties; @Configuration public class CustomInfoContributorAutoConfiguration { // 替换为你构建生成的properties文件在classpath下的路径 private static final String CUSTOM_PROPS_PATH = "classpath:custom-build.properties"; @Bean @ConditionalOnMissingBean @ConditionalOnResource(resources = CUSTOM_PROPS_PATH) public CustomBuildProperties customBuildProperties() throws IOException { Properties props = PropertiesLoaderUtils.loadProperties( new DefaultResourceLoader().getResource(CUSTOM_PROPS_PATH) ); return new CustomBuildProperties(props); } @Bean @ConditionalOnMissingBean @ConditionalOnBean(CustomBuildProperties.class) public CustomInfoContributor customInfoContributor(CustomBuildProperties properties) { return new CustomInfoContributor(properties); } }
如果是Spring Boot 2.7+版本,需要在META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中添加上述自动配置类的全限定名;低版本在META-INF/spring.factories中配置自动配置类路径即可。
注意事项
- 确保构建阶段生成properties文件的步骤早于项目编译、打包环节,否则启动时classpath下找不到对应文件,
@ConditionalOnResource条件不满足会跳过相关Bean注册 - 如果需要对齐GitInfoContributor的模式切换能力(SIMPLE/FULL),可以自行绑定
management.info.custom.mode配置项,注册Contributor时传入对应Mode参数 - 如果不需要FULL模式的全量属性暴露能力,无需继承
InfoPropertiesInfoContributor,直接实现InfoContributor接口,在contribute方法中加载properties后写入Info.Builder即可,上述写法是完全对齐官方GitInfoContributor的标准实现
内容的提问来源于stack exchange,提问作者Sam
相关产品推荐
相关产品推荐

