Reputation: 125
I am new to CDI. I can't figure out how convert the following code using CDI.
Class Client {
void method(){
List<Events> events = getEvents();
I b = new B(events);
I c = new C("Hello");
}
List<Events> getEvents(){
//Do Something
return events;
}
}
Class B implements I{
List<Events> events ;
B(List<Events> events){
this.events = events;
}
}
Class C implements I{
String s;
C(String s){
this.s = s;
}
}
I used Qualifiers to avoid ambiguity but can figure out how to pass parameters from the client.Do I need to use producer to inject the List and string into Class B and C respectively?
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface Ii {
Type value() ;
public enum Type {
B,
C
}
}
Class Client {
@Inject @Ii(Ii.type.B)
B b;
@Inject @Ii(Ii.type.C)
C b;
}
@Ii(Ii.type.B)
Class B {
}
@Ii(Ii.type.C)
Class C {
}
Upvotes: 0
Views: 799
Reputation: 5903
You need to declare a Producer.
@Produces @Ii(Ii.type.B)
public void produceB {
return Ii.type.B;
}
@Produces @Ii(Ii.type.C)
public void produceC {
return Ii.type.C;
}
In Order to you events you need to annotate the parameter that should create events with @Observes
. In the code below you can see how you get a list of fired events.
@Inject
private List<Event> events;
Upvotes: 2