79325819

Date: 2025-01-03 08:01:23
Score: 0.5
Natty:
Report link

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);
    }
}

} `

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: basil daniel