SpringBoot实现自定义stater

1、添加依赖


    
        org.springframework.boot
        spring-boot-configuration-processor
        true
    
    
        org.springframework.boot
        spring-boot-autoconfigure
    

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/

你可能感兴趣的:(java,json)