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

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.

主站蜘蛛池模板: 盐边县| 潮安县| 右玉县| 会宁县| 丹阳市| 绥棱县| 湘西| 浦北县| 武宣县| 彩票| 汉阴县| 定安县| 郎溪县| 营山县| 象州县| 巴青县| 南澳县| 花莲市| 资源县| 裕民县| 永康市| 巴东县| 瓦房店市| 丰顺县| 西畴县| 明光市| 铁力市| 南宁市| 滦南县| 阳山县| 湟源县| 买车| 沭阳县| 建昌县| 济阳县| 怀化市| 宁远县| 兴山县| 电白县| 景东| 五寨县|