Quick revision
The short answer
After all singletons are created, EventListenerMethodProcessor goes through every bean definition, finds methods annotated with @EventListener on the bean's class, and registers one ApplicationListenerMethodAdapter per method with the context. The adapter keeps only the bean name and asks the context for the bean on every event, so scope rules apply at that moment: a prototype bean gets a brand-new instance for each event, and a lazy bean is created by its first event. Event matching uses the method's parameter type, generics included, and on proxied beans the method must be callable through the proxy, so not private.
What to remember
In this answer, we saw that EventListenerMethodProcessor scans every bean definition after the singletons are created and registers one ApplicationListenerMethodAdapter per @EventListener method. The adapter stores the bean name and calls getBean() on each event, so a prototype listener gets a new instance per event and a lazy bean is created by its first event, while scoped proxies need an active scope. Matching uses the parameter's full ResolvableType, and on proxied beans listener methods must not be private.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
After all singletons are created, EventListenerMethodProcessor goes through every bean definition, finds methods annotated with @EventListener on the bean's class, and registers one ApplicationListenerMethodAdapter per method with the context. The adapter keeps only the bean name and asks the context for the bean on every event, so scope rules apply at that moment: a prototype bean gets a brand-new instance for each event, and a lazy bean is created by its first event. Event matching uses the method's parameter type, generics included, and on proxied beans the method must be callable through the proxy, so not private.
Why this question is asked
Most of us use @EventListener as if it were a direct method call. Under the hood there is an adapter between the event and our bean, and the way it finds the bean explains several bugs that look impossible: a listener that forgets its state between events, a lazy bean created at an odd moment, a startup failure about method visibility. Interviewers ask this to see whether we can connect framework internals to behaviour. In this answer, we will follow how listener methods are discovered and registered, see what happens on each event, and then work through prototype and lazy beans, proxies and type matching.
1Discovering the Listener Methods
EventListenerMethodProcessor is an infrastructure bean that Spring registers automatically when annotation configuration is enabled. It implements SmartInitializingSingleton, an interface whose afterSingletonsInstantiated() method is called once all non-lazy singletons have been created.
In that method the processor asks the bean factory for every bean name, of any type and scope. For each name it works out the bean's target class without creating the bean, skips scoped-proxy internals, and looks for methods annotated with @EventListener, including meta-annotations such as @TransactionalEventListener. For each method found, it asks the registered EventListenerFactory beans to build a listener. The default factory builds an ApplicationListenerMethodAdapter; the transaction support adds a factory that builds a transactional variant. Each adapter is then added to the context's list of listeners.
- All non-lazy singletons createdcontext refresh is nearly done
- afterSingletonsInstantiated()
EventListenerMethodProcessorstarts - For every bean nameany scope, target class resolved without creating it
- Has @EventListener methods?YesCreate one adapter per methodNoRemember the class and skip it
- Adapter stores bean name and methodnot the bean instance
- Add the adapter to the contextthe multicaster can now deliver events to it
6 steps in order. Play them, or step through them one at a time.
Because the adapter stores only a name, scope rules apply each time an event arrives.
2What Happens on Each Event
When an event is published, the multicaster asks each adapter whether it supports the event type, which it decides from the method's parameter type. Matching uses ResolvableType, Spring's representation of a full generic type, so a method taking EntityCreated<Order> does not match an EntityCreated<Customer>, provided the event exposes its generic type.
If the type matches and the condition, if any, is true, the adapter calls getTargetBean(), which is simply applicationContext.getBean(beanName), and invokes the method by reflection. This lookup on every event is the key to the rest of this answer: the adapter does not hold on to an instance, it asks the container each time, and the container answers according to the bean's scope.
3Why a Prototype Listener Forgets Its State
A prototype bean is one for which the container creates a new instance on every getBean() call. Combine that with the adapter's lookup and each event gets a fresh object.
The count never passes 1, because each event is handled by a new instance that is thrown away afterwards. Worse, if the prototype holds resources, each event creates and abandons one, since the container does not manage the destruction of prototype beans. So listeners should almost always be singletons, and state that must survive between events belongs in a singleton or in storage.
4Lazy and Scoped Beans
A lazy singleton, one marked @Lazy, is not created during startup. The processor can still find its listener methods, because it resolves the class from the bean definition. The adapter is registered, and the first matching event calls getBean(), which creates the bean at that moment. So a bean meant to be lazy becomes initialised by an event, possibly on a request thread, and any slow initialisation happens in the middle of the publisher's call. If the bean's class cannot be resolved without creating it, the processor skips it, and then its listeners are never registered at all.
Scoped beans with a scoped proxy, such as a request-scoped bean, are handled through the proxy's bean name. The adapter gets the proxy, and invoking the method looks up the current request's instance. If the event is published outside a web request, for example from a scheduled job, the call fails with an error saying the scope is not active. The general lesson is the same: the scope is applied at the moment of each event, not once at startup.
5Proxies and Method Visibility
Many beans are proxies, for example when they have @Transactional or @Async methods. The adapter invokes the listener method on what getBean() returns, which is the proxy, so the method must be callable through it. For a class-based CGLIB proxy, a private method cannot be overridden or delegated, and Spring fails at startup with a message asking us to change the method's visibility to package or protected. For an interface-based JDK proxy, the method must be declared on one of the interfaces the proxy exposes.
Notice that a private listener on a bean that is not proxied works, which is why this failure appears suddenly when someone adds @Transactional to an unrelated method of the same class. Making listener methods package-private or public by habit avoids the surprise.
6Follow-up questions
What is the difference between implementing ApplicationListener<E> and using @EventListener?
ApplicationListener is an interface: the bean itself is the listener, it handles one event type through onApplicationEvent, and it is registered as a bean or added to SpringApplication, which also lets it receive early Boot events. @EventListener is method-level: one bean can have many listener methods, and it adds conditions, return-value publishing, the classes attribute and the transactional variant. Under the hood, @EventListener methods are wrapped in an adapter that implements ApplicationListener, so the delivery path is the same.
Do @EventListener methods on beans registered after startup work?
The processor runs once, after the singletons are created. A bean definition added later is not scanned for listener methods automatically, so its methods are not registered. For dynamic listeners, we add an ApplicationListener to the context in code.
Does the adapter look up the bean on every event even for singletons?
Yes, it calls getBean() each time, but for a singleton the container returns the cached instance from a map, so the cost is tiny. The lookup only changes behaviour for non-singleton scopes and lazy beans.
7Conclusion
In this answer, we saw that EventListenerMethodProcessor scans every bean definition after the singletons are created and registers one ApplicationListenerMethodAdapter per @EventListener method. The adapter stores the bean name and calls getBean() on each event, so a prototype listener gets a new instance per event and a lazy bean is created by its first event, while scoped proxies need an active scope. Matching uses the parameter's full ResolvableType, and on proxied beans listener methods must not be private.
Open with
EventListenerMethodProcessor is a SmartInitializingSingleton, so once all non-lazy singletons are created, it goes through every bean name in the factory, resolves each bean's class without instantiating it, and looks for @EventListener methods, including meta-annotations like @TransactionalEventListener.
Then, if they want more
For each method it uses an EventListenerFactory to create an ApplicationListenerMethodAdapter and adds that to the context. The important detail is that the adapter stores the bean name, not the instance.
On every event, it checks the event type against the method parameter, using ResolvableType so generics count, evaluates any condition, and then calls getBean with that name and invokes the method. That is why scope rules apply at invocation time.
A prototype bean gets a new instance for every event, so any field state is lost and the count in a counter never goes past one. A lazy singleton is created by its first matching event, possibly in the middle of a request. A request-scoped bean fails if the event is published outside a request.
Listener detection also only happens once, for bean definitions known at that point. And because the method is invoked on the proxy if the bean is proxied, it cannot be private; Spring fails at startup and asks for package or protected visibility. In practice I keep listeners on singleton beans with non-private methods.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does Spring turn @EventListener methods into listeners, and why can a listener on a...": https://scrivio-interview-library.netlify.app/spring-boot/spring-events/994edddd34/