I want to pass a session-scoped domain bean around my controllers for consistency and simplicity - but this doesn't seem possible OOTB. Hope someone can advise.
Question: Can a session-scoped bean be exposed as a MVC Controller argument
There appears to be an annotation for this: @SessionAttributes("myBean") however this only maintains a Controller-level scope.
I'm looking to avoid having to interact with HttpSession and instead pass around my domain object graph consistently through my controllers. This would seem a fairly standard requirement.
This has benefits:
- Testability - just inject bean to test and not have to mock out HttpSession
- Abstraction - avoids Servlet model concerns and just the business problem
Here's the current config:
@Controller
@SessionAttributes("customer")
public class LoginController {
@Inject Customer customer;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String welcome(Customer customer) {
...
return "loginDetailsView";
}
public String processLogin(@Valid Customer customer, BindingResult bindingResult) {
...
if (bindResult.hasErrors()) {
return "loginDetailsView";
else {
return "homePageView";
}
}
'Customer' session bean is a regular POJO using XML configured aop-proxy (CGLIB) to allow the session scoped bean to be injected into singletons (controller and service classes):
<beans:bean id="customer" class="com.mypackage.domain.Customer" scope="session">
<aop:scoped-proxy proxy-target-class="true"/>
</beans:bean>
The following question is virtually the same but there are no answers that don't involve extension to the Spring 3 core framework.
Has this been intentionally omitted by Spring's designers? My intent is to use the domain model to back the forms without using 'form beans' and a layer of mappers.
Can anyone advise a way / or indicate if this is poor practice?
Prior question with bespoke solution:
How to pass a session attribute as method argument (parameter) with Spring MVC
Please note: attempting to minimise the dependencies on the Spring framework and use Annotation support esp JSR-303 etc. Will adopt any OOTB solution - please don't suggest custom extentions.