这里主要介绍 neo4j 的 main 函数的一些流程。
main 函数在 CommunityEntryPoint
类中。
1 | public static void main( String[] args ) |
首先通过 ServerBootstrapper.start(new CommunityBootstrapper(), args)
进入到 ServerBootstrapper.start(Bootstrapper boot, String... argv)
方法, 在该方法的末尾通过 boot.start(args.homeDir(), args.configFile(), args.configOverrides())
调用进入到 ServerBootstrapper.start(File homeDir, Optional<File> configFile, Map<String, String> configOverrides)
, 在该方法的末尾通过
1 | server = createNeoServer(config, dependencies); |
启动。
通过 createNeoServer(config, dependencies)
会进入到,ServerBootstrapper.createNeoServer(Config config, GraphDatabaseDependencies dependencies)
, 在该函数的最后通过 createNeoServer(graphFactory, config, dependencies)
会进入到 CommunityBootstrapper.createNeoServer(GraphFactory graphFactory, Config config, GraphDatabaseDependencies dependencies)
, 该函数只有一个语句
1 | return new CommunityNeoServer(config, graphFactory, dependencies); |
最终是新建了一个 CommunityNeoServer
对象返回到了 ServerBootstrapper.start()
中, 并调用了 CommunityNeoServer
对象的 start()
方法,完成了 neo4j 的启动。
这样我们发现,整个启动的核心代码都在 CommunityNeoServer.start()
中,CommunityNeoServer
继承自 AbstractNeoServer
, 并且没有重写 AbstractNeoServer
的 start()
方法,所以启动的核心代码都在 AbstractNeoServer.start()
函数中。 AbstractNeoServer.start()
中只有一行代码:
1 | life.start(); |
这里的 life
是 AbstractNeoServer
类的一个变量,是 LifeSupport
类的一个实例。lift.start()
会跳去执行 LifeSupport.start()
方法。