How can I programmatically inject a Java CDI 1.1+ managed bean into a local variable in a static method?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Use for instance this utility class. You basically have to obtain instance of
BeanManager
and than grab the bean you want from it (imagine something like JNDI lookup).Update
You could also use CDI utility class offered in CDI 1.1
The link suggested by @Petr Mensik is very useful. I am using the same code in my example.
Here is a way to get an instance of the class in instance methods/static methods. It is always better to code for interfaces instead of using the class name hard coded in the methods.
This programmatic look up of beans can be quite useful when you have an application scoped object with method that requires an instance of a class that may change over time. So, it is always better to extract the interface and use programmatic bean look up for the sake of loose coupling.
To inject an instance of class
C
:This is available in CDI 1.1+
You should include qualifiers:
@BRS
With: