Prasanna
Prasanna

Reputation: 3771

Continue to load webapp even if one spring bean initialization fails

So if spring initialization fails in a webapp then the webapp itself does not come up. To prevent this, I can probably not re-throw any exception from my code for that specific bean initialization and the webapp will continue to load, right?

Is there any other way to tell to spring not to fail the webapp itself on particular bean initialization failure?

Upvotes: 5

Views: 4347

Answers (3)

Deniz
Deniz

Reputation: 31

Have attribute lazy-init="true" in that bean and every dependant bean. Link for more details.

Upvotes: 3

Stephen C
Stephen C

Reputation: 718788

Continue to load webapp even if one spring bean initialization fails

AFAIK, you can't do this.

I do multiple DNS lookups on start up. I do not want the webapp to fail if one of them fails.

In that case, you need to modify your bean(s) to handle the case where the DNS lookup fails, leaving the bean instance in a state where it is essentially inactive but harmless, or where it can retry the DNS lookup later on.

In short, you have to cope with this yourself.

Upvotes: 8

hvgotcodes
hvgotcodes

Reputation: 120188

why would you do this? If your spring context is not correct, something is seriously wrong and there will be issues. The correct way to deal with this is to fix the application context.

Upvotes: 0

Related Questions