I'm writing some tool class which should be a singleton. However, the functionality of the class requires dependencies on some service interfaces, i.e. PullRequestService. I'm facing the dilemma about how can I make it both singleton and correctly import those services.
When I was reading some documents from Java Bean, I realized that the Inject annotation can also be applied to a public setter. I tried writing something employing the setter, but it's no use. Any ideas?
public class ConflictStrategy extends AbstractMergeStrategy implements IMergeStrategy {
static public final IMergeStrategy instance = new ConflictStrategy();
@ComponentImport
private PullRequestService pullRequestService; // this is null in runtime
private ConflictStrategy() {}
@Inject
public void setComponents(PullRequestService pullRequestService) {
this.pullRequestService = pullRequestService;
}
public MergeCheckResponse inspect(MergeCheckRequest request) {
PullRequest pullRequest = request.getPullRequest();
if (pullRequestCanMerge(pullRequest).isConflicted()) {
return buildResponse(false, "Pull request conflicts");
} else {
return buildResponse(true, "No merge conflicts");
}
}
private PullRequestMergeability pullRequestCanMerge(PullRequest pullRequest) {
final long pullRequestID = pullRequest.getId();
final int repoID = pullRequest.getFromRef().getRepository().getId();
return this.pullRequestService.canMerge(repoID,pullRequestID);
}
}