Nacos深入浅出(七)

大家可以把这个也下载下来,结合之前的Nacos一起来看下,感觉前面几篇看了好像冰山一角的感觉

学无止境!

https://github.com/nacos-group/nacos-spring-project

其实前面的很多猜测都是在这个里面去实现的;这个明天再系统的去看吧;

把之前的问题给大家补上,就是EventDispatcher.java如何初始化的

    static final CopyOnWriteArrayList<Entry> LISTENER_HUB = new CopyOnWriteArrayList<Entry>();

这里给大家看下一个小方法,我自己写的

public abstract class AbstractEventListener {
// 抽象类中定义一个构造方法
public AbstractEventListener(){ System.out.println("print abstract constractor init"); } static class AsyncNotifyService extends AbstractEventListener{ private String serviceName ;
// 子类中自定义构造函数
public AsyncNotifyService(String serviceName){ System.out.println("print class AsyncNotifyService constractor init"); this.serviceName = serviceName; } } // 子类中在自定义构造函数 static class LongPollingService extends AbstractEventListener { public LongPollingService() { System.out.println("print class LongPollingService constractor init"); } } public static void main(String[] args){ AsyncNotifyService asyncNotifyService = new AsyncNotifyService("123"); LongPollingService longPollingService = new LongPollingService(); } }

其实这个不管父类是抽象类还是具体某个类,子类在继承的时候都会先调用父类中的构造函数,然后再执行子类中的构造函数;

下面贴下结果:

这样再回去看Nacos的方法就能明白了

public class LongPollingService extends AbstractEventListener {}
public class AsyncNotifyService extends AbstractEventListener {}

在每个子类的Service中,都有构造函数,在构造函数中进行了初始化,初始化了LISTENER_HUB ;
这样就完成了;

明天继续给大家写!
原文地址:https://www.cnblogs.com/longxok/p/11018849.html