Gbru
Gbru

Reputation: 1087

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;
    }
}

Upvotes: 0

Views: 1513

Answers (1)

Elias
Elias

Reputation: 1562

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

Upvotes: 2

Related Questions