-
Notifications
You must be signed in to change notification settings - Fork 1
Injecting Endpoint Proxies
To use an endpoint interface, a class which implements that contract needs to be created and instantiated. ZombieLink does this for you by generating a proxy which adheres to the endpoint interface and tailored for the HTTP communication demands specified via annotations. Instances of this proxy are then cached and injected into your code where requested.
####1. Wiring Endpoint Proxies
"...so that the
Zombiemay know where toBite."
Proxy injections are requested via the `@Bite` annotation. This annotation can be used on either on a **property** or a **constructor** depending on the type of injection you require.
######Property Wiring
@Bite
TwitterEndpoint twitterEndpoint;######Constructor Wiring
@Bite
public TwitterService(TwitterEndpoint twitterEndpoint) {
this.twitterEndpoint = twitterEndpoint;
}####2. Injecting Endpoint Proxies
"...getting the
Zombieto spread the infection."
To inject proxies use the utility methods on `Zombie`. Although injection works differently for property and constructor wiring, in both cases if the endpoint type is new, a proxy is generated, cached and injected. For successive injections of the same endpoint type, the cached proxy instance is used.
####a. Property Injection
Property injection works on an instance. Use the Zombie.infect(endpoint) method to inject the requested endpoint proxy.
- For each property, the first injection attempt is made directly on the field, provided that its accessible.
@Bite
public TwitterEndpoint twitterEndpoint;
{
Zombie.infect(this);
}- If the field is not accessible, a second injection attempt is made through its mutator, if available.
@Bite
private TwitterEndpoint twitterEndpoint;
{
Zombie.infect(this);
}
public void setTwitterEndpoint(TwitterEndpoint twitterEndpoint) {
this.twitterEndpoint = twitterEndpoint;
}- If a mutator is not found, a third and final attempt is made by changing the accessibility of the field.
@Bite
private TwitterEndpoint twitterEndpoint;
{
Zombie.infect(this);
}####b. Constructor Injection
Constructor injection works on a Class. Use the Zombie.infect(Endpoint.class) method to retrieve a new instance of the class which contains the endpoint references with the proxies injected.
- The initial injection attempt is made on the first
@Biteannotated constructor.
######TwitterService
private TwitterEndpoint twitterEndpoint;
@Bite
public TwitterService(TwitterEndpoint twitterEndpoint) {
this.twitterEndpoint = twitterEndpoint;
}######SocialShareController
private TwitterService twitterService;
public SocialShareController() {
this.twitterService = Zombie.infect(TwitterService.class);
}-
If it succeeds, property injection is performed on the created instance.
-
If it fails, instance creation is attempted via the default constructor and a property injection follows.
> **Lazy Injection** can be performed at will with `Zombie.infect(Endpoint.class)`, as well as with `Zombie.infect(endpoint)` (by invoking it outside of the wired class). Assume that the code snippet below is from a simple note taking application which might occasionally, or even rarely, defer to a heavy-weight mashup for it social features. ```java public class NotesService {
private TwitterService twitterService;
private FacebookService facebookService;
private GooglePlusService googlePlusService;
public void saveNote(Note note) { ... }
public void shareNote(Note note) {
...
loginWithCachedCredentials();
...
}
private void loginWithCachedCredentials() throws OAuthException {
twitterService = Zombie.infect(TwitterService.class);
facebookService = Zombie.infect(FacebookService.class);
googlePlusService = Zombie.infect(GooglePlusService.class);
...
}
...
}