Bob Tiernay opened SPR-13155 and commented

This would be very useful for conditional processing of startup methods, etc:

@EventListener
@Profile("simulation")
public void start(ApplicationReadyEvent ready) {
    simulator.simulate();
}

No further details from SPR-13155

Comment From: spring-projects-issues

Stéphane Nicoll commented

That's very unusual in style. I'd rather invite you to add a condition to the component that emit the event or a condition on the bean on which your start method is defined. Thanks.

Comment From: mwisnicki

@snicoll but if you disable whole bean you won't be able inject it into tests so the only solution becomes the ugly

@EventListener
public void start(ApplicationReadyEvent event) {
    if (event.getApplicationContext().getEnvironment().acceptsProfiles(Profiles.of("test")))
        return;
    doSomething();
}

or separating listener to another bean which is also unnecessarily ugly.

I have a warmup service and if I don't disable it, it's going to execute for every test but I still want to test it once so I can't completely disable the bean. Using @Profile("!test") on event listener seems like the cleanest solution IMHO.

Comment From: snicoll

Using @Profile("!test") on event listener seems like the cleanest solution IMHO.

Sorry, but it isn't IMO. @Profile is a bean factory feature, determining whether the whole bean is going to be added to the bean factory or not. Just because it is an annotation you could put elsewhere doesn't mean it's the right architectural choice. If you need the event listener to fire conditionally, you could separate it in its own class and not register it in test like you'd do for any other bean.