Mixing Field and Constructor injection

487 views
Skip to first unread message

Itrat Jameel

unread,
Nov 12, 2017, 2:40:32 PM11/12/17
to google-guice
Hi Guice team,

I have a usecase where I am using an external jar that is based on Spring, while my code is on Google guice.

I am trying to inject dependencies in a class of my dependency jar, by writing modules.


External class:

public class PTRS {
    @Inject
    private Event countEvent;
    @Inject
    private Event durationEvent;
    private GeoServiceClient gClient;
    private AssignmentClient aClient;
    public void setGeoServiceClient(GeoServiceClient client){this.gClient=client}
    public void setAssignmentClient(AssignmentClient client){this.aClient=client}
}

I am able to set the members with setters in @provides method in my module, but the @inject members have null, and I am getting a NullPointerException for countEvent and durationEvent. :(

My uses following provider class to create an object to bind with PTRS class.

class PTRSProvider implements Provider<PTRS>
   {
          public PTRSProvider(){
              }

    @Override
    public PTRSProvider get() {
        PTRS ptrs = new PTRS();
        ptrs.setGeoServiceClient(new client());
        ptrs.setAssignmentServiceClient(new client());
        return ptrs;
    }
}
Can you please guide me as to how I can combine both Setter and Field injection in this case?

Any help would be appreciated! :)

Regards.

Tavian Barnes

unread,
Nov 13, 2017, 11:24:54 AM11/13/17
to google-guice
You can use a MembersInjector<PTRS> to trigger field/method injection.
Reply all
Reply to author
Forward
0 new messages