typeEncounter.register(new InjectionListener<I>() {
@Override
public void afterInjection(Object object) {
final View view = (View) object;
final Iterable<ViewMBean> viewMamanagementBeans = viewManagementOf(view);
final Closer closer = view.unwrap().utils().injector().getInstance(Closer.class);
//We get the name from the view and not from the view management object to avoid proxy issues.
final String name = view.unwrap().getName();
managementContext.register(view);
//Manage the created management view objects to the context.
for (ViewMBean viewMBean : viewMamanagementBeans) {
managementContext.manage(viewMBean, name);
}
//Add the the management view objects to the Closer, so that they are unregistered on close.
closer.addToClose(new Closeable() {
@Override
public void close() throws IOException {
for (ViewMBean viewMBean : viewMamanagementBeans) {
managementContext.unmanage(viewMBean, name);
}