Sometimes my beans may not initialize properly due to external factors. For example, a MongoDB instance is not connected to the network. Is there an elegant way to handle failed bean initializations? The following is a bean:
@Bean public MorphiaDataSource morphiaDataSource() { try { MorphiaDataSource bean = new MorphiaDataSource(); Mongo mongo = new Mongo(mongoHost, mongoPort); bean.setMongo(mongo); bean.setMorphia(new Morphia()); bean.setDatabase(mongoDatabase); bean.setUsername(mongoUsername); bean.setPassword(mongoPassword); return bean; } catch(Exception e) { logger.error("Error creating MorphiaDataSource: " + e.getMessage());
source share