dpolishuk / atinject

Automatically exported from code.google.com/p/atinject
0 stars 0 forks source link

make Injection optional #20

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
like @Autowired(required=false) in spring

@Inject(required=false)
private UserService userService;

or

@Inject
@Optional
private UserService userService;

Original issue reported on code.google.com by zhouyanm...@gmail.com on 2 Dec 2009 at 7:34

GoogleCodeExporter commented 8 years ago
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.

Original comment by mccallum...@gmail.com on 8 Feb 2010 at 7:00

GoogleCodeExporter commented 8 years ago
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.

Original comment by gavin.k...@gmail.com on 8 Feb 2010 at 9:55

GoogleCodeExporter commented 8 years ago
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

Original comment by limpbizkit on 9 Feb 2010 at 1:07

GoogleCodeExporter commented 8 years ago
such feature implemented by both guice and spring,so why not add support in 
jsr-330?

Original comment by zhouyanm...@gmail.com on 9 Feb 2010 at 12:57

GoogleCodeExporter commented 8 years ago
We decided against supporting optional injection in JSR-330: 
http://code.google.com/p/atinject/issues/detail?id=1&can=1&q=optional

Original comment by crazybob...@gmail.com on 18 Oct 2010 at 11:46

GoogleCodeExporter commented 8 years ago
how about provide a @Optional qualifier?

Original comment by zhouyanm...@gmail.com on 19 Oct 2010 at 1:47

GoogleCodeExporter commented 8 years ago
+1 to @Optional qualifier, though in theory different injectors could provide 
their own ways of doing it.  For instance, ioc-containers that support 
"Greediest constructor" don't really need @optional.  Field-injector 
optionality such as guice has could simply have Guice support an @Optional 
annotation.

Original comment by cgruber@google.com on 6 Dec 2011 at 4:51

GoogleCodeExporter commented 8 years ago
Please reopen this, as there is no way to have collection injection!

Original comment by ptit...@gmail.com on 13 Dec 2011 at 2:48