Solon v3.7.3

插件 Spi 的配置元信息的自动处理

</> markdown
2025年12月4日 上午11:17:35

插件(或者模块)开发时,手写 solon-configuration-metadata.json 显然是很麻烦的。使用 @BindProps 注解,和 solon-configuration-processor 插件,可自动生成配置元信息文件。

1、引入依赖

这是一个编译增强工具,可以在编译时为 @BindProps 注解的类,生成相应的配置元信息。从而使插件具备配置提示功能(配合 solon idea 插件):

maven:

<dependencies>
    <dependency>
        <groupId>org.noear</groupId>
        <artifactId>solon-configuration-processor</artifactId>
        <scope>provided</scope> <!-- 这条一定要加 -->
    </dependency>
<dependencies>


<!-- jdk25 之后要添加 annotationProcessorPaths -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <annotationProcessorPaths>
            ...
            <path>
                <groupId>org.noear</groupId>
                <artifactId>solon-configuration-processor</artifactId>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

gradle:

compileOnly("org.noear:solon-configuration-processor")
annotationProcessor("org.noear:solon-configuration-processor")

2、代码应用示例(其它是自动的)

通过类绑定属性方式

@BindProps(prefix = "server")
@Configuration
public class ServerProps {
    private Integer port;
    private String host;
}

通过方法结果绑定属性方式

public class ServerProps {
    private Integer port;
    private String host;
}

@Configuration
public class ServerConfig {
    @BindProps(prefix = "server")
    @Bean
    public ServerProps serverProps() {
        return new ServerProps();
    }
}