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

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.

主站蜘蛛池模板: 长兴县| 壶关县| 香港 | 江阴市| 曲水县| 肃北| 峨边| 泸州市| 凯里市| 孟津县| 无锡市| 威信县| 汉川市| 保亭| 辰溪县| 麻阳| 崇阳县| 若尔盖县| 井冈山市| 太白县| 保德县| 辰溪县| 大邑县| 马龙县| 屯留县| 长垣县| 永靖县| 兴国县| 宁陵县| 广饶县| 盐亭县| 肃南| 清丰县| 新巴尔虎左旗| 宝应县| 射阳县| 贺兰县| 太白县| 贵港市| 磐安县| 汾阳市|