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 org.picocontainer.Characteristics; 014 import org.picocontainer.ComponentAdapter; 015 import org.picocontainer.ComponentMonitor; 016 import org.picocontainer.LifecycleStrategy; 017 import org.picocontainer.Parameter; 018 import org.picocontainer.PicoCompositionException; 019 import org.picocontainer.behaviors.AbstractBehaviorFactory; 020 021 import java.util.Properties; 022 023 024 /** 025 * A {@link org.picocontainer.InjectionFactory} for JavaBeans. 026 * The factory creates {@link SetterInjector}. 027 * 028 * @author Jörg Schaible 029 */ 030 @SuppressWarnings("serial") 031 public class SetterInjection extends AbstractInjectionFactory { 032 033 private final String prefix; 034 035 public SetterInjection(String prefix) { 036 this.prefix = prefix; 037 } 038 039 public SetterInjection() { 040 this("set"); 041 } 042 043 /** 044 * Create a {@link SetterInjector}. 045 * 046 * @param monitor 047 * @param lifecycleStrategy 048 * @param componentProperties 049 * @param componentKey The component's key 050 * @param componentImplementation The class of the bean. 051 * @param parameters Any parameters for the setters. If null the adapter 052 * solves the dependencies for all setters internally. Otherwise 053 * the number parameters must match the number of the setter. 054 * @return Returns a new {@link SetterInjector}. 055 * @throws PicoCompositionException if dependencies cannot be solved 056 */ 057 public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor monitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey, Class<T> componentImplementation, Parameter... parameters) 058 throws PicoCompositionException { 059 boolean useNames = AbstractBehaviorFactory.arePropertiesPresent(componentProperties, Characteristics.USE_NAMES, true); 060 return wrapLifeCycle(monitor.newInjector(new SetterInjector(componentKey, componentImplementation, parameters, monitor, prefix, useNames)), lifecycleStrategy); 061 } 062 063 }