smeeb
smeeb

Reputation: 29507

Spring Boot ApplicationListener not running at app startup

I have created a spring-boot-troubleshooting repo on GitHub that reproduces this error exactly.

I am building a Spring Boot-based REST service and am having difficulty getting a startup listener to work:

@Slf4j
class StartupListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    ScheduledReporter metricReporter

    @Override
    void onApplicationEvent(ContextRefreshedEvent event) {
        log.info('StartupListener is starting...')
        metricReporter.start(1, TimeUnit.SECONDS)
    }
}

When I run the app:

./gradlew build && java -Dspring.config=. -jar build/libs/spring-boot-troubleshooting.jar

Everything starts up without errors/exceptions, however I never see my "StartupListener is starting..." log message print to the console. This tells me Spring is not firing up my StartupListener. Any ideas what the fix is?

Upvotes: 2

Views: 3783

Answers (1)

pvpkiran
pvpkiran

Reputation: 27048

This class is not a spring bean(I looked into the file in repo as well), So spring hasn't scanned this at all. And hence this is never called.
Try adding @Component for this class.

Upvotes: 5

Related Questions