We can listen for the ApplicationFailedEvent for dealing with Exceptions occurring during the startup of spring boot application. Attaching the sample code how i dealt with same.
`@Component public class PortConflictListener implements ApplicationListener {
private static final Logger log = LoggerFactory.getLogger(PortConflictListener.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("custom error message");
System.exit(1);
}
}
} `