001 /***************************************************************************** 002 * Copyright (c) PicoContainer Organization. All rights reserved. * 003 * ------------------------------------------------------------------------- * 004 * The software in this package is published under the terms of the BSD * 005 * style license a copy of which has been included with this distribution in * 006 * the LICENSE.txt file. * 007 * * 008 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant * 009 *****************************************************************************/ 010 011 package org.picocontainer.injectors; 012 013 import java.util.Properties; 014 015 import org.picocontainer.Characteristics; 016 import org.picocontainer.ComponentAdapter; 017 import org.picocontainer.ComponentMonitor; 018 import org.picocontainer.LifecycleStrategy; 019 import org.picocontainer.Parameter; 020 import org.picocontainer.PicoCompositionException; 021 import org.picocontainer.behaviors.AbstractBehaviorFactory; 022 023 /** 024 * A {@link org.picocontainer.InjectionFactory} for constructor injection. 025 * The factory creates {@link ConstructorInjector}. 026 * 027 * If there is more than one constructor for the component, the one with the 028 * most satisfiable parameters will be used. By default, the choice of 029 * constructor for the component in question will be remembered between usages. 030 * 031 * @author Paul Hammant 032 * @author Jon Tirsén 033 */ 034 @SuppressWarnings("serial") 035 public class ConstructorInjection extends AbstractInjectionFactory { 036 037 private final boolean rememberChosenConstructor; 038 039 /** 040 * 041 * @param rememberChosenConstructor whether 'which constructor?' should be remembered 042 * from use to use for the associated injector. 043 */ 044 public ConstructorInjection(boolean rememberChosenConstructor) { 045 this.rememberChosenConstructor = rememberChosenConstructor; 046 } 047 048 /** 049 * Will remember which constructor to use between usages on the associated 050 * Injector. 051 */ 052 public ConstructorInjection() { 053 this(true); 054 } 055 056 public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor monitor, LifecycleStrategy lifecycleStrategy, Properties properties, Object componentKey, 057 Class<T> componentImplementation, Parameter... parameters) throws PicoCompositionException { 058 boolean useNames = AbstractBehaviorFactory.arePropertiesPresent(properties, Characteristics.USE_NAMES, true); 059 ConstructorInjector injector = new ConstructorInjector(componentKey, componentImplementation, parameters, monitor, useNames, rememberChosenConstructor); 060 injector.enableEmjection(AbstractBehaviorFactory.removePropertiesIfPresent(properties, Characteristics.EMJECTION_ENABLED)); 061 return wrapLifeCycle(monitor.newInjector(injector), lifecycleStrategy); 062 } 063 }