2
我有一个单一资源,在其构造函数中创建对象,当应用程序关闭,服务器终止时,我需要释放这些对象。泽西岛2队是如何完成的?泽西岛2资源需要清理
@Path("/")
@Singleton
public class MyResource {
private Map<String, MyObject> cache;
public MyResource() {
cache = new ConcurrentHashMap<>();
// at some point I need to remove all entries
// from the map and close all MyObject objects there
//
// the reason is because MyObject might have files open
// and I need to close the files
//
// where can I do that?
}
...
}
您需要更具体的说,“在某些时候”之前。在/哪一点?容器管理生命周期的重点在于那些点被定义并成为配置项目。手动调用“释放/清除这些”方法完全滥用整个想法。 – BadZen
谢谢。我将编辑该问题。 – akonsu
(另外,作为一般来说,你可能不希望容器管理的生命周期是打开/关闭文件,这不是CDI的原因。) – BadZen