That example is a bit contrived. How about injection of listeners to a
repository
public class UsefulRespository {
@Inject
Collection<UsefulUpdateListener> listeners
public void update(Useful u) {
store.replace(u);
for (UsefulUpdateListener l: listeners) {
l.notify(u);
}
}
}
The key aspect is that the repository does not care if there are listeners
but
provides the facility to notify them. Ideally if there were listeners they
would be
injected and if not an empty collection. I've been doing this for a long
time in
Spring using required=false and testing for nullity.
Obviously @Optional or @Required could be used to force it
not-null/not-empty but
have an indication that implementors do something consistent with
collections would
be really useful.
Surely an empty collection represents "no listeners". So I don't understand
your
usecase. You would only need some special "optional injection" feature if
you wanted
to inject null, not if you want to inject an empty collection.
You could use something like Guice's multibindings to populate an
initially-empty set
with values. http://code.google.com/p/google-guice/wiki/Multibindings
such feature implemented by both guice and spring,so why not add support in
jsr-330?
Comment #5 on issue 20 by crazyboblee: make Injection optional
http://code.google.com/p/atinject/issues/detail?id=20
We decided against supporting optional injection in JSR-330:
http://code.google.com/p/atinject/issues/detail?id=1&can=1&q=optional
how about provide a @Optional qualifier?