Gary Russell opened SPR-16308 and commented

I am not sure if this is a bug, or intended, but resolvableDependencies in DLBF.findAutowireCandidates() (e.g. ApplicationEventPublisher ) trump any user-defined beans.

public class So47793485aApplication {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
        MyBean bean = ctx.getBean(MyBean.class);
        Assert.isTrue(bean.publisher.equals(ctx.getBean("publisher")),
                () -> "publisher is not 'publisher'; it's " + bean.publisher);
        ctx.close();
    }

    @Configuration
    @Import(MyBean.class)
    public static class Config {

        @Bean
        public ApplicationEventPublisher publisher() {
            return object -> System.out.println(object.toString());
        }

    }

    @Component
    public static class MyBean {

        private final ApplicationEventPublisher publisher;

        @Autowired
        public MyBean(ApplicationEventPublisher publisher) {
            this.publisher = publisher;
        }


    }
}

The ApplicationContext is wired in as the publisher instead of the user's publisher.


Affects: 5.0.2

Comment From: snicoll

Yes, this is the expected behavior. There are a number of beans that are core and for which user-bean resolution won't happen at all. In this case, the event listener is required very early on, so it needs to be set on the ApplicationContext prior for it being refreshed.