PicoContainer
  1. PicoContainer
  2. PICO-388

Multiple types of injection with @Inject annotation

    Details

    • Type: Bug Bug
    • Status: Resolved Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 3.0
    • Fix Version/s: 3.0
    • Component/s: PicoContainer (Java)
    • Labels:
      None
    • Testcase included:
      yes
    • Number of attachments :
      0

      Description

      Discussed here:

      http://article.gmane.org/gmane.comp.java.picocontainer.user/1293/match=inject

      and other places in the mailing list.

      	@Test
      	 public void testComponentCanUseConstructorAndAtInjectWithoutAnInitMethod() {
      		
      		
      	        final MutablePicoContainer pico = new DefaultPicoContainer();
      	        pico.addComponent(Carrot.class)
      	        	.addComponent(Cheese.class)
      	        	.addComponent(Mixer.class);
      
      	        // fails with org.picocontainer.PicoCompositionException: java.lang.NoSuchMethodException: ...$Mixer.<init>()
      	        final Utensil u = pico.getComponent(Utensil.class);
      	        assertEquals("Mixing carrot with some cheese", u.use());
      	    }
      
      	    public static final class Cheese {
      	    }
      
      	    public static final class Carrot {
      	    }
      
      	    public static interface Utensil {
      	        String use();
      	    }
      
      	    public static class Spoon implements Utensil {
      	        public String use() {
      	            return "there is no spoon";
      	        }
      	    }
      
      	    public static class Mixer implements Utensil {
      	        private final Cheese cheese;
      	        @Inject
      	        private Carrot veg;
      
      	        public Mixer(Cheese cheese) {
      	            this.cheese = cheese;
      	        }
      
      	        public String use() {
      	            return "Mixing " + simpleClassNameOrNull(veg) + " with some " + simpleClassNameOrNull(cheese);
      	        }
      	    }
      
      	    private static String simpleClassNameOrNull(Object o) {
      	        return o == null ? null : o.getClass().getSimpleName().toLowerCase();
      	    }
      
      

        People

        • Assignee:
          Michael Rimov
          Reporter:
          Michael Rimov
        • Votes:
          0 Vote for this issue
          Watchers:
          1 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: