【源起Netty 外传】ServiceLoader详解

【源起Netty 外传】ServiceLoader详解

 

 1.5k 次阅读  ·  读完需要 7 分钟

前戏

netty源码注释有云:

...

If a provider class has been installed in a jar file that is
visible to the system class loader, and that jar file contains a
provider-configuration file named
<tt>java.nio.channels.spi.SelectorProvider</tt> in the resource
directory <tt>META-INF/services</tt>, then the first class name
specified in that file is taken.  The class is loaded and
instantiated; if this process fails then an unspecified error is
thrown.

...

不知所云?好吧,重点是,jar文件中在META-INF/services目录下配置了A属性,以某种方式能加载到。

how can do that? 答曰:ServiceLoader

摸索

ServiceLoader直译为服务加载器,最终目的是获取service的impl实现类。正如前文所说,它将加载META-INF/services下的配置文件,来锁定impl实现类。

  • 代码走起

1.首先,要有一个接口

//形状接口
public interface Shape {
    String introduce(); //介绍
}

2.然后,要有该接口的实现类。实现类很简单,介绍自己的形状是啥

//实现类一
public class Circle implements Shape {
    public String introduce() {
        return "圆形";  //言简意赅的介绍
    }
}
//实现类二
public class Sequare implements Shape {
    static{
        System.out.println("【Sequare】据说有延时加载,try it..");
    }
    public String introduce() {
        return "方形";
    }
}

眼尖的朋友可能已经注意到了,这里有个静态块,因为资料中有提到ServiceLoader有延时加载的效果。寡人不信,遂验之……

3.配置文件,放在META-INF/services

文件位置:

- src
    -main
        -resources
            - META-INF
                - services
                    - xxxpackage.Shape

文件名:包名.接口名

文件内容:包名.接口实现类,换行符分隔

xxxpackage.Circle
xxxpackage.Sequare

4.ServiceLoader调用

ServiceLoader<Shape> shapeLoader = ServiceLoader.load(Shape.class);
Iterator<Shape> it = shapeLoader.iterator();
while(it.hasNext()){
    System.out.println("Iterator<Shape> next()方法调用..");
    Shape shape = it.next();
    System.out.printf("what's shape?%s\n",shape.introduce());
}

调用结果:

Iterator<Shape> next()方法调用..
【Sequare】据说有延时加载,try it..
what's shape?方形

从该结果可看出,在调用it.next()的时候,才真正的加载了Sequare类,确确实实是延时加载。期具体实现依靠ServiceLoader的内部类LazyIterator,感性趣的朋友可以自己看一下。

总结

  1. ServiceLoader重点在于可跨越jar包获取impl,这一点笔者通过maven多模块项目亲测ok
  2. 延时加载特性
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值