代替
Factory<T>.dispose(T)
,在注射剂上注册
CloseableService
可以做你想做的大部分事情。A.
Closeable
将需要适配器。
可关闭服务
closes()
退出请求范围时的所有注册资源。
class MyEntityManagerFactory implements Factory<EntityManager> {
private final CloseableService closeableService;
EntityManagerFactory emf;
@Inject
public MyEntityManagerFactory(CloseableService closeableService) {
this.closeableService = checkNotNull(closeableService);
emf = Persistence.createEntityManagerFactory("manager1");
}
@Override
public void dispose(EntityManager em) {
em.close();
}
@Override
public EntityManager provide() {
final EntityManager em = emf.createEntityManager();
closeableService.add(new Closeable() {
public final void close() {
em.close();
}
});
return em;
}
}