Hello I use Google Guice for injecting my objects especiialy my
DatabaseService.class which holds the Objectgraph.
But when I'm getting my objects from the graph with
ObjectGraph.get(classname), the injector and the databaseservice is
not injected (null).
f.ex.:
package data;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.googlecode.guicejunit4.core.InjectWith;
import guice.InjectorProviderA;
import jo4neo.Nodeid;
import jo4neo.neo;
import service.DatabaseService;
@InjectWith(InjectorProviderA.class)
public class BasicItem {
// ------------------------------ FIELDS
------------------------------
@Inject
Injector injector;
@Inject
DatabaseService ds;
transient Nodeid node;
@neo
private String name;
// --------------------------- CONSTRUCTORS
---------------------------
public BasicItem() {
}
@Inject
public BasicItem(String name, DatabaseService ds, Injector
injector) {
this.name = name;
this.ds = ds;
this.injector = injector;
}
// --------------------- GETTER / SETTER METHODS ---------------------
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setDs(DatabaseService ds) {
this.ds = ds;
}
public void setInjector(Injector injector) {
this.injector = injector;
}
// -------------------------- OTHER METHODS --------------------------
public void delete() {
ds.getGraph().delete(this);
}
}
if I call now my BasicItems entities with get() i get null in the
injector and databaseservice field. So i have to set the
DatabaseService and the injector manually:
for (BasicItem basicItem : ds.getAllBasicItems()) {
basicItem.setInjector(injector);
basicItem.setDs(ds);
}
This works but it is not really nice, because i would set this very
often in my classes. Maybe can you help me for a smarter solution.
injector and and databaseservicefield should be injected everytimes
when a BasicItem Object is called either from the graph or via guice
or constructor invoke.