Google-Guice:
Is a java framework to provide dependency injection (DI).
Basic advantages:
My example:
public class AllInOne {
public interface DemoInt {
public String test();
}
public static class DemoImp implements DemoInt {
public String test() {
return "OK";
}
}
public static class Demo {
@Inject
private DemoInt injectedClass;
public String makeTest() {
return injectedClass != null ? injectedClass.test() : "ERROR";
}
}
public class DemoModule extends AbstractModule {
@Override
protected void configure() {
bind(DemoInt.class).to(DemoImp.class);
}
}
public AllInOne() {
Injector injector = Guice.createInjector(new DemoModule());
Demo demo = injector.getInstance(Demo.class);
System.out.println(demo.makeTest());
}
public static void main(String a[]) {
new AllInOne();
}
}
No hay comentarios:
Publicar un comentario