- 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.
- Python快樂(lè)編程:人工智能深度學(xué)習(xí)基礎(chǔ)
- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- Visual Basic程序設(shè)計(jì)教程
- 新編Premiere Pro CC從入門到精通
- iOS編程基礎(chǔ):Swift、Xcode和Cocoa入門指南
- Learning Docker Networking
- 嵌入式Linux C語(yǔ)言程序設(shè)計(jì)基礎(chǔ)教程
- Monitoring Docker
- C/C++代碼調(diào)試的藝術(shù)(第2版)
- JBoss AS 7 Development
- Spring Boot 2+Thymeleaf企業(yè)應(yīng)用實(shí)戰(zhàn)
- Java網(wǎng)絡(luò)編程實(shí)用精解
- Django 2.0 入門與實(shí)踐
- Visual C++ 開發(fā)從入門到精通
- 面向?qū)ο蟪绦蛟O(shè)計(jì)教程(C#版)