码迷,mamicode.com
首页 > 移动开发 > 详细

玄学springboot applicationcontext.getBean(用类名String还是类型Class), getBean(..)的调用场景结果不同?getBean(..)还会阻塞?@DependsOn按照名称依赖,那么getBean用类名String

时间:2019-08-14 21:55:20      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:soc   ice   worker   color   cache   normal   oop   bsp   string   

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(..)还会阻塞?@DependsOn按照名称依赖,那么getBean用类名String

标签:soc   ice   worker   color   cache   normal   oop   bsp   string   

原文地址:https://www.cnblogs.com/CreatorKou/p/11354682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!