SpringBoot实现自定义stater

1、添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
</dependencies>

2、配置文件读取类

@ConfigurationProperties(prefix = "gefs.socketio")
@Data
public class SocketioProperties {
    private boolean enabled;
    private String host;
    private int port;
}

3、编写AutoConfigure类

@org.springframework.context.annotation.Configuration
@EnableConfigurationProperties(SocketioProperties.class)
@ConditionalOnProperty(prefix = "gefs.socketio", name = "enabled", havingValue = "true")
public class SocketioAutoConfiguration {
    @Autowired
    private SocketioProperties socketioProperties;

    @Bean
    public SocketIOServer socketIOServer() {
        Configuration config = new Configuration();
        //在本地window环境测试时用localhost
        config.setHostname(socketioProperties.getHost());
        config.setPort(socketioProperties.getPort());
        SocketIOServer server = new SocketIOServer(config);
        server.start();
        return server;
    }

    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
        return new SpringAnnotationScanner(socketServer);
    }
}

4、在resources/META-INF/下创建spring.factories文件并编写内容

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.aostarit.gefs.socketio.SocketioAutoConfiguration

5、编译代码(增加配置文件自动提示)
将编译好的target\classes\META-INF/spring-configuration-metadata.json文件拷贝至resources/META-INF/

猜你喜欢

转载自blog.51cto.com/handsomebingli/2433821