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

## Injection via Providers

A Provider class is a class with a method named provide that can participate in injection

Consider the following example of a provider:

    public class Chocolatier implements Provider {
      public Chocolate provide(CocaoBeans cocaoBeans) {
        return new Chocolate(cocaoBeans);
      }
    }

And its example usage:

    pico.addAdapter(new ProviderAdapter(new Chocolatier()));
    pico.addComponent(NeedsChocolate.class);

In this example, 'NeedChocolate' and other components added to the PicoContainer instance can depend on a 'Chocolate' instance. Such dependencies are declared in the usual way (Constructor args etc) and PicoContainer will call the provider method to create the Chocolate instance. For the class in question, there must be only one provide method, it must return something other than void, and it can take zero, one or many arguments. Those arguments are themselves dependencies that will be satisfied by PicoContainer at time of invocation.

You will note that the example above shows a transparent class (no implements/extends/throws/imports from PicoContainer's API). It is also possible to extend the Provider class directly to do the same:

    public class Chocolatier2 extends ProviderAdapter {
      public Chocolate provide(CocaoBeans cocaoBeans) {
        return new Chocolate(cocaoBeans);
      }
    }

    ...

    pico.addAdapter(new Chocolatier());
    pico.addComponent(NeedsChocolate.class);

It may be that you want more than one dependency for the provide method. If that is the case and you want to leverage parameter names for disambiguation. To do this for the compositional case, choose the contructor to ProviderAdapter that has a boolean flag (set to true). To do this for the extends case, override the useNames() method.