Skip to content

Instantly share code, notes, and snippets.

@qrtt1
Created September 16, 2013 13:42
Show Gist options
  • Save qrtt1/6580834 to your computer and use it in GitHub Desktop.
Save qrtt1/6580834 to your computer and use it in GitHub Desktop.
沒想到,竟然有用 hk2 做 dependency injection 的一天。
package my.web.toolkits;
import javax.inject.Singleton;
import my.web.toolkits.ydl.YoutubeSignature;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
public class MyApplication extends ResourceConfig {
static Log logger = LogFactory.getLog(MyApplication.class);
public MyApplication() {
register(new MyApplicationBinder());
}
static class MyApplicationBinder extends AbstractBinder {
@Override
protected void configure() {
bindFactory(new Factory<YoutubeSignature>() {
@Override
public YoutubeSignature provide() {
YoutubeSignature signature = new YoutubeSignature(YoutubeSignature.SCRIPT_URL);
signature.addReportReceiver("[email protected]");
return signature;
}
@Override
public void dispose(YoutubeSignature instance) {
}
}).to(YoutubeSignature.class).in(Singleton.class);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment