我正在尝试将我的 spring 4 代码迁移到 spring 5(特别是最新的 -> 5.3.16),其中大部分似乎还可以,但是我有一个
No default constructor found; nested exception is java.lang.NoSuchMethodException: com.acme.AcmeEventListenerFactory.<init>()
尝试像这样初始化 bean 时出错:
@Configuration
@Import({ EventForwardingActor.class, AcmeTopicPublisher.class, AcmeEventListenerFactory.class })
public class AcmeSpringBridgeConfiguration {
}
其中,AcmeEventListenerFactory 有一个构造函数,例如
public class AcmeEventListenerFactory implements EventListenerFactory, Ordered {
private static final Logger LOGGER = LoggerFactory.getLogger(AcmeEventListenerFactory.class);
private int m_listenerFactoryOrder = 50;
private final AcmeTopicPublisher m_publisher;
public AcmeEventListenerFactory(AcmeTopicPublisher publisher) {
m_publisher = publisher;
}
参数当然应该是第二次导入所代表的bean(AcmeTopicPublisher)
使用
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AcmeSpringBridgeConfiguration.class)
public @interface EnableAcmeListener {
}
由于这在 spring4 中有效,我假设我错过了讨论此更改的迁移文档。有人可以解释我需要在这里做什么吗?
(下面的完整堆栈跟踪)
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.acme.framework.receive.acme.AcmeEventListenerFactory': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.acme.framework.receive.acme.AcmeEventListenerFactory]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.acme.framework.receive.acme.AcmeEventListenerFactory.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1232)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:671)
at org.springframework.context.event.EventListenerMethodProcessor.postProcessBeanFactory(EventListenerMethodProcessor.java:113)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:325)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:198)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564)
at com.acme.framework.receive.acme.AcmeEventListenerFactoryAcceptanceTest.initSpringWithListener-AcmeEventListenerFactoryAcceptanceTest.java:250)
为了完整起见,这里是其他类
@Component(EventForwardingActor.BEAN_NAME)
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.NO)
public class EventForwardingActor extends UntypedActorWithMetrics {
static final String BEAN_NAME = "declarativeEventsEventForwardingActor";
@Autowired
private ApplicationEventPublisher m_publisher;
private final AcmeTopic m_topic;
public EventForwardingActor(AcmeTopic topic) {
m_topic = topic;
}
....
}
public class AcmeTopicPublisher {
private final Map<AcmeTopic, ActorRef> m_topicActors = new HashMap<>();
private final SpringActorFactory m_factory;
public AcmeTopicPublisher(SpringActorFactory factory) {
m_factory = factory;
}
....
}