configuring Guice DI within Spring Environment and general configuration question

2,097 views
Skip to first unread message

zixzigma

unread,
Feb 23, 2011, 2:13:27 PM2/23/11
to google...@googlegroups.com
Hello Everyone,

I am using Spring framework on the serverside.
SpringSecurity, REST, MVC, etc.

however for DI I prefer to use Guice.
with Spring 3.0, @Autowired can accomplish same thing as @Inject,
but when writing my own code, I would like to use Guice, because of
Providers, Assisted Inject, easier AOP, etc.
I do not want to get rid of Spring IoC container entirely,
because Spring uses it internally, but I would like to adopt Guice to use DI in my own code.


- do you have any suggestion on this ? is this a bad idea ?

- do you know how I can configure Guice within Spring ?

My problem is bootstrapping Injectors, eg.
Injector injector = Guice.createInjector(new MyModule());
Foo foo = injector.getInstance(Foo.class);

I believe Guice.createInjector should be created once during bootstrapping,
When integrating with Spring, I would like to know specifically what I should register with Spring IoC.
and do I have to use injector.getInstance(xxx.class) all over my code anytime I would like to get hold of a instance of a class ?
if not, what is the purpose of injector.getInstance(xxx.class) ?

Thank You

Brian Lough

unread,
Mar 1, 2011, 10:36:50 PM3/1/11
to google...@googlegroups.com
I'm using both, but I rarely mix the two.  So far I've only made Spring bindings available to Guice (via http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/spring/SpringIntegration.html).  

I don't see any reason why Guice couldn't be configured within Spring: I'd just question if it's necessary.  Perhaps someone else has more feedback.

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

zixzigma

unread,
Mar 1, 2011, 11:05:18 PM3/1/11
to google...@googlegroups.com
could you please explain for what purpose you use each of Guice and Spring in your development ?
how you separate the responsibility ?

the following features in Guice I would like to use, which I don't think are available in Spring, I can be wrong.
1- Providers (is Spring FactoryBean serving similar purpose ?)
2-Assisted Inject
3- Modular approach, having GinModules, and adding them to injector. (is Spring @Bean Java Configuration similar to GinModules?)

Thank  You

Brian Lough

unread,
Mar 2, 2011, 10:06:33 AM3/2/11
to google...@googlegroups.com
I use individual components of Spring for targeted tasks: JMS, JDBC legacy interfaces, etc.  The XML I keep as lean as possible (the less XML the more I enjoy my job).  The Spring context is manually initialized as I use Guice Filter as my entry point in the Web container. The various Test harnesses, obviously, also manually initialize the two DI contexts.

The equivalent of features 1-3 mentioned below are indeed provided by Spring, though the semantics differ. (import versus install for instance).



Thank  You

--

zixzigma

unread,
Mar 2, 2011, 12:29:18 PM3/2/11
to google...@googlegroups.com
- so when you use Spring Component, for JMS, etc
you inject your own components/classes using Guice ?


- and by manually initializing Spring,
do you mean using ApplicationContext context = new ClassPathXmlApplicationContext(xxx)
in a Guice module ? and then you Guice @Inject this spring ApplicationContext ?

- don't you need to annotate your classes with @Component, or somehow tell spring about the beans you want to have access to spring services ?
 and if you have a constructor with dependencies injected with Guice @Inject, and with no default constructor, Spring cannot instantiate your class,
and gives error ? or you never let Spring instantiate anything ?

Thank You

Kartik Kumar

unread,
Mar 2, 2011, 2:55:24 PM3/2/11
to google...@googlegroups.com, zixzigma
If you want to use Spring api to create and reference an injector, then you can implement Spring's FactoryBean api to return Guice's injector. In your xml (not sure how to do it with annotations)

<bean id = "injector" class = "MyInjectorFactoryBean">
  <property name = "modules" values="Module1, Module2, Module3" />
  ......
</bean>

To set up an injector,  you need to implement getObject method. To instantiate the modules, you can do it two ways.

1. The modules property could be set up  as a String array and then instantiate the modules one by one to set up in your Factory implementation using Class.forName and newInstance methods.
2. You delegate it to a Java or Spring's PropertyEditor api to transform fully qualified module path names to modules. The link might prove useful. http://static.springsource.org/spring/docs/2.0.x/reference/validation.html#beans-beans-conversion




Thank You

--
Reply all
Reply to author
Forward
0 new messages