- JBoss Weld CDI for Java Platform
- Ken Finnegan
- 217字
- 2021-08-13 16:49:56
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 orAnnotatedParameter
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.
- Boost程序庫完全開發指南:深入C++”準”標準庫(第5版)
- Mobile Application Development:JavaScript Frameworks
- JavaScript 從入門到項目實踐(超值版)
- Android 9 Development Cookbook(Third Edition)
- 21天學通C++(第6版)
- Java深入解析:透析Java本質的36個話題
- C#應用程序設計教程
- Java面向對象程序設計
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- 零基礎學C語言(第4版)
- 會當凌絕頂:Java開發修行實錄
- AutoCAD基礎教程
- Learning Apache Thrift
- JSP大學實用教程
- Swift從入門到精通 (移動開發叢書)