Read the code of NettyRPC and found that Spring's InitializingBean
is implemented in RpcServer
. A Netty server is started in the method implementation and blocked.
@Override
public void afterPropertiesSet() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
// 前略...
future.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
Source code address:
https://github.com/luxiaoxun/...
What I want to ask is, will such blocking affect the initialization of Spring? (I have not read the Spring source code and do not know the situation. Please give me some advice...
I feel like I asked a very stupid question... The initialization process of the Spring container is single-threaded. If it is blocked here, it will naturally mean that... the follow-up work cannot be carried out.
A test Bean is added at the end of the bean configuration file, and the log will be logged during initialization, but the result is not printed.
This should be reasonable, because if a bean is instantiated by multiple threads, the dependency between the bean and the bean will be difficult to handle, and the code complexity will increase sharply.
Reasonable, the reason has been mentioned above. If you need to initialize beans asynchronously, just rewrite the logic of BeanFactory. The premise is that you have to ensure that these beans do not depend on each other