ServiceLoader的使用

300 阅读1分钟

serviceLoader在注册listener时候还是挺好用的

1,在服务提供者jar中下配置,建立一个META-INF文件夹,在META-INF文件夹下建立一个services文件夹;(如果是一般的web工程, 就在resources下面操作)


2, 在services下用接口名创建配置文件,如:io.shardingsphere.core.event.ShardingEventListenerRegistry

文件的内容就是实现类的类名:com.lepeen.service.sharding.listener.JDBCRegisterListener


3,服务的使用者:

 for (ShardingEventListenerRegistry each : ServiceLoader.load(ShardingEventListenerRegistry.class)) {
            each.register();
        }