- 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.
- 嵌入式軟件系統(tǒng)測(cè)試:基于形式化方法的自動(dòng)化測(cè)試解決方案
- Reporting with Visual Studio and Crystal Reports
- Microsoft Dynamics 365 Extensions Cookbook
- 動(dòng)手玩轉(zhuǎn)Scratch3.0編程:人工智能科創(chuàng)教育指南
- 算法訓(xùn)練營(yíng):入門篇(全彩版)
- PHP+MySQL網(wǎng)站開發(fā)技術(shù)項(xiàng)目式教程(第2版)
- 零基礎(chǔ)學(xué)C語(yǔ)言(第4版)
- Django Design Patterns and Best Practices
- C語(yǔ)言從入門到精通
- Qt 5.12實(shí)戰(zhàn)
- Java程序設(shè)計(jì)教程
- 劍指大數(shù)據(jù):企業(yè)級(jí)電商數(shù)據(jù)倉(cāng)庫(kù)項(xiàng)目實(shí)戰(zhàn)(精華版)
- Learning IBM Bluemix
- SQL Server 2008數(shù)據(jù)庫(kù)應(yīng)用技術(shù)(第2版)
- Jenkins 2.x Continuous Integration Cookbook(Third Edition)