title: picocontainer for singleton DI
body: <p>I'm trying to use picocontainer for DI but still getting my shared object instantiated several times instead of being automatically managed as a singleton. Here's an example to illustrate. Classes ASteps and BSteps receive a SharedObject instance through their constructors. I expected it to be managed as a singleton by picocontainer: instantiated only once, as per the Cucumber docs. Instead, I see it gets instantiated once for ASteps and once for BSteps:</p>
  
  <pre><code>Running my.domain.CucumberRunTest
   INFO [main] (CucumberHooks.java:15) - Executing before()
   INFO [main] (SharedObject.java:11) - SharedObject - instantiated
   INFO [main] (ASteps.java:21) - Executing a_step_one()
   INFO [main] (ASteps.java:26) - Executing a_step_two()
   INFO [main] (ASteps.java:31) - Executing a_step_three()
   INFO [main] (CucumberHooks.java:20) - Executing after()
   INFO [main] (CucumberHooks.java:15) - Executing before()
   INFO [main] (SharedObject.java:11) - SharedObject - instantiated
   INFO [main] (BSteps.java:23) - Executing b_step_one()
   INFO [main] (BSteps.java:28) - Executing b_step_two()
   INFO [main] (BSteps.java:33) - Executing b_step_three()
   INFO [main] (CucumberHooks.java:20) - Executing after()
  </code></pre>
  
  <p>What am I doing wrong? Here is the code:</p>
  
  <pre><code>package my.domain;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import cucumber.api.java.en.Given;
  import cucumber.api.java.en.Then;
  import cucumber.api.java.en.When;
  
  public class ASteps {
  
  final Logger log = LoggerFactory.getLogger(getClass());
  SharedObject sharedObject;
  
  public ASteps(SharedObject sharedObject) {
  this.sharedObject = sharedObject;
  }
  
  @Given("^A step one$")
  public void a_step_one() {
  log.info("Executing a_step_one()");
  }
  
  @When("^A step two$")
  public void a_step_two() {
  log.info("Executing a_step_two()");
  }
  
  @Then("^A step three$")
  public void a_step_three() {
  log.info("Executing a_step_three()");
  }
  }
  
  ************************
  
  package my.domain;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import cucumber.api.java.en.Given;
  import cucumber.api.java.en.Then;
  import cucumber.api.java.en.When;
  
  public class BSteps {
  
  final Logger log = LoggerFactory.getLogger(getClass());
  SharedObject sharedObject;
  
  public BSteps(SharedObject sharedObject) {
  this.sharedObject = sharedObject;
  }
  
  @Given("^B step one$")
  public void b_step_one() {
  log.info("Executing b_step_one()");
  }
  
  @When("^B step two$")
  public void b_step_two() {
  log.info("Executing b_step_two()");
  }
  
  @Then("^B step three$")
  public void b_step_three() {
  log.info("Executing b_step_three()");
  }
  }
  
  ************************
  
  package my.domain;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import cucumber.api.java.After;
  import cucumber.api.java.Before;
  
  public class CucumberHooks {
  
  final Logger log = LoggerFactory.getLogger(getClass());
  
  @Before
  public void before() {
  log.info("Executing before()");
  }
  
  @After
  public void after() {
  log.info("Executing after()");
  
  }
  }
  
  *********************
  
  package my.domain;
  
  import org.junit.runner.RunWith;
  
  import cucumber.api.junit.Cucumber;
  
  @RunWith(Cucumber.class)
  public class CucumberRunTest {
  
  }
  
  **********************
  
  package my.domain;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  public class SharedObject {
  
  final Logger log = LoggerFactory.getLogger(getClass());
  
  public SharedObject() {
  log.info("SharedObject - instantiated");
  }
  }
  </code></pre>