- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 HK2 解决我的 Jersey/Jetty 网络服务中的服务依赖关系。我有一种情况,对于一个特定的接口(interface),我想使用一个特定的实现作为“默认”实现。 “默认”是指没有名称或限定符——如果您没有在字段或参数上指定任何注释,它就是您得到的。但是,在一些非常具体的情况下,我想提供一个将使用注释限定的替代实现。
作为我实验的结果,我实际上通过在我的绑定(bind)中使用 ranked()
限定符使它可靠地工作。似乎最高等级成为默认等级。但是,我不明白它为什么会起作用,而且我担心我正在编写的代码依赖于 HK2 的一个未记录的实现细节,当我们更新版本时这些细节可能会改变。
这是我正在做的有趣部分的人为示例。 我应该使用 ranked()
来指定服务的“默认”和注释变体吗?我应该使用另一种技术吗?
public interface IFoo {
public String getString();
}
public class DefaultImpl implements IFoo {
public String getString() {
return "Default Implementation";
}
}
public class AnnotatedImpl implements IFoo {
public String getString() {
return "Annotated Implementation";
}
}
public class Bindings extends AbstractBinder {
@Override
public void configure() {
ServiceBindingBuilder<DefaultImpl> defaultImpl =
bind(DefaultImpl.class)
.to(IFoo.class);
defaultImpl.ranked(9);
ServiceBindingBuilder<AnnotatedImpl> annotatedImpl =
bind(AnnotatedImpl.class)
.qualifiedBy(new MyAnnotationQualifier())
.to(IFoo.class);
annotatedImpl.ranked(1);
}
}
public class MyService {
@Inject
public MyService(
IFoo defaultImplementation,
@MyAnnotation
IFoo annotatedImplementation) {
// ... my code here ...
}
}
最佳答案
我在 HK2 的网站上偶然发现了一些与我所看到的行为一致的文档。
If there are more than one Widget (e.g. Widget is an interface that can have many implementations) then the best Widget will be returned from the
getService
method.Services are sorted by (in order) the service ranking, the largest locator id (so that services in children are picked before services in parents) and smallest service id (so that older services are picked prior to newer services). Therefore the best instance of a service is a service with the highest ranking or largest service locator id or the lowest service id. The ranking of a service is found in its
Descriptor
and can be changed at any time at run time. The locator id of a service is a system assigned value for theDescriptor
when it is bound into theServiceLocator
and is the id of thatServiceLocator
. The service id of a service is a system assigned value for theDescriptor
when it is bound into theServiceLocator
. The system assigned value is a monotonically increasing value. Thus if two services have the same ranking the best service will be associated with the oldestDescriptor
bound into the system.
因此,我在我的绑定(bind)上正确使用了 ranked()
。它是控制 HK2 定义为“默认”(或“最佳”)服务以注入(inject)我的依赖服务的两种方式之一。
关于java - 如何在 HK2 中定义 "default"实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35994965/
我要匹配港语的字符串我有以下香港语言的月份和年份 二零一六年六月份 ===>June 2016 二零一五年六月份 ===>June 2015 我已经使用文化信息(zh-HK)来获得月份 但是如何获
我正在构建一个多语言网络应用程序。客户要求香港网站以英文显示。不幸的是,“en-HK”在 asp.net 中不是有效的区域性,因此我尝试使用“zh-hk”来绕过它。但是,这导致日期时间字符串(格式为
下面是Shapeless的LabelledProductTypeClassCompanion中deriveHCons的签名: implicit def deriveHCons[HK <: Symbol
当我尝试创建 CultureInfo 时对于使用 Windows Phone 7 RTM SDK 的 zh-HK,我得到了 System.ArgumentException带有“值不在预期范围内。”。
我对此感到非常困惑。 这是我正在使用的。 请求 2.18.4 python 2.7.14 我正在构建一个爬虫并尝试使用 requests.get() 连接到一个 url。 这是一个从 indeed 跳
我是一名优秀的程序员,十分优秀!