Based on spring documentation @ControllerAdvice is annotated with @Component which means it will be scanned by Spring Boot normally.
So, let's focus on your calss implementation, I see you are depending on the @ExceptionHandler(ApplicationContextException.class) although you want to listen to application failed event, right? I have good news ! there is something ready to use already provided by springboot, the ApplicationFailedEvent.
All what you need is just to create a custom expetion handler that listen for the ApplicationFailedEvent, I have tweaked you code submitted above to work as the explanasion above:
@ControllerAdvice
public class GenericExceptionHandler implements ApplicationListener<ApplicationFailedEvent> {
private static final Logger log = LoggerFactory.getLogger(GenericExceptionHandler.class);
@Value("${server.port}")
private String serverPort;
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
Throwable exception = event.getException();
if (exception instanceof ApplicationContextException
&& exception.getCause() instanceof PortInUseException) {
log.error("Port Already in use, Refer Step 3.1 for configuring new port");
System.exit(1);
}
}
}