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

Posted creatorkou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了玄学springboot applicationcontext.getBean(用类名String还是类型Class), getBean(..)的调用场景结果不同?getBean(..)还会阻塞?@D相关的知识,希望对你有一定的参考价值。

springContextStati是一个实现了Applicationcontextaware的类的名字,可以getBean(..)
RegistryTask是一个在RegistryServer调用netty封装的channelinitializer时候,的clienthandler被触发读事件的时候 new 的
@Component
@DependsOn(value="springContextStatic","normalConfig","channelCache","serviceAddrCache")
DependsOn的参数都是String字符串,类的名称,后面在RegistryTask的构造函数中,必须用appcontext.getBean(String name)获得bean,否则阻塞。
public class RegistryServer ..... ...... @PostConstruct private void doRunServer() try
此处被注释的代码部分如果不执行,那么后面一个RegistryTask阻塞在构造函数上
    执行此处getBean(Class<T>)代码的意义是什么?加载?玄。。
/*LOGGER.info("doRunServer:appctx:",SpringContextStatic.getApplicationContext()); LOGGER.info("normalconfig:",SpringContextStatic.getBean(NormalConfig.class)); LOGGER.info("channelcache:",SpringContextStatic.getBean(ChannelCache.class)); LOGGER.info("serviceaddrcache:",SpringContextStatic.getBean(ServiceAddrCache.class));*/ //创建并初始化 Netty 服务端辅助启动对象 ServerBootstrap ServerBootstrap serverBootstrap = initServerBootstrap(bossGroup, workerGroup); //绑定对应ip和端口,同步等待成功 ChannelFuture future = serverBootstrap.bind(paramConfig.getServerport()).sync(); LOGGER.info("regist server 已启动,端口:", paramConfig.getServerport()); //等待服务端监听端口关闭 future.channel().closeFuture().sync(); catch (InterruptedException i) LOGGER.error("regist server 出现异常,端口:, cause:", paramConfig.getServerport(), i.getMessage()); finally //优雅退出,释放 NIO 线程组 workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); /** * 创建并初始化 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 = LoggerFactory.getLogger(RegistryTask.class);

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

    public RegistryTask(ChannelHandlerContext ctx, RegistryMessage msg) 
        System.out.println("constructing registrytask,appctx:"+SpringContextStatic.getApplicationContext());
        log.info("doRunServer:appctx:",SpringContextStatic.getApplicationContext());
        log.info("normalconfig:",SpringContextStatic.getBean("normalConfig"));
        /*log.info("channelcache:",SpringContextStatic.getBean(ChannelCache.class));
        log.info("serviceaddrcache:",SpringContextStatic.getBean(ServiceAddrCache.class));*/
        
        this.ctx = ctx;
        this.msg = msg;

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

 

以上是关于玄学springboot applicationcontext.getBean(用类名String还是类型Class), getBean(..)的调用场景结果不同?getBean(..)还会阻塞?@D的主要内容,如果未能解决你的问题,请参考以下文章

面试一问: SpringBoot的启动流程是什么样的?

SpringBoot 启动打印 Banner:佛祖保佑,永无BUG!

UOJ #46 清华集训2014玄学

各种各样的——玄学搜索

CSS 玄学:为啥页面垂直对齐如此困难?

CSS 玄学:为啥页面垂直对齐如此困难?