Hi,

I'm looking for advice on how to best integrate Spring with Guice.
Basically, I want to be able to use Spring managed modules with Guice
managed ones. Guice Spring does that, but the implementation is a bit
simplistic. Particularly, the (very typical) case where you'd define
the implementing class in the Spring config, but in code refer to the
interface isn't supported.

E.g. (in applicationContext.xml):

<bean id="springGreeter"
        class="com.foo.DefaultSpringGreeter">
</bean>


Key<SpringGreeter> key = Key.get(SpringGreeter.class, Names
                .named("springGreeter"));
SpringGreeter springGreeter = Dependencies.getInstance(key);

(SpringGreater is the interface that is implemented by DefaultSpringGreeter).

The problem is that only the implementation type is registered. A
quick fix in the Spring support package is this:

        private static final Pattern IGNORE_PACKAGES = Pattern
                        .compile("^javax?\\.|^com.sun\\.");

        static void bindAll(Binder binder, ListableBeanFactory beanFactory) {
                binder = binder.skipSources(SpringBindings.class);
                for (String name : beanFactory.getBeanDefinitionNames()) {
                        Class<?> type = beanFactory.getType(name);
                        for (Class<?> intf : type.getInterfaces()) {
                                if 
(!IGNORE_PACKAGES.matcher(intf.getName()).find()) {
                                        bindBean(binder, beanFactory, name, 
intf);
                                }
                        }
                        bindBean(binder, beanFactory, name, type);
                }
        }

but it's a bit brute force. Is there a nicer way to do this? Any other
pitfalls when it comes to integrating Spring with Guice?

Cheers,

Eelco

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

Reply via email to