-
Notifications
You must be signed in to change notification settings - Fork 824
Advanced Custom Bindings
This document proposes a more fine grained approach to the creation of custom bindings than Your First Custom Binding.
A custom binding allows you to specify which implementation of an interface should be used when injecting that interface. This allows you to easily change implementations of an interface throughout the codebase from a single location.
To create a custom binding you need to take the following steps:
- Create a custom application class
- Create a Module that binds the interface to a specific class
- Register this module with Roboguice
- (Optional) Using a Provider that provides the dependency
This should be familiar to you, if not read this first. Note that your application class does not have to extend a Roboguice base class.
The module's sole purpose here is to link the interface annotated with @Inject
(IFoo) to a specific implementation of that interface (in this case SimpleFoo). The module also implements an interface which is (surprisingly) called Module
.
public class MyModule implements Module {
@Override
public void configure(Binder binder) {
binder.bind(IFoo.class).to(SimpleFoo.class);
}
}
This tells RoboGuice: If we want to Inject IFoo
; use SimpleFoo
.
Up until now RoboGuice doesn't know about our module. The best place to do this is in the Application
class onCreate
method.
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
RoboGuice.setBaseApplicationInjector(this, RoboGuice.DEFAULT_STAGE,
RoboGuice.newDefaultRoboModule(this), new MyModule());
}
}
Note that we need to reregister the DefaultRoboModule
. If we don't do this all the default Android magic will be gone that is provided by RoboGuice.
That's it. When we now @Inject IFoo foo
the IFoo instance will be SimpleFoo. You can add as many modules as you want.
If you want to use special logic for constructing your dependency than you will need a Provider
.
Your provider should implement the Provider
interface which just contains one method get
. This method should return an instance of you class. Basically this works just like a normal Factory.
public class FooProvider implements Provider<IFoo> {
@Override
public IFoo get() {
return new ComplexFoo(new Bar());
}
}
Now instead of just binding IFoo
to SimpleFoo
we need to tell RoboGuice about our FooProvider
.
This is done inside our module.
public class MyModule implements Module {
@Override
public void configure(Binder binder) {
binder.bind(IFoo.class).toProvider(FooProvider.class);
}
}
Now every time we do @Inject IFoo foo
it will use the provider to give us an instance of IFoo. This allows you to do more complex object creation.
- For Projects Using Gradle
- For Projects Using Maven
- Manual Installation for Older Projects
- Inheriting from RoboGuice Classes
- Upgrade Instructions from RoboGuice 2
- Your First View Injection
- Your First Resource Injection
- Your First System Service Injection
- Your First POJO Injection
- Singletons and ContextSingletons (wiki/Understanding Scopes)
- Your First Custom Binding
- Your First Injected Fragment
- Your First Injected Service and BroadcastReceiver
- Your First Testcase
- Your First Injection into a Custom View class
- Your First Injected ContentProvider
- Using Events in your RoboGuice application
- Logging via Ln
- RoboGuice Standard Injections
- How Injection Works
- When Injection Just Works, and when you have to call injectMembers()
- The Difference between Global and Context-scoped Injection
- Analyzing a Guice Stack Trace
- What's the difference between Nullable and Optional?
- RoboBlender wiki
- Use ProGuard with RoboGuice
- Define advanced custom bindings
- Remove or replace RoboGuice's default bindings
- Use your own BaseActivity with RoboGuice
- Inject into an object that you don't instantiate
- Dealing with Circular Dependencies
- Work with Library Projects
- Deal with ComputationException due to StackOverflowError
- Taming Fragmentation using RoboGuice