【发布时间】:2021-05-03 05:00:03
【问题描述】:
我有一个类,它调用两个单例类 FirstClass 和 SecondClass,如下所示。有没有办法访问在 SecondClass 中的 FirstClass 中计算的数据。在这种情况下,我不想在第二堂课中进行外部服务调用,因为第一堂课已经调用了它。相反,只需在第二个数据函数中使用数据(存储在第一类函数中)。 Spring Framework有哪些实现方式?
public class CallingFunction() {
List<String> generateData() {
return Lists.newArrayList(new FirstClass(), new SecondClass())
}
@Singleton
public class FirstClass() extends interface {
public String function() {
//operations. This function calls multiple services and stores ouput to hashMap
Map<String, String> hashedData = Maps.newHashMap();
hashedData.put(dataFromAnotherService);
return hashedData.get("abc");
}
}
@Singleton
public class SecondClass() extends interface {
public String function() {
//Use hashedData here instead of invoking the service again.
//Other operations
return "data";
}
}
【问题讨论】: