4
有時我的豆因外部因素無法正確初始化。比如MongoDB實例不在線。有處理失敗的bean初始化的優雅方式嗎?以下是有問題的豆:如何'優雅地'處理Spring 3應用程序中的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());
// Tell the context it's screwed?
}
return null;
}
優雅的以什麼方式?你想要發生什麼? – 2011-03-24 15:03:33
停止應用程序啓動? – 2011-03-24 18:19:52