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

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.

主站蜘蛛池模板: 紫阳县| 峨边| 邢台县| 鹤山市| 长汀县| 贵州省| 朝阳区| 宜兰市| 萨嘎县| 高邑县| 永寿县| 专栏| 多伦县| 肇东市| 红河县| 栖霞市| 云南省| 博湖县| 宿州市| 平南县| 光泽县| 林甸县| 西华县| 屏东县| 宣化县| 甘肃省| 视频| 河间市| 沙雅县| 汉沽区| 崇义县| 封丘县| 安顺市| 南昌市| 岑巩县| 晋城| 腾冲县| 北票市| 响水县| 绥滨县| 瑞昌市|