具有多种混凝土的 Guice ......选择其中之一
Guice with multiple concretes......picking one of them
我正在注入同一接口的多个具体。
我了解了指南 "code it up" 约定。
我的代码目前吐出来
[INFO] App - About to ship. (abc)
[INFO] App - ShipperInterface . (FedExShipper)
[INFO] App - ShipperInterface . (UpsShipper)
[INFO] App - ShipperInterface . (UspsShipper)
所以我有多个 "shippers" 触手可及。
注意方法:
public void ProcessOrder(String preferredShipperAbbreviation, Order
ord) {
我正在尝试找出使用 (String) preferredShipperAbbreviation 来选择 3 个具体托运人中的 1 个的最佳方法。
当我在 Guice 注册我的 3 个混凝土时,有没有办法 "name"?
或者从三个中选择一个的最佳方法是什么?
public class ProductionInjectModule extends AbstractModule implements Module {
@Override
protected void configure() {
try {
bind(OrderProcessorInterface.class).toConstructor(OrderProcessorImpl.class.getConstructor(Set.class));
Multibinder<ShipperInterface> multibinder = Multibinder.newSetBinder(binder(), ShipperInterface.class);
multibinder.addBinding().toConstructor(FedExShipper.class.getConstructor(org.apache.commons.logging.Log.class));
multibinder.addBinding().toConstructor(UpsShipper.class.getConstructor(org.apache.commons.logging.Log.class));
multibinder.addBinding().toConstructor(UspsShipper.class.getConstructor(org.apache.commons.logging.Log.class));
} catch (NoSuchMethodException e) {
addError(e);
}
}
}
=============
import java.util.Collection;
import java.util.Set;
import org.apache.commons.logging.Log;
public class OrderProcessorImpl implements OrderProcessorInterface {
private Log logger;
Set<ShipperInterface> shippers;
public OrderProcessorImpl(Log lgr, Set<ShipperInterface> shprs) {
if (null == lgr) {
throw new IllegalArgumentException("Log is null");
}
if (null == shprs) {
throw new IllegalArgumentException("ShipperInterface(s) is null");
}
this.logger = lgr;
this.shippers = shprs;
}
public void ProcessOrder(String preferredShipperAbbreviation, Order ord) {
this.logger.info(String.format("About to ship. (%1s)", preferredShipperAbbreviation));
for (ShipperInterface sh : shippers) {
this.logger.info(String.format("ShipperInterface . (%1s)", sh.getClass().getSimpleName()));
}
}
}
=============
public interface OrderProcessorInterface {
void ProcessOrder(String preferredShipperAbbreviation, Order ord);
}
public class FedExShipper implements ShipperInterface {
private Log logger;
public FedExShipper(Log lgr) {
if (null == lgr) {
throw new IllegalArgumentException("Log is null");
}
this.logger = lgr;
}
public void ShipOrder(Order ord) {
this.logger.info("I'm shipping the Order with FexEx");
}
}
public class UpsShipper implements ShipperInterface {
private Log logger;
public UpsShipper(Log lgr) {
if (null == lgr) {
throw new IllegalArgumentException("Log is null");
}
this.logger = lgr;
}
public void ShipOrder(Order ord) {
this.logger.info("I'm shipping the Order with Ups");
}
}
public class UspsShipper implements ShipperInterface {
private Log logger;
public UspsShipper(Log lgr) {
if (null == lgr) {
throw new IllegalArgumentException("Log is null");
}
this.logger = lgr;
}
public void ShipOrder(Order ord) {
this.logger.info("I'm shipping the Order with Usps");
}
}
.......
"Main"方法:
ProductionInjectModule pm = new ProductionInjectModule();
Injector injector = Guice.createInjector(pm);
Order ord = new Order();
OrderProcessorInterface opi = injector.getInstance(OrderProcessorInterface.class);
opi.ProcessOrder("WhatDoIPutHere?", ord);
===========
以下 Guice 版本:
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.0</version>
</dependency>
==================================
我正在尝试这种方式。这和任何方式一样好吗?
最终,在我的 "real" 场景中(不是这个编造的场景)......我想将 "concreteKey" 保留为 database/configuration 设置。
Order ord = new Order();
OrderProcessorInterface opi = injector.getInstance(OrderProcessorInterface.class);
opi.ProcessOrder(FedExShipper.class.getSimpleName(), ord);
public void ProcessOrder(String preferredShipperAbbreviation, Order ord) {
this.logger.info(String.format("About to ship. (%1s)", preferredShipperAbbreviation));
ShipperInterface foundShipperInterface = this.FindShipperInterface(preferredShipperAbbreviation);
foundShipperInterface.ShipOrder(ord);
}
private ShipperInterface FindShipperInterface(String preferredShipperAbbreviation) {
/* requires java 8 */
ShipperInterface foundShipperInterface = this.shippers
.stream().filter(x -> x.getClass().getSimpleName().equalsIgnoreCase(preferredShipperAbbreviation)).findFirst().orElse(null);
if(null == foundShipperInterface)
{
throw new NullPointerException(String.format("ShipperInterface not found in ShipperInterface collection. ('%1s')", preferredShipperAbbreviation));
}
return foundShipperInterface;
}
============= 追加 ==================
感谢 Jeff B 的 answer/comments。
import java.util.Map;
import java.util.Set;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.multibindings.MapBinder;
import com.google.inject.multibindings.Multibinder;
public class ProductionInjectModule extends AbstractModule implements Module {
@Override
protected void configure() {
try {
MapBinder<String, ShipperInterface> mappyBinder = MapBinder.newMapBinder(binder(), String.class, ShipperInterface.class);
mappyBinder.addBinding("myFedExName").toConstructor(FedExShipper.class.getConstructor(org.apache.commons.logging.Log.class));
mappyBinder.addBinding("myUPSName").toConstructor(UpsShipper.class.getConstructor(org.apache.commons.logging.Log.class));
mappyBinder.addBinding("myUSPSName").toConstructor(UspsShipper.class.getConstructor(org.apache.commons.logging.Log.class));
/* below is not needed, but shows what needs to be injected */
java.util.Map<String, javax.inject.Provider<ShipperInterface>> shipperProviderMap;
} catch (NoSuchMethodException e) {
addError(e);
}
}
}
================
import java.util.Collection;
import java.util.Set;
import org.apache.commons.logging.Log;
public class OrderProcessorImpl implements OrderProcessorInterface {
private Log logger;
private java.util.Map<String, javax.inject.Provider<ShipperInterface>> shipperProviderMap;
public OrderProcessorImpl(Log lgr, java.util.Map<String, javax.inject.Provider<ShipperInterface>> spMap) {
if (null == lgr) {
throw new IllegalArgumentException("Log is null");
}
if (null == spMap) {
throw new IllegalArgumentException("Provider<ShipperInterface> is null");
}
this.logger = lgr;
this.shipperProviderMap = spMap;
}
public void ProcessOrder(String preferredShipperAbbreviation, Order ord) {
this.logger.info(String.format("About to ship. (%1s)", preferredShipperAbbreviation));
ShipperInterface foundShipperInterface = this.FindShipperInterface(preferredShipperAbbreviation);
foundShipperInterface.ShipOrder(ord);
}
private ShipperInterface FindShipperInterface(String preferredShipperAbbreviation) {
ShipperInterface foundShipperInterface = this.shipperProviderMap.get(preferredShipperAbbreviation).get();
if (null == foundShipperInterface) {
throw new NullPointerException(
String.format("ShipperInterface not found in shipperProviderMap. ('%1s')", preferredShipperAbbreviation));
}
return foundShipperInterface;
}
}
================
"main"方法
ProductionInjectModule pm = new ProductionInjectModule();
Injector injector = Guice.createInjector(pm);
Order ord = new Order();
OrderProcessorInterface opi = injector.getInstance(OrderProcessorInterface.class);
opi.ProcessOrder("myFedExName", ord); /* now use the "friendly named" strings */
输出:
[INFO] App - About to ship. (myFedExName)
[INFO] App - I'm shipping the Order with FexEx
我可能在我新发布的代码中有一些额外的 "logger" 注入.....但是简单的清理就可以了 运行.
如果您将 Multibinder 用于 map 绑定,那么您可以使用 MapBinder:
将每个 Shipper 实例绑定到一个 Map 中
MapBinder<String, ShipperInterface> multibinder = MapBinder.newMapBinder(
binder(), String.class, ShipperInterface.class);
multibinder.addBinding("FedEx").to(FedExShipper.class);
multibinder.addBinding("UPS").to(UpsShipper.class);
multibinder.addBinding("USPS").to(UspsShipper.class);
然后在你注入的class中你可以注入一个Map<String, Provider<ShipperInterface>>
:
private ShipperInterface FindShipperInterface(String
preferredShipperAbbreviation) {
ShipperInterface foundShipperInterface =
providerMap.get(preferredShipperAbbreviation).get();
}
您也可以直接注入 Map<String, ShipperInterface>
,但 Multibinder 会免费处理 Provider 间接寻址,这样您就可以避免创建三个 ShipperInterface 实例,而实际上只需要一个。此外,如果您的实例选择代码比简单地从您在编译时知道的一组实现中选择一个字符串更复杂,you might still want a Factory implementation you write.
作为旁注,ideally use @Inject
annotations and bind(...).to(...)
instead of toConstructor
。这不会将您与 Guice 联系起来,因为 @Inject
是在 JSR-330 中定义的,并且您正在添加可以选择以后不使用的注释。你也可以在你的 AbstractModule 中写一个 @Provides
方法,就像这样,它并不比你的 toConstructor
绑定更脆弱:
@Provides UspsShipper provideUspsShipper(Log log) {
return new UspsShipper(log);
}
当且仅当您使用遗留代码、您无法控制的代码、非常严格的代码样式规则或 AOP(这里可能就是这种情况)时,才使用 toConstructor
。为了一个简洁的例子,我在上面这样做了,但如果需要,你可以恢复到 toConstructor
。
我正在注入同一接口的多个具体。
我了解了指南 "code it up" 约定。
我的代码目前吐出来
[INFO] App - About to ship. (abc)
[INFO] App - ShipperInterface . (FedExShipper)
[INFO] App - ShipperInterface . (UpsShipper)
[INFO] App - ShipperInterface . (UspsShipper)
所以我有多个 "shippers" 触手可及。
注意方法:
public void ProcessOrder(String preferredShipperAbbreviation, Order ord) {
我正在尝试找出使用 (String) preferredShipperAbbreviation 来选择 3 个具体托运人中的 1 个的最佳方法。
当我在 Guice 注册我的 3 个混凝土时,有没有办法 "name"?
或者从三个中选择一个的最佳方法是什么?
public class ProductionInjectModule extends AbstractModule implements Module {
@Override
protected void configure() {
try {
bind(OrderProcessorInterface.class).toConstructor(OrderProcessorImpl.class.getConstructor(Set.class));
Multibinder<ShipperInterface> multibinder = Multibinder.newSetBinder(binder(), ShipperInterface.class);
multibinder.addBinding().toConstructor(FedExShipper.class.getConstructor(org.apache.commons.logging.Log.class));
multibinder.addBinding().toConstructor(UpsShipper.class.getConstructor(org.apache.commons.logging.Log.class));
multibinder.addBinding().toConstructor(UspsShipper.class.getConstructor(org.apache.commons.logging.Log.class));
} catch (NoSuchMethodException e) {
addError(e);
}
}
}
=============
import java.util.Collection;
import java.util.Set;
import org.apache.commons.logging.Log;
public class OrderProcessorImpl implements OrderProcessorInterface {
private Log logger;
Set<ShipperInterface> shippers;
public OrderProcessorImpl(Log lgr, Set<ShipperInterface> shprs) {
if (null == lgr) {
throw new IllegalArgumentException("Log is null");
}
if (null == shprs) {
throw new IllegalArgumentException("ShipperInterface(s) is null");
}
this.logger = lgr;
this.shippers = shprs;
}
public void ProcessOrder(String preferredShipperAbbreviation, Order ord) {
this.logger.info(String.format("About to ship. (%1s)", preferredShipperAbbreviation));
for (ShipperInterface sh : shippers) {
this.logger.info(String.format("ShipperInterface . (%1s)", sh.getClass().getSimpleName()));
}
}
}
=============
public interface OrderProcessorInterface {
void ProcessOrder(String preferredShipperAbbreviation, Order ord);
}
public class FedExShipper implements ShipperInterface {
private Log logger;
public FedExShipper(Log lgr) {
if (null == lgr) {
throw new IllegalArgumentException("Log is null");
}
this.logger = lgr;
}
public void ShipOrder(Order ord) {
this.logger.info("I'm shipping the Order with FexEx");
}
}
public class UpsShipper implements ShipperInterface {
private Log logger;
public UpsShipper(Log lgr) {
if (null == lgr) {
throw new IllegalArgumentException("Log is null");
}
this.logger = lgr;
}
public void ShipOrder(Order ord) {
this.logger.info("I'm shipping the Order with Ups");
}
}
public class UspsShipper implements ShipperInterface {
private Log logger;
public UspsShipper(Log lgr) {
if (null == lgr) {
throw new IllegalArgumentException("Log is null");
}
this.logger = lgr;
}
public void ShipOrder(Order ord) {
this.logger.info("I'm shipping the Order with Usps");
}
}
.......
"Main"方法:
ProductionInjectModule pm = new ProductionInjectModule();
Injector injector = Guice.createInjector(pm);
Order ord = new Order();
OrderProcessorInterface opi = injector.getInstance(OrderProcessorInterface.class);
opi.ProcessOrder("WhatDoIPutHere?", ord);
=========== 以下 Guice 版本:
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.0</version>
</dependency>
==================================
我正在尝试这种方式。这和任何方式一样好吗?
最终,在我的 "real" 场景中(不是这个编造的场景)......我想将 "concreteKey" 保留为 database/configuration 设置。
Order ord = new Order();
OrderProcessorInterface opi = injector.getInstance(OrderProcessorInterface.class);
opi.ProcessOrder(FedExShipper.class.getSimpleName(), ord);
public void ProcessOrder(String preferredShipperAbbreviation, Order ord) {
this.logger.info(String.format("About to ship. (%1s)", preferredShipperAbbreviation));
ShipperInterface foundShipperInterface = this.FindShipperInterface(preferredShipperAbbreviation);
foundShipperInterface.ShipOrder(ord);
}
private ShipperInterface FindShipperInterface(String preferredShipperAbbreviation) {
/* requires java 8 */
ShipperInterface foundShipperInterface = this.shippers
.stream().filter(x -> x.getClass().getSimpleName().equalsIgnoreCase(preferredShipperAbbreviation)).findFirst().orElse(null);
if(null == foundShipperInterface)
{
throw new NullPointerException(String.format("ShipperInterface not found in ShipperInterface collection. ('%1s')", preferredShipperAbbreviation));
}
return foundShipperInterface;
}
============= 追加 ==================
感谢 Jeff B 的 answer/comments。
import java.util.Map;
import java.util.Set;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.multibindings.MapBinder;
import com.google.inject.multibindings.Multibinder;
public class ProductionInjectModule extends AbstractModule implements Module {
@Override
protected void configure() {
try {
MapBinder<String, ShipperInterface> mappyBinder = MapBinder.newMapBinder(binder(), String.class, ShipperInterface.class);
mappyBinder.addBinding("myFedExName").toConstructor(FedExShipper.class.getConstructor(org.apache.commons.logging.Log.class));
mappyBinder.addBinding("myUPSName").toConstructor(UpsShipper.class.getConstructor(org.apache.commons.logging.Log.class));
mappyBinder.addBinding("myUSPSName").toConstructor(UspsShipper.class.getConstructor(org.apache.commons.logging.Log.class));
/* below is not needed, but shows what needs to be injected */
java.util.Map<String, javax.inject.Provider<ShipperInterface>> shipperProviderMap;
} catch (NoSuchMethodException e) {
addError(e);
}
}
}
================
import java.util.Collection;
import java.util.Set;
import org.apache.commons.logging.Log;
public class OrderProcessorImpl implements OrderProcessorInterface {
private Log logger;
private java.util.Map<String, javax.inject.Provider<ShipperInterface>> shipperProviderMap;
public OrderProcessorImpl(Log lgr, java.util.Map<String, javax.inject.Provider<ShipperInterface>> spMap) {
if (null == lgr) {
throw new IllegalArgumentException("Log is null");
}
if (null == spMap) {
throw new IllegalArgumentException("Provider<ShipperInterface> is null");
}
this.logger = lgr;
this.shipperProviderMap = spMap;
}
public void ProcessOrder(String preferredShipperAbbreviation, Order ord) {
this.logger.info(String.format("About to ship. (%1s)", preferredShipperAbbreviation));
ShipperInterface foundShipperInterface = this.FindShipperInterface(preferredShipperAbbreviation);
foundShipperInterface.ShipOrder(ord);
}
private ShipperInterface FindShipperInterface(String preferredShipperAbbreviation) {
ShipperInterface foundShipperInterface = this.shipperProviderMap.get(preferredShipperAbbreviation).get();
if (null == foundShipperInterface) {
throw new NullPointerException(
String.format("ShipperInterface not found in shipperProviderMap. ('%1s')", preferredShipperAbbreviation));
}
return foundShipperInterface;
}
}
================
"main"方法
ProductionInjectModule pm = new ProductionInjectModule();
Injector injector = Guice.createInjector(pm);
Order ord = new Order();
OrderProcessorInterface opi = injector.getInstance(OrderProcessorInterface.class);
opi.ProcessOrder("myFedExName", ord); /* now use the "friendly named" strings */
输出:
[INFO] App - About to ship. (myFedExName)
[INFO] App - I'm shipping the Order with FexEx
我可能在我新发布的代码中有一些额外的 "logger" 注入.....但是简单的清理就可以了 运行.
如果您将 Multibinder 用于 map 绑定,那么您可以使用 MapBinder:
将每个 Shipper 实例绑定到一个 Map 中MapBinder<String, ShipperInterface> multibinder = MapBinder.newMapBinder(
binder(), String.class, ShipperInterface.class);
multibinder.addBinding("FedEx").to(FedExShipper.class);
multibinder.addBinding("UPS").to(UpsShipper.class);
multibinder.addBinding("USPS").to(UspsShipper.class);
然后在你注入的class中你可以注入一个Map<String, Provider<ShipperInterface>>
:
private ShipperInterface FindShipperInterface(String
preferredShipperAbbreviation) {
ShipperInterface foundShipperInterface =
providerMap.get(preferredShipperAbbreviation).get();
}
您也可以直接注入 Map<String, ShipperInterface>
,但 Multibinder 会免费处理 Provider 间接寻址,这样您就可以避免创建三个 ShipperInterface 实例,而实际上只需要一个。此外,如果您的实例选择代码比简单地从您在编译时知道的一组实现中选择一个字符串更复杂,you might still want a Factory implementation you write.
作为旁注,ideally use @Inject
annotations and bind(...).to(...)
instead of toConstructor
。这不会将您与 Guice 联系起来,因为 @Inject
是在 JSR-330 中定义的,并且您正在添加可以选择以后不使用的注释。你也可以在你的 AbstractModule 中写一个 @Provides
方法,就像这样,它并不比你的 toConstructor
绑定更脆弱:
@Provides UspsShipper provideUspsShipper(Log log) {
return new UspsShipper(log);
}
当且仅当您使用遗留代码、您无法控制的代码、非常严格的代码样式规则或 AOP(这里可能就是这种情况)时,才使用 toConstructor
。为了一个简洁的例子,我在上面这样做了,但如果需要,你可以恢复到 toConstructor
。