项目中引入了dubbo-spring-boot-starter 
version=2.7.1,其中中有一个配置类是以FactoryBean的形式加载到Spring 容器中,该配置类只有一个含参构造器,此时会触发一个异常:
`Caused by: java.lang.NoSuchMethodException: 
com.abakus.user.web.StringFactoryBean.<init>()`

该配置类示例如下:
`
@Component
@RequiredArgsConstructor
public class StringFactoryBean implements FactoryBean<String> {

    private final ObjectMapper objectMapper;

    @Override
    public String getObject() throws Exception {
        return "";
    }

    @Override
    public Class<?> getObjectType() {
        return String.class;
    }
}
`

异常栈信息如下所示:
`org.springframework.beans.factory.BeanCreationException: Error creating bean 
with name 'stringFactoryBean' defined in file [/*/*/*/StringFactoryBean.class]: 
Instantiation of bean failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[*.*.*.StringFactoryBean]: No default constructor found; nested exception is 
java.lang.NoSuchMethodException: *.*.*.StringFactoryBean.<init>()
        at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1270)
        at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1164)
        at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:974)
        at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:848)
        at 
org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:574)
        at 
org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:514)
        at 
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:477)
        at 
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:471)
        at 
org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:190)
        at 
org.apache.dubbo.spring.boot.beans.factory.config.DubboConfigBeanDefinitionConflictProcessor.resolveUniqueApplicationConfigBean(DubboConfigBeanDefinitionConflictProcessor.java:78)
        at 
org.apache.dubbo.spring.boot.beans.factory.config.DubboConfigBeanDefinitionConflictProcessor.postProcessBeanFactory(DubboConfigBeanDefinitionConflictProcessor.java:63)
        at 
org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286)
        at 
org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:130)
        at 
org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:691)
        at 
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528)
        at 
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
        at 
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
        at 
org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
        at 
org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
        at com.abakus.user.StartApp.main(StartApp.java:30)

Caused by: org.springframework.beans.BeanInstantiationException: Failed to 
instantiate [*.*.*.StringFactoryBean]: No default constructor found; nested 
exception is java.lang.NoSuchMethodException: 
com.abakus.user.web.StringFactoryBean.<init>()
        at 
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
        at 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1262)
        ... 19 common frames omitted

Caused by: java.lang.NoSuchMethodException: *.*.*.StringFactoryBean.<init>()
        at java.lang.Class.getConstructor0(Class.java:3082)
        at java.lang.Class.getDeclaredConstructor(Class.java:2178)
        at 
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)
        ... 20 common frames omitted`

根据我的研究,得出以下结论:DubboConfigBeanDefinitionConflictProcessor会从spring容器中获取ApplicationConfig.class
 
的所有beanName,对于FactoryBean的beanType会先获取该实例,然而对于含参构造器的spring容器类的实例化需要AutowiredAnnotationBeanPostProcessor的辅助。不过当DubboConfigBeanDefinitionConflictProcessor被调用的时候AutowiredAnnotationBeanPostProcessor还未被加载到spring容器内,所以触发上述异常。

[ Full content available at: 
https://github.com/apache/dubbo-spring-boot-project/issues/638 ]
This message was relayed via gitbox.apache.org for 
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to