Little Problem with jo4neo and google guice.

1 view
Skip to first unread message

Stefan Berndt

unread,
Jun 16, 2010, 6:00:32 AM6/16/10
to jo4neo
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.



Peter Neubauer

unread,
Jun 16, 2010, 4:41:54 PM6/16/10
to jo4...@googlegroups.com
Stefan,
any progress on this? If not, do you have the full example to look at?
I could try it out tomorrow ...

Cheers,

/peter neubauer

COO and Sales, Neo Technology

GTalk: neubauer.peter
Skype peter.neubauer
Phone +46 704 106975
LinkedIn http://www.linkedin.com/in/neubauer
Twitter http://twitter.com/peterneubauer

http://www.neo4j.org - Your high performance graph database.
http://www.thoughtmade.com - Scandinavia's coolest Bring-a-Thing party.

Taylor Cowan

unread,
Jun 16, 2010, 11:45:01 PM6/16/10
to jo4...@googlegroups.com
I think Guice injection happens by way of using guice to instantiate
objects...so I'm thinking this would require a jo4neo/guice
integration such that the jo4neo graph is primed with Guice's factory
(sorry, forgot what it's called)...and would use that to get new
instances.

Peter Neubauer

unread,
Jun 17, 2010, 1:04:09 AM6/17/10
to jo4...@googlegroups.com
Stefan,
you can see one of these GuiceBindingModules in
https://svn.neo4j.org/examples/imdb-osgi/trunk/org.neo4j.examples.imdb.neoservice/src/main/java/org/neo4j/examples/imdb/neoservice/internal/GuiceBindingModule.java
. It tells Guice what to instantiate and then inject into something
else, for instance the Activator, see
https://svn.neo4j.org/examples/imdb-osgi/trunk/org.neo4j.examples.imdb.neoservice/src/main/java/org/neo4j/examples/imdb/neoservice/internal/ExampleActivator.java.

Does that help?

Cheers,

/peter neubauer

COO and Sales, Neo Technology

GTalk: neubauer.peter
Skype peter.neubauer
Phone +46 704 106975
LinkedIn http://www.linkedin.com/in/neubauer
Twitter http://twitter.com/peterneubauer

http://www.neo4j.org - Your high performance graph database.
http://www.thoughtmade.com - Scandinavia's coolest Bring-a-Thing party.

Reply all
Reply to author
Forward
0 new messages