You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NullPointerException happened when using SpringContainer.getContext() #3476
write a project that is a provider side exposed serviceA and in the service implimentation invokes another remote service serviceB. The invoke code is like this SpringContainer.getContext().getBean("ref");
continious request from outside for the serviceA (many node for serviceA)
stop one serviceA node
start the stopped serviceA node
almost every time first one or two serviceA invoke failed, exception is NullPointerException, occurred for SpringContainer.getContext();
Reason: dubbo service is exposed with spring container ContextRefreshedEvent。 but that moment SpringContainer.getContext() is not started, if it is ContextStartedEvent, this bug will be fixed.
write a project that is a provider side exposed serviceA and in the service implimentation invokes another remote service serviceB. The invoke code is like this SpringContainer.getContext().getBean("ref");
I don't understand why SpringContainer.getContext().getBean("ref"); will throw NPE, by the time contextRefreshedEvent is send, the reference of ServiceB should be ready in ApplicationContext for use.
context.start() should only be responsible for those beans that have implemented Lifecycle, as the javadoc showed:
/** * Start all registered beans that implement {@link Lifecycle} and are <i>not</i> * already running. Any bean that implements {@link SmartLifecycle} will be * started within its 'phase', and all phases will be ordered from lowest to * highest value. All beans that do not implement {@link SmartLifecycle} will be * started in the default phase 0. A bean declared as a dependency of another bean * will be started before the dependent bean regardless of the declared phase. */@Overridepublicvoidstart() {
startBeans(false);
this.running = true;
}
write a project that is a provider side exposed serviceA and in the service implimentation invokes another remote service serviceB. The invoke code is like this SpringContainer.getContext().getBean("ref");
I don't understand why SpringContainer.getContext().getBean("ref"); will throw NPE, by the time contextRefreshedEvent is send, the reference of ServiceB should be ready in ApplicationContext for use.
context.start() should only be responsible for those beans that have implemented Lifecycle, as the javadoc showed:
/** * Start all registered beans that implement {@link Lifecycle} and are <i>not</i> * already running. Any bean that implements {@link SmartLifecycle} will be * started within its 'phase', and all phases will be ordered from lowest to * highest value. All beans that do not implement {@link SmartLifecycle} will be * started in the default phase 0. A bean declared as a dependency of another bean * will be started before the dependent bean regardless of the declared phase. */@Overridepublicvoidstart() {
startBeans(false);
this.running = true;
}
Activity
chickenlj commentedon Feb 18, 2019
I don't understand why
SpringContainer.getContext().getBean("ref");will throw NPE, by the timecontextRefreshedEventis send, the reference of ServiceB should be ready in ApplicationContext for use.context.start()should only be responsible for those beans that have implemented Lifecycle, as the javadoc showed:stoneapple commentedon Feb 18, 2019
因为服务暴露是在Spring的ContextRefresh阶段,服务暴露后,就会有交易进来。这个阶段SpringContainer中的XMLClasspathApplicationContext对象还在启动中,并没有赋值成功,但是服务已经暴露了,如果交易量大的情况下,服务实现类里面通过SpringContainer.getContext().getBean("ref");去获取另外一个服务的引用,肯定会报NPE的,不知道说清楚了没。
chickenlj commentedon Feb 19, 2019
多谢,明白问题了:
SpringContainer.getContext()返回null
我刚开始理解成SpringContainer.getContext().getBean("ref");找不到bean。
stoneapple commentedon Feb 21, 2019
我们有个解决思路,把contextrefresh 事件换成contextstarted事件,这样的改法能解决这个问题,请你们看下是否有其他问题,这个修改方案,我提一个PR
cvictory commentedon Feb 22, 2019
stoneapple commentedon Feb 25, 2019
在一个服务的具体实现类内部,我们是没法获取到ApplicationContext对象的,这个对象是main方法启动的时候才有,而且在启动过程中,它也是null,无法通过他来getbean
beiwei30 commentedon Mar 5, 2019
我的意见是,这种用法不太对,service 实现类直接实现
ApplicationContextAware自然而然就拿到 context 实例了。相反,我觉得 SpringContainer 上根本就不应该暴露 getContext 方法。mercyblitz commentedon Mar 5, 2019
@stoneapple Did you start
SpringContainerin concurrency? Why you have to get a nullingClassPathXmlApplicationContext?beiwei30 commentedon Mar 5, 2019
otherwise we may change the original impl to:
cvictory commentedon Mar 5, 2019
除了本身优化外,使用优雅上线服务,也可以解决这个问题吧。
整个spring还没启动好,被提早依赖其他bean。
[DUBBO-3476]: NullPointerException happened when using SpringContaine…
Merge master branch to 3.x dev branch (#3718)