玄学springboot (用类名String还是类型Class), getBean(..)的调用场景结果不同?getBean(..)还会阻塞?@D...

时间:2025-02-18 07:15:16
springContextStati是一个实现了Applicationcontextaware的类的名字,可以getBean(..)
RegistryTask是一个在RegistryServer调用netty封装的channelinitializer时候,的clienthandler被触发读事件的时候 new 的
@Component
@DependsOn(value={"springContextStatic","normalConfig","channelCache","serviceAddrCache"})
DependsOn的参数都是String字符串,类的名称,后面在RegistryTask的构造函数中,必须用(String name)获得bean,否则阻塞。
public class RegistryServer { ..... ...... @PostConstruct private void doRunServer() { try {
此处被注释的代码部分如果不执行,那么后面一个RegistryTask阻塞在构造函数上
执行此处getBean(Class<T>)代码的意义是什么?加载?玄。。
/*("doRunServer:appctx:{}",()); ("normalconfig:{}",()); ("channelcache:{}",()); ("serviceaddrcache:{}",());*/ //创建并初始化 Netty 服务端辅助启动对象 ServerBootstrap ServerBootstrap serverBootstrap = initServerBootstrap(bossGroup, workerGroup); //绑定对应ip和端口,同步等待成功 ChannelFuture future = (()).sync(); ("regist server 已启动,端口:{}", ()); //等待服务端监听端口关闭 ().closeFuture().sync(); } catch (InterruptedException i) { ("regist server 出现异常,端口:{}, cause:", (), ()); } finally { //优雅退出,释放 NIO 线程组 (); (); } } /** * 创建并初始化 Netty 服务端辅助启动对象 ServerBootstrap * * @param bossGroup * @param workerGroup * @return */ private ServerBootstrap initServerBootstrap(EventLoopGroup bossGroup, EventLoopGroup workerGroup) { return new ServerBootstrap() .group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024) .childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(new ServerChannelInitializer(new RegistryServerHandler(threadPool))); } }
public class RegistryTask implements Runnable {
    private static Logger log = (RegistryTask.class);

    private ChannelHandlerContext ctx;
    private RegistryMessage msg;
    private ChannelCache channelCache = null;
    private ServiceAddrCache serviceAddrCache = null;
    private NormalConfig normalConfig = null;
    
    /*private ChannelCache channelCache = ();
    
    private ServiceAddrCache serviceAddrCache = ();
    
    private NormalConfig normalConfig = ();*/

    public RegistryTask(ChannelHandlerContext ctx, RegistryMessage msg) {
        ("constructing registrytask,appctx:"+());
        ("doRunServer:appctx:{}",());
        ("normalconfig:{}",("normalConfig"));
        /*("channelcache:{}",());
        ("serviceaddrcache:{}",());*/
        
        this.ctx = ctx;
        this.msg = msg;

上一个文件的被注释部分getBean(Class<T>)如果不执行,此处会阻塞,why?
/* = (); = (); = ();*/ 正确写法:getBean(String name) this.channelCache = (ChannelCache) ("channelCache"); this.serviceAddrCache = (ServiceAddrCache) ("serviceAddrCache"); this.normalConfig = (NormalConfig) ("normalConfig"); }