????Module???????
????Guice????????????????????AbstractModule?????configure????????configure???????????????Binder??????????
????Binder????????γ????????????DSL???磺
?????????????binder.bind(serviceClass).to(implClass).in(Scopes.[SINGLETON | NO_SCOPE]);
??????base??????????binder.bind(implClass).in(Scopes.[SINGLETON | NO_SCOPE]);
????service????????binder.bind(serviceClass).toInstance(servieInstance).in(Scopes.[SINGLETON | NO_SCOPE]);
?????????????????binder.bind(serviceClass).annotatedWith(Names.named(“name”)).to(implClass).in(Scopes.[SINGLETON | NO_SCOPE]);
????????????????@Provides??????????????spring??@Bean??
????@ImplementedBy??????????????????@ImplementedBy?????????????????е?OO????????????????????
????????????????????????????????@Inject???????????????????????????????@Named??????磺
public void configure() {
final Binder binder = binder();
//TODO: bind named instance;
binder.bind(NamedService.class).annotatedWith(Names.named("impl1")).to(NamedServiceImpl1.class);
binder.bind(NamedService.class).annotatedWith(Names.named("impl2")).to(NamedServiceImpl2.class);
}
@Inject
public List<NamedService> getAllItemServices(@Named("impl1") NamedService nameService1??
@Named("impl2") NamedService nameService2) {
}
Guice?????????@Provides????????????????????
@Provides
public List<NamedService> getAllItemServices(@Named("impl1") NamedService nameService1??
@Named("impl2") NamedService nameService2) {
final ArrayList<NamedService> list = new ArrayList<NamedService>();
list.add(nameService1);
list.add(nameService2);
return list;
}
????Guice???
?????????????Guice module???????????????????????????÷???????????μ?github .
package com.github.greengerong.app;
/**
* ***************************************
* *
* Auth: green gerong                     *
* Date: 2014                             *
* blog: http://greengerong.github.io/    *
* github: https://github.com/greengerong *
* *
* ****************************************
*/
public class AppModule extends AbstractModule {
private static final Logger LOGGER = LoggerFactory.getLogger(AppModule.class);
private final BundleContext bundleContext;
public AppModule(BundleContext bundleContext) {
this.bundleContext = bundleContext;
LOGGER.info(String.format("enter app module with: %s"?? bundleContext));
}
@Override
public void configure() {
final Binder binder = binder();
//TODO: bind interface
binder.bind(ItemService.class).to(ItemServiceImpl.class).in(SINGLETON);
binder.bind(OrderService.class).to(OrderServiceImpl.class).in(SINGLETON);
//TODO: bind self class(without interface or base class)
binder.bind(PriceService.class).in(Scopes.SINGLETON);
//TODO: bind instance not class.
binder.bind(RuntimeService.class).toInstance(new RuntimeService());
//TODO: bind named instance;
binder.bind(NamedService.class).annotatedWith(Names.named("impl1")).to(NamedServiceImpl1.class);
binder.bind(NamedService.class).annotatedWith(Names.named("impl2")).to(NamedServiceImpl2.class);
}
@Provides
public List<NamedService> getAllItemServices(@Named("impl1") NamedService nameService1??
@Named("impl2") NamedService nameService2) {
final ArrayList<NamedService> list = new ArrayList<NamedService>();
list.add(nameService1);
list.add(nameService2);
return list;
}
}
????Guice?????
????????Guice????????????????????Guice module?????Guice??????injector???磺
????Injector injector = Guice.createInjector(new AppModule(bundleContext));
????????????????module?????????????module??????????????
????Guice api??????
????public static Injector createInjector(Module... modules)
????public static Injector createInjector(Iterable<? extends Module> modules)
????public static Injector createInjector(Stage stage?? Module... modules)
????public static Injector createInjector(Stage stage?? Iterable<? extends Module> modules)
????Guice????????Region??????????State?????state??? TOOL??DEVELOPMENT??PRODUCTION???;????DEVELOPMENT??????
????????
????????Guice?????demo??????μ?github .
????Guice???к????????AOP??????????????????set??map??OSGI??UOW?????????μ?Guice wiki.