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

Injection point metadata

There is lots of useful metadata information that is present on an injection point, which is represented in the javax.enterprise.inject.spi.InjectionPoint interface. Weld provides an implementation of InjectionPoint with @Dependent scope and @Default qualifier for us to retrieve the metadata.

The injection point provides the following functions:

  • getBean(): This returns the Bean object of the bean defined on the injection point
  • getType(): This returns the bean type of the injection point
  • getQualifiers(): This returns all the qualifiers of the injection point
  • getMember(): This returns a different instance depending on whether the injection point utilizes field injection (Field), method parameter injection (Method), or constructor parameter injection (Constructor)
  • getAnnotated(): This returns AnnotatedField for field injection or AnnotatedParameter for method and constructor parameter injection

When we have an @Dependent scoped bean, there are occasions when it needs to retrieve metadata about the injection point to be properly constructed. A typical example of this requirement is with a logger:

class LoggerFactory {
  @Produces
  Logger createLogger(InjectionPoint point) {
    return Logger.getLogger(point.getMember().getDeclaringClass().getName());
  }
}

Injecting a logger with the name set to that of the class is then:

@Inject
Logger log;

Writing that simple producer has saved us time in not needing to specifically retrieve the class name to set it on a logger in every class we want to use it.

主站蜘蛛池模板: 五指山市| 建宁县| 麻江县| 隆德县| 江达县| 榆树市| 威海市| 宜兰县| 洪泽县| 宁明县| 岳池县| 平果县| 墨玉县| 宁城县| 砀山县| 郯城县| 济源市| 蕉岭县| 西峡县| 漠河县| 万荣县| 海原县| 高陵县| 石首市| 乌兰浩特市| 石狮市| 屏东市| 华坪县| 陇南市| 黑水县| 永新县| 壶关县| 綦江县| 漳浦县| 武定县| 砀山县| 雷波县| 子洲县| 惠州市| 龙南县| 城固县|