PicoContainer

# About

- What is PicoContainer?
- Introduction
- Javadoc
- Downloads
- News
- FAQ
- Project Goals
- Support

# Core Concepts

- Component Lifecycle
- Dependency Injection
- Scoped Containers

# Dependency Injection

- Constructor Injection
- Setter Injection
- Annotated Field Injection
- Annotated Method Injection
- Typed Field Injection
- Named Field Injection
- Method Injection
- Multiple Injection
- Composite Injection
- Factory Injection
- Reinjection
- Injection via Providers

# Configuration

- Disambiguation

# Modifying Component Behaviors

- Modifying Behaviors
- Caching and Singletons
- Hiding Implementations
- Thread Safety Behaviors
- AOP Style Interception

# Advanced Concepts

- Fluent Style
- Arrays, Collections and Maps
- Leveraging Parameter Names
- Building Containers
- Component Properties
- Component Configuration
- Monitoring Component Events

# PicoContainer's Architecture

- Design Patterns
- Inversion of Control
- Component Adapters
- Mock Objects
- Terminology
- Container Comparisons
- Transparency

# Container Choices

- DefaultPicoContainer
- CommandLinePicoContainer
- PropertiesPicoContainer
- SystemPropertiesPicoContainer
- TransientPicoContainer
- TieringPicoContainer

# Dependant & Related

- Web Frameworks
- Scripting Support
- Persistence
- Logging

# Anti-patterns

- Propagating Dependency
- Long Constructor Argument List
- Container Dependency
- Unit Tests Use Container
- Singleton

# Help Pages

- Ambiguous Injectable
- Unprocessed Properties

# History

- Inversion of Control History

# Project Info

- Project
- Team

# Hosted by

# Tools

## Setter Injection

## Overview

Setter Injection is where the container or embedder hands dependencies to a component via setter methods after instantiation.

## Example

Consider a plain class that has one private field and one setter for each dependency that it needs.

    public class Apple {
        private Orange orange;
        private Pear pear;
        private Banana banana;
        public setOrange(Orange orange) {
            this.orange = orange;
        }
        public setPear(Pear pear) {
            this.pear = pear;
        }
        public setBanana(Banana banana) {
            this.banana = banana;
        }
       public void initialize() {
           // all setXXXs are now done :-)
       }
       // other methods

    }

Some other technologies marked up the need with a doclet tag:

    ...

      /**
        * @config name="orange"
        */
        public setOrange(Orange orange) {
            this.orange = orange;
        }

    ...

####

The container use the meta-information to resolve all the dependencies. Components need not be interface/implementation separated. That would be the developer's choice.

### Using Setter Injector Components Without a Container.

Setter Injection components can be used directly, without any container. The component-using class will continue to compile, but at run time it will be apparent that there are missing dependencies. The downside of this is that a developer may miss a setXXX(..) method invocation if they are using the component directly. That is fairly small as a risk as it would clearly be caught in the development cycle. Caught in the development cycle, but maybe obscurely so with a NullPointerException.

    Apple apple = new Apple();
    apple.setPear(myPear);

### Using Setter Injection with PicoContainer

The component factory for this is SetterInjection. It only handles setter injection types of components.

    pico = new DefaultPicoContainer(new SetterInjection());
    pico.addComponent(Apple.class);
    pico.addComponent(Banana.class);
    pico.addComponent(Pear.class);
    pico.addComponent(Orange.class);

    // etc
    Apple apple = pico.getComponent(Apple.class);

Setter methods (those prefixed with 'set') may not be your preferred choice. You can force a different prefix to be choosable in PicoContainer, such as 'init' or 'inject'. If you want to use an prefix other than 'set'...

    pico = new DefaultPicoContainer(new SetterInjection("mySynonymForSet"));
    pico.addComponent(Apple.class);
    // etc
    Apple apple = pico.getComponent(Apple.class)

Factory AdaptiveInjection can also handle setter injection types, though it requires that the component was registered with the property 'SDI' in order to activate the Setter Injection functionality. AdaptiveInjection will also fall through to constructor injection if there is no SDI property.

## References + Comparison

Setter Injection is a Dependency Injection variant where an object gets all dependencies via setter methods. PicoContainer support this with SetterInjection, but the PicoContainer team recommends Constructor Injection.

The disadvantage of Setter Injection there is the possibility to forget to set some of the dependencies.