alibaba / fastjson

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
https://github.com/alibaba/fastjson2/wiki/fastjson_1_upgrade_cn
Apache License 2.0
25.76k stars 6.5k forks source link

fastjson.properties禁用fastjson.asmEnable时未加载JSON类时,单独调用SerializeConfig.getGlobalInstance()报无法实例化异常 #3489

Open suciver opened 4 years ago

suciver commented 4 years ago

当在项目中使用fastjson.properties设置 fastjson.asmEnable=false 此时如果没有先加载com.alibaba.fastjson.JSON类,而是直接调用SerializeConfig.getGlobalInstance()或者new SerializeConfig都会报 Exception in thread "main" java.lang.ExceptionInInitializerError at com.alibaba.fastjson.serializer.SerializeConfig.(SerializeConfig.java:67) at com.alibaba.fastjson.serializer.SerializeConfig.(SerializeConfig.java:301) at com.alibaba.fastjson.serializer.SerializeConfig.(SerializeConfig.java:293) at com.alibaba.fastjson.serializer.SerializeConfig.(SerializeConfig.java:53) at AppTest.main(AppTest.java:108) Caused by: java.lang.NullPointerException at com.alibaba.fastjson.JSON.config(JSON.java:130) at com.alibaba.fastjson.JSON.(JSON.java:99) ... 5 more 看了SerializeConfig类的定义中有依赖com.alibaba.fastjson.JSON的类,而在使用fastjson.asmEnable=false时,JSON类在加载过程中就会调用SerializeConfig.getGlobalInstance()进行实例化动作,这种情况下程序入口如果没有加载完成com.alibaba.fastjson.JSON类直接就实例化SerializeConfig就会出现实例化失败的异常。为了方便重现直接用简单的main方法: import com.alibaba.fastjson.serializer.SerializeConfig; public class AppTest public static void main(String[] args){ new SerializeConfig(); } }

该问题在当前的最新版本1.2.74仍然存在。

qixiaobo commented 5 months ago
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 27 common frames omitted

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.fastjson.support.config.FastJsonConfig]: Factory method 'fastJsonConfig' threw exception; nested exception is java.lang.ExceptionInInitializerError at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ... 40 common frames omitted Caused by: java.lang.ExceptionInInitializerError: null at com.alibaba.fastjson.serializer.SerializeConfig.(SerializeConfig.java:67) at com.alibaba.fastjson.serializer.SerializeConfig.(SerializeConfig.java:295) at com.alibaba.fastjson.serializer.SerializeConfig.(SerializeConfig.java:287) at com.alibaba.fastjson.serializer.SerializeConfig.(SerializeConfig.java:53) at com.alibaba.fastjson.support.config.FastJsonConfig.(FastJsonConfig.java:88) at com.f6car.stock.config.ApimgplusConfigurer.fastJsonConfig(ApimgplusConfigurer.java:55) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ... 41 common frames omitted Caused by: java.lang.NullPointerException: null at com.alibaba.fastjson.JSON.config(JSON.java:131) at com.alibaba.fastjson.JSON.(JSON.java:100) ... 52 common frames omitted

qixiaobo commented 5 months ago

image