2 Replies Latest reply on Apr 20, 2016 6:18 AM by Christian Wansart

    Why injection through new does not work?

    Christian Wansart Newbie



      I am trying to inject a List in an object that's called through a number of elements. Using the @Singleton annotation and injecting Lister in my UI class, it works.


      I have:

      • App that creates a WeldContainer
      • It creates an object of UI and runs it's start() method
      • in the start() method it creates a new object of Lister with "new Lister()"
      • in Lister there's an attribute that is injected with @Inject and the @FooFinder qualifier
      • in Lister is a getFoos() method that returns finder.getFoos()
      • Finder has an attribute List<Foo> which is injected by a @Produces in FooCreator and has a getFoos() method which returns this list
      • FooCreator simply returns a list:
        @Produces public List<Foo> createFoos() {
            List<Foo> fooList = new ArrayList<>();
            fooList.add(new Foo());
            fooList.add(new Foo());
            fooList.add(new Foo()));
            return fooList;

      When I run lister.getFoos().stream().forEach(System.out::println); in my UI class with the new Lister() generated Lister, the fooList is null.

      I added @Singleton to Lister and tried @Inject Lister lister, then it works.


      Why is that? I thought it gets injected because the injection annotations will make sure that fooList will be injected.


      I uploaded the complete code to Github: WeldProducesTest/src/main/java/com/mycompany/weldproducestest at master · cwansart/WeldProducesTest · GitHub