Here's a generic, bean-independent class:
@ApplicationScoped public class FavouriteChooser<T> { public T getFavourite() {
How many instances of this bean will be in the application?
Here is the injection site:
@Inject private FavouriteChooser<String> favouriteWord;
And one more thing:
@Inject private FavouriteChooser<Integer> favouriteNumber;
Do you want to change your answer ?: D
Oh, and here's another:
@Inject private FavouriteChooser<CharSequence> favouriteLetters;
EDIT. If you want a solution, I would suggest making your abstract abstract class and adding specific subclasses that bind the type. So:
public abstract class MyProducer<T> {...} @Named @SessionScoped public class MyStringProducer extends MyProducer<String> {} @Named @SessionScoped public class MyIntegerProducer extends MyProducer<Integer> {}
This is a pattern, but there are only three lines for each type. Keep in mind that this will give you one instance per session for each type that you might not need.
Tom anderson
source share