- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的一个类中有一个注入(inject)点
@Inject
private UsbServices usbServices;
还有一个带有 @Produces
的 Weld 类方法
public class AppProducer
{
@Produces
@Singleton
public UsbServices getUsbServices()
{
UsbServices usbServices = null;
try
{
usbServices = UsbHostManager.getUsbServices();
} catch (SecurityException | UsbException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return usbServices;
}
}
我希望在集成测试期间交换替代实现,所以我有上述类的替代版本
@Alternative
public class TestAppProducer
{
@Produces
@Singleton
public UsbServices getUsbServices()
{
return new UsbServicesSimulator();
}
}
还有下面我的测试包resources/META-INF里面的beans.xml
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<alternatives>
<class>xxx.TestAppProducer</class>
</alternatives>
</beans>
为了对此进行测试,我尝试使用它的主类启动 Weld 并手动连接一个 USB“设备”。测试方法有以下
Weld weld = new Weld();
WeldContainer container = weld.initialize();
PCSyncApplication app = container.instance().select(PCSyncApplication.class).get();
ServicesManager servMan = app.getServicesManager();
UsbServicesSimulator servSimulator = (UsbServicesSimulator) servMan.getUsbServices();
servSimualtor.attachDevice(new FakeDevice());
但是我得到了一个不明确的依赖异常
Exception in thread "main" org.jboss.weld.exceptions.DeploymentException: Exception List with 2 exceptions:
Exception 0 :
org.jboss.weld.exceptions.DeploymentException: WELD-001409: Ambiguous dependencies for type UsbServices with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private xxx.manager.ServicesManager.usbServices
at xxx.manager.ServicesManager.usbServices(ServicesManager.java:0)
Possible dependencies:
- Managed Bean [class xxx.simulator.usb.UsbServicesSimulator] with qualifiers [@Any @Default],
- Producer Method [UsbServices] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces @Singleton public xxx.AppProducer.getUsbServices()]
编辑:如果我对 UsbServicesSimulator
进行注释,则进行小更新随着以下消息消失
@Alternative
public class UsbServicesSimulator implements UsbServices
但我不确定我是否理解其中的原因。我会想到 TestAppProducer
和 <alternatives>
节beans.xml
够了吗?
最佳答案
使用@Alternative 注释,您只能禁用测试服务,而不能解决不明确的依赖关系。如果您想启用替代方案,则主版本不应在类路径中可见或应具有较低的优先级,例如@Priority 用 beans.xml 或 @Vetoed 注释或禁用。还有一些其他的技巧,
您可以在这里找到完整的文档 https://docs.jboss.org/weld/reference/latest/en-US/html/injection.html#alternatives
我更喜欢使用单独的项目,或者使用 Arquillian 运行测试并在类路径中只包含我需要的那个类。在这种情况下,我不建议使用优先级。因为您需要为生产版本禁用或设置较低的优先级。
关于java - 使用@Alternative Weld 不明确的依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33301172/
我在 linux 上工作。我对windows没有太多想法。 windows中文件的权限是如何组织的?我们在unix中是否有像chmod这样的api来更改权限? 最佳答案 对于 Windows,有一个名
应用程序编程接口(interface) (API) 是一组用于访问基于 Web 的软件应用程序的编程指令和标准。 如果出现 ,有人可以向我解释一下吗?谷歌地图 或 优酷 这是API哪个是softwar
我有两个应用程序,A 和 B,它们使用 android 库 C。B 有一个服务 A 想通过 C 使用,例如 在我的库中有一个类试图将它绑定(bind)到服务,
我正在正常或安全模式下启动相机应用程序,具体取决于使用我的应用程序执行的手势,但一旦用户选择应用程序并点击始终,则没有选项可以更改默认值,即使是从 Android 的设置菜单中也是如此. camera
我有一个数据集,本质上是一个稀疏二进制矩阵,表示两个集合的元素之间的关系。例如,让第一组是人(用他们的名字表示),例如像这样的东西: people = set(['john','jane','mike
何为pythonic? pythonic如果翻译成中文的话就是很python。很+名词结构的用法在中国不少,比如:很娘,很国足,很CCTV等等。 我的理解为,很+名词表达了一种特殊和强调的意味。
某些 Prolog 目标的确定性成功问题已经一次又一次地出现在 - 至少 - 以下问题: Reification of term equality/inequality Intersection an
我指的是 DateTime.TryParse(string s, out DateTime result) 重载,它尝试从字符串中解析 DateTime - 没有特定的格式正在指定。 我可以从http
2020 年 04 月 10 日,《中共中央国务院关于构建更加完善的要素市场化配置体制机制的意见》正式公布,将数据确立为五大生产要素(土地、资本、劳动力以及技术)之
有人可以解释一下 NSNotification 的 addObserver 函数中 notificationSender 的用途吗? 这是 Apple 文档的解释: notificationSende
我是一名优秀的程序员,十分优秀!