假设我在我的Web应用程序中有一个名为class“Foo”的类。它有一个initialise()方法,当使用Spring创建bean时会调用它。 initialise()方法然后尝试加载外部服务并将其分配给一个字段。如果服务无法联系,则该字段将设置为空。Java Web应用程序同步问题
private Service service;
public void initialise() {
// load external service
// set field to the loaded service if contacted
// set to field to null if service could not be contacted
}
当有人调用get()方法的类“富”,如果它是在INITIALISE()方法启动该服务将被调用。如果服务的字段为空,我想尝试加载外部服务。
public String get() {
if (service == null) {
// try and load the service again
}
// perform operation on the service is service is not null
}
如果我能做这样的事情,我可能有同步问题吗?
谢谢!重构的好主意;) – digiarnie 2008-10-22 23:57:55