@Singleton with @IOCProvider -- inject different instances
crinaldi Mar 18, 2013 7:14 PMHello people:
I making a implementation of Activities and Places with errai ui, by the way, I made a serie of Providers.
EventBusProvider, PlaceHistoryHandlerProvider, PlaceControllerProvider
All this Providers are made in the same way, by example:
@IOCProvider
@Singleton
public class EventBusProvider implements Provider<EventBus>{
@Override
public EventBus get() {
return new SimpleEventBus();
}
}
But when I inject the event bus in other provider or in other classes, this is "singleton" only in the instances of the same type.
This is the output of Hosted Mode:
1 - 00:00:06,286 [INFO] Loading module app
2 - 00:00:26,024 [DEBUG] Rebinding org.jboss.errai.marshalling.client.api.MarshallerFactory
3 - 00:00:29,745 [DEBUG] Rebinding org.jboss.errai.ioc.client.QualifierEqualityFactory
4 - 00:00:29,998 [DEBUG] Rebinding org.jboss.errai.ioc.client.Bootstrapper
5 - 00:00:32,508 [INFO] com.google.gwt.event.shared.SimpleEventBus@68de52 PlaceControllerProvider
6 - 00:00:32,932 [INFO] com.google.gwt.event.shared.SimpleEventBus@381b58 PlaceHistoryHandlerProvider
7 - 00:00:32,962 [INFO] com.google.gwt.event.shared.SimpleEventBus@68de52 PlaceControllerProvider
8 - 00:00:33,234 [INFO] Init Layout OK
9 - 00:00:33,284 [INFO] LoginView init Success
10 - 00:00:33,293 [INFO] com.google.gwt.event.shared.SimpleEventBus@6fe1e1 MVP
11 - 00:00:33,298 [INFO] class com.google.gwt.activity.shared.ActivityManager Injected
12 - 00:00:33,302 [INFO] Center ActivityManager init Success
13 - 00:00:33,643 [INFO] UI Constructed!
14 - 00:00:33,728 [INFO] Module app has been loaded
As you can see, in the line 5 and 7, the instance of EventBus is the same.
But, whe I inject the event bus in PlacehistoryHandlerProvider the instance is other (line 6), the same goes when I inject EventBus in other class who MVPModule (line 10)
Some code of other providers and classes:
@IOCProvider
@Singleton
public class PlaceControllerProvider implements Provider<PlaceController>{
@Inject
EventBus eventBus;
@Override
public PlaceController get() {
GWT.log(eventBus.toString() + " PlaceControllerProvider");
return new PlaceController(eventBus);
}
}
-------------------------------------------------------------------------------------------
@IOCProvider
@Singleton
public class PlaceHistoryHandlerProvider implements Provider<PlaceHistoryHandler> {
@Inject
PlaceHistoryMapper mapper;
@Inject
EventBus eventBus;
@Inject
PlaceController placeController;
@Override
public PlaceHistoryHandler get() {
PlaceHistoryHandler handler = new PlaceHistoryHandler(mapper);
mapper = Preconditions.checkNotNull(mapper, "PlaceHistoryMapper is NULL");
eventBus = Preconditions.checkNotNull(eventBus, "EventBus is NULL");
GWT.log(eventBus.toString() + " PlaceHistoryHandlerProvider");
placeController = Preconditions.checkNotNull(placeController, "PlaceController is NULL");
handler.register(placeController, eventBus, new LoginPlace());
return handler;
}
}
----------------------------------------------------------------------------------------------------------------------------------------
@ApplicationScoped
public class MVPModule {
@Inject
private EventBus bus;
@Inject
private CenterActivityMapper centerMapper;
@Inject
private PopupActivityMapper popupMapper;
@Inject
@CenterRegion
private ActivityManager centerActivityManager;
@Produces
@Singleton
@CenterRegion
public ActivityManager getCenterActivityManager() {
return new ActivityManager(centerMapper, bus);
}
@PostConstruct
private void init() {
bus = Preconditions.checkNotNull(bus, "Event Buss is Null");
GWT.log(bus.toString() + " MVP");
centerActivityManager = Preconditions.checkNotNull(centerActivityManager, "Center ActivityManager is Null");
GWT.log(centerActivityManager.getClass().toString() + " Injected");
}
public void setDisplayRegions(MainLayout layout) {
layout = Preconditions.checkNotNull(layout, "Popup ActivityManager is Null");
centerActivityManager.setDisplay(layout.getCenterReqion());
GWT.log("Center ActivityManager init Success");
}
}
I imagine the same will happen with:
PlaceController and PlaceHistoryHandler ....
I have a conceptual mistake?
I am creating and / or injecting something wrong?
Any idea?
Thank you!!