A few questions about interceptors
marcelstoer Oct 23, 2010 5:36 PMI'm helping a few friends cleaning-up/refactoring a Seam-based project which, besides the web interface, also has a REST interface.
Personally I'm very familiar with Spring but not with Seam - I might be biased. I've got a few questions about a piece of code whose underlying pattern
I see time and time again in this project.
@Name("activityPageController") @Scope(ScopeType.PAGE) public class ActivityPageController extends AbstractPageController { @In private FacesContext facesContext; @In private LocaleSelector localeSelector; @In(required = false) // outjected in Authenticator (Identity.EVENT_POST_AUTHENTICATE) private UserDTO loggedInUser; @In private CurrentRequest currentRequest; @In private GetName getNameService; ...some more service references... @In private SessionData sessionData; @In private Conversation conversation; @Override public void preparePage() { ...some code...
While injecting dependencies as seen above seems fair in components with page scope (as with the above controller) its usage feels very wrong for the REST interface.
In my opinion a REST interface implementation is like a facade aggregating functionality of a number of underlying services. And each REST service/facade must be stateless. Also, ideally I'd like them to be singletons with JVM-scope (i.e. one instance per JVM, which is default behavior in Spring). In my friend's project I see REST services like this:
@Name("activityRestService") @Scope(ScopeType.EVENT) @Path("/activity") public class ActivityRestService { ...a number of service dependencies... @In private CreateActivity createActivityService; ...a number of service dependencies... @In(required = false) // outjected in Authenticator (Identity.EVENT_POST_AUTHENTICATE) private UserDTO loggedInUser; ..methods...
I'm not quite sure whether my concepts can't be applied to Seam, whether my friends didn't use Seam properly or whether the above really is wrong. My complaints:
- The scope is wrong (don't want a new instance for each event), but there is no singleton scope...only application but that's not quite the same (think clusters)
- If this service is stateless I don't want the same dependent services be injected again and again before each method call (that's what @In causes). Hence, all methods should get their dependencies through Component.getInstance()???
- If a service method really needs to get a hold of the logged in user it should get it through Component.getInstance() or some other means like ThreadLocal. Injecting the logged in user with @In effectively violates the desired
statelessness
of this service.
I'd appreciate if someone familiar with Seam could explain if and where I'm wrong.
Thanks!
FYI, I'm familiar with the discussion about Component.getInstance vs. Injection in this forum.