官术网_书友最值得收藏!

Dynamic lookups

Another great feature of the CDI is to be able to control a lazy instantiation or resolution of a bean. This is done with the Provider<?> and Instance<?> APIs. Instance is a Provider allowing you to resolve a bean at runtime. Provider is an instance wrapper allowing you to decide when to instantiate the underlying instance.

Take a look at the following code snippet:

@ApplicationScoped
public class DynamicInstance {
@Inject
private Provider<MyService> myServiceProvider;

@Inject
private Instance<MyService> myServices;

public MyService currentService() {
return myServiceProvider.get(); <1>
}

public MyService newService(final Annotation qualifier) {
return myServices.select(qualifier).get(); <2>
}
}

Let's look at the underlying mechanism of the preceding code snippet:

  • Calling Provider.get() will trigger the creation of an underlying instance (MyService here). It delays the instantiation of the injection or makes the instantiation conditional. Note that it depends on the scope of the bean and that a normal scoped bean won't benefit much from this use.
  • Calling Instance.select(...) will make the bean definition more specific based on the injection point. In this case, we start from a bean type (MyService) with the implicit @Default qualifier and replace the implicit qualifier with the one passed as the parameter. Then, we resolve the bean and get its instance. This is useful for switching the implementation dynamically and conditionally.

Since an Instance is a Provider, the implementations share the same code for both. This means their performances will be the same.

Now the question is, what is the cost of using a programmatic lookup versus a plain injection? Is it more expensive or not? In terms of implementation, the code is quite comparable, it has to resolve the bean to instantiate and then instantiate it so that we are very close to an injection. We will ignore the small differences that do not impact the performance much. One issue here is its use: if you get a Provider injected and resolve it for each use, you will then increase a lot of the time spent on resolving and instantiating versus just using an already resolved and created instance.

主站蜘蛛池模板: 通榆县| 宁河县| 泸溪县| 丹凤县| 凤翔县| 文成县| 花莲市| 蓝山县| 额敏县| 宁德市| 夹江县| 孟连| 日喀则市| 铜陵市| 郴州市| 东方市| 新和县| 乐亭县| 龙井市| 固始县| 登封市| 临漳县| 黑河市| 怀安县| 新巴尔虎右旗| 金沙县| 伊吾县| 城步| 汉阴县| 朝阳市| 新河县| 富顺县| 平遥县| 龙川县| 昌邑市| 普兰店市| 浦县| 泸定县| 舞阳县| 咸丰县| 慈溪市|