I'm trying migrate my spring 4 code to spring 5 (specifically latest -> 5.3.16), and most of it seems to be ok, but am having a
No default constructor found; nested exception is java.lang.NoSuchMethodException: com.acme.AcmeEventListenerFactory.<init>()
error, when trying to initialize a bean like this:
@Configuration
@Import({ EventForwardingActor.class, AcmeTopicPublisher.class, AcmeEventListenerFactory.class })
public class AcmeSpringBridgeConfiguration {
}
where, AcmeEventListenerFactory has one constructor like
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;
}
The parameter should of course be the bean represented by the 2nd import (AcmeTopicPublisher)
Using
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AcmeSpringBridgeConfiguration.class)
public @interface EnableAcmeListener {
}
As this worked in spring4 i'm assuming i missed the migration doc that talked about this change. Could someone explain what i need to do here?
(Full Stack trace below)
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)
For completeness here is the other classes
@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;
}
....
}