javadependency-injectioncucumbercucumber-javapicocontainer

Possible to use PicoContainer (Dependency injection) without constructor parameters?


I need to achieve dependency injection using PicoContainer without passing constructor parameters, current setup:

public class Shared_Data  {

    public Account_Pojo account_pojo;

    public Shared_Data(Account_Pojo account_pojo) {
        this.account_pojo = account_pojo;
    }

In the above example I need to achieve DI using PicoContainer without passing: Account_Pojo account_pojo as a parameter to the constructor, is this even possible?

I have tried the following with no luck:

public class Shared_Data {
    public Account_Pojo account_pojo;

    public Shared_Data() {
    }

    public void setAccount_pojo(Account_Pojo account_pojo) {
        this.account_pojo = account_pojo;
    }

    public Account_Pojo getAccount_pojo() {
        setAccount_pojo(account_pojo);
        return account_pojo;
    }
}

Solution

  • You can set up a pico container with a SetterInjection component factory[0].

    pico = new DefaultPicoContainer(new SetterInjection());
    pico.addComponent(Account_Pojo.class);
    

    Something like this should work.

    [0] http://picocontainer.com/setter-injection.html