Reputation: 305
I am implementing a multi-tenant application. Many of my resources have paths like "/api/tenant/{tenant_id}/resource/path/". What I would like to do is to inject different DAOs (or possibly other objects) to the resource based on the "{tenant_id}" path parameter.
I have two suboptimal ideas on how to achieve something similar:
Use a wrapper class like this:
class SomeDAOWrapper
{
SomeDAO getSomeDAO()
{
return new SomeDAO(tenantId_m);
// Alternatively we could store the DAOs in some hash-table
// with tenantId_m as the key.
}
@PathParam("tenant_id")
private long tenantId_m;
}
Then in my resource class I would have SomeDAOWrapper
as an attribute annotated with @BeanParam
.
Use a sub-resource locator on path "/api/tenant/{tenant_id}" that would return the resources with the correct DAOs.
Any other ideas? Ideally what I would like to do is to simply have SomeDAO
attribute in my resource class that is annotated with @Inject
or something similar (and that would use some factory that takes the tenant_id
path parameter into account).
Upvotes: 0
Views: 825
Reputation: 7275
I ran into this same kind of problem and ended up using the guice multibinder solution. You essentially bind your Dao's to a MultiBinder and then Inject a factory into your service. This was the cleanest solution I could come up with for the problem.
Check out this url, it is pretty much what I did to get dependency injection working with a resource that needed a specific dao.
https://groups.google.com/forum/#!topic/google-guice/J6S77sILTAY
Upvotes: 1