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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With