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

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.

主站蜘蛛池模板: 江陵县| 沧州市| 高陵县| 萨迦县| 张家港市| 云南省| 同仁县| 紫阳县| 德清县| 芜湖县| 堆龙德庆县| 黄龙县| 郓城县| 罗田县| 瓦房店市| 浦北县| 闵行区| 英吉沙县| 萨迦县| 东乡| 义乌市| 汤原县| 阿克苏市| 丘北县| 治县。| 绍兴县| 买车| 无极县| 通化市| 英吉沙县| 鹤庆县| 高邑县| 达孜县| 娱乐| 阳泉市| 万源市| 治多县| 财经| 邵阳市| 曲靖市| 家居|