java - Guice does not load Bindings -
interface -
public interface add { public int add(int a, int b) } implementation 1 -
class martianadd implements add public int add(int a, int b) { return -a+b; } } implementation 2-
public class simpleadd implements add { public simpleadd() {} public int add(int a, int b) { return + b } } class bindings extends abstractmodule { protected void configure() { bind(add).annotatedwith(names.named('earth')).to(simpleadd) bind(add).annotatedwith(names.named('mars')).to(martianadd) } } main class -
class test { public static void main(string[] args) { injector injector = guice.createinjector(new bindings()) @named('mars') add = injector.getinstance(add) print a.class print a.add(5, 8) } } exception -
exception in thread "main" com.google.inject.configurationexception: guice configuration errors: 1) no implementation in.ksharma.add bound. while locating in.ksharma.add why doesn't guice load bindings?
this problem, believe:
@named('mars') add = injector.getinstance(add) you're asking instance of add - fact variable you're assigning result has @named annotation irrelevant guice. believe you'll need use injector.getinstance(key) key right class with right name binding, e.g.
add add = injector.getinstance(key.get(add, names.named('mars')))
Comments
Post a Comment