Raduan Santos
Raduan Santos

Reputation: 1063

Google Guice, how to inject resources before, and after inject the class

I have the following situation :

Class ServiceCaller:

public class ServiceCaller{
    private Service service;
    public void initialize(List<String> params){
        //get the params from list and set to String objects
        service = new Service(param1,param2);
    }
}

Class Service:

public class Service{
    SomeClass classe;
    public Service(String param1,String param2){
        //call methods to do stuffs with params
        classe = new SomeClass();
    }
}

My Application has a framework that reads a .properties and calls ServiceCaller Class and goes to initialize and fill the param in ServiceCaller, after construct. The Service class, simply work with the parameter, and instances the SomeClass class.

I'm trying to pass all of this to Google Guice, and my classes were so :

Class Service Caller:

public class ServiceCaller{
    private final Service service;

    @Inject
    public ServiceCaller(Service service){
        this.service = service;
    }
    public initialize(List<String> params){
        //set params to objects...
    }
}

and Class Service :

public class Service{
    private final SomeClass classe;

    @Inject
    public Service(SomeClass classe, String param1,String param2){
        //call methods to do stuffs with params
        this.classe = classe;
    }
}

Here's my doubt : How i can do this ?

1 - In ServiceCaller, the injected construtor is called BEFORE the initMethod, this way he dont have the Params yet. I think that generates a nullPointer in Service class. How i can receive the String parameters ?

2 - How can i inject the Service Class inside ServiceCaller Class ? He needs to call init, mount Service class with parameters, and only in this moment inject Service (with params already) to the ServiceCaller.

Upvotes: 2

Views: 1316

Answers (1)

Tom Anderson
Tom Anderson

Reputation: 47163

Is it really the ServiceCaller's job to configure the Service? If so, then this case is not really suitable for dependency injection. Just create Service with new as you do now.

But it's possible that it's not really ServiceCaller's job to create the Service. Rather, you should inject the parameters directly into the Service to configure it, and then inject the configured Service into the ServiceCaller.

Now, if the problem is that the parameters come from the ServiceCaller, but the SomeClass needs to be injected, then neither ServiceCaller nor Guice will be able to do the job on their own. I would suggest you split the construction: create a ServiceFactory which can be injected with SomeClass, and then injected into ServiceCaller, and which then provides a factory method which takes the parameters. Something like:

ServiceCaller {
    private final ServiceFactory serviceFactory;
    private Service service;

    @Inject
    public ServiceCaller(ServiceFactory serviceFactory) {
        this.serviceFactory = serviceFactory;
    }

    public void initialize(List<String> params) {
        service = serviceFactory.create(params.get(0), params.get(1));
    }
}

class Service {
    private final SomeClass classe;

    public Service(SomeClass classe, String param1, String param2) {
        //call methods to do stuffs with params
        this.classe = classe;
    }
}

class ServiceFactory {
    private final SomeClass classe;

    @Inject
    public ServiceFactory(SomeClass classe) {
        this.classe = classe;
    }

    public Service create(String param1, String param2) {
        return new Service(classe, param1, param2);
    }
}

Upvotes: 1

Related Questions