graceful handling of exception thrown while creating beans

graceful handling of exception thrown while creating beans  using -'spring,exception-handling'

I want my Spring MVC web app to gracefully handle a particular type of exception thrown while creating the beans.

The construction of one of my beans reads configuration data from an external file. If that configuration data is faulty, one of my bean constructors will throw an exception of a particular type. As the cause of the problem would be a faulty configuration file, I want my web application to respond with a useful log message and/or an error page, rather than a stack-trace of the thrown exception. So I guess I need some kind of exception handler hooked into the IOC container or dispatcher servlet. How can I do that?

Just to be clear. I'm asking about exceptions thrown as the servlet is initialising, not as it handles HTTP requests, so @ExceptionHandler annotations on controllers are not useful.

asked Oct 7, 2015 by jekbishnoi
0 votes

1 Answer

0 votes

I worked around this difficulty by introducing a level of indirection. My bean is really just a handle. If the configuration file is bad, the bean catches the exception, logs a message, and notes that reading failed. Accessing the bean later then throws a suitable exception.

answered Oct 7, 2015 by param.oncemore