Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalency of Guice Provider in Spring

Tags:

java

spring

guice

What is the equivalency of Guice's Provider in Spring?

Here is the Guice code I need to replace with Spring:

public class MyProxyProvider implements Provider<MyProxy> {

    @Inject
    Config config;

    @Override
    public MyProxy get() {
        return new MyProxy(httpsclient, config.server, config.user, config.password, config.version);
    }

}

and here the binding is defined:

public class MyModule implements Module {

    @Override
    public void configure(Binder g) {
        g.bind(MyProxy.class).toProvider(MyProxyProvider.class);
    }

}

Finally, my goal is use @Autowired for the proxy object as follows:

public class ConnectionTest {

    @Autowired
    MyProxy proxy;
}

Also note, that MyProxy class in in the external jar file that cannot be modified.

like image 383
luksmir Avatar asked May 08 '13 05:05

luksmir


People also ask

Can I use Guice with Spring?

Using Guice as an API for accessing a Spring ApplicationContext. In this case the main feature is an Injector implementation that wraps a Spring ApplicationContext . Example: AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.

Which is better Guice or Spring?

Spring allows you to omit the @Autowired annotation when there's only one constructor. Guice allows binding to a Provider, as well as injecting a Provider of your class, even when your class has no Provider binding.

What does bind do in Guice?

Guice Basic Bindings. Binding is to Guice as wiring is to Spring. With bindings, we define how Guice is going to inject dependencies into a class. This module implementation specifies that an instance of DefaultCommunicatorImpl is to be injected wherever a Communicator variable is found.

What is the use of Spring boot framework?

Spring Boot helps developers create applications that just run. Specifically, it lets you create standalone applications that run on their own, without relying on an external web server, by embedding a web server such as Tomcat or Netty into your app during the initialization process.


1 Answers

The equivalent to the Provider of guice is the FactoryBean in Spring.

From the documentation:

The FactoryBean interface is a point of pluggability into the Spring IoC container's instantiation logic. If you have complex initialization code that is better expressed in Java as opposed to a (potentially) verbose amount of XML, you can create your own FactoryBean, write the complex initialization inside that class, and then plug your custom FactoryBean into the container.

Sample

public class MyFactoryBean implements FactoryBean<MyClassInterface> {

    @Override
    public MyClassInterface getObject() throws Exception {
        return new MyClassImplementation();
    }

    @Override
    public Class<?> getObjectType() {
        return MyClassInterface.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

}

This is a good approach when you have external libraries and complex object creation to avoid long XML configuration to setup your beans.

In order to use it within your context, you could just provide it as a normal bean within your context XML like:

...
<bean id="myClass" class="foo.bar.MyFactoryBean" />
...

BUT, if your bean is quite simple to be instantiated (not lots of dependencies), you can setup it directly on the context XML like:

<bean id="myClass" class="foo.bar.MyClassImplementation" />

Alternative

If you use Spring 3 you could write a configuration class (class annotated with @Configuration) as documented here. With this approach you could do something like:

@Configuration
public class MyConfiguration {

    @Bean
    public MyClassInterface getMyClass() {
        return new MyClassImplementation();
    }

}

This would add your instance to spring context and let autowire it within other classes. But some configuration is required, and its quite easy following the documentation provided.

like image 162
Francisco Spaeth Avatar answered Sep 22 '22 04:09

Francisco Spaeth