Access to the session bean in the controller

I am experimenting with beans in Spring with a Spring 3 session. I have the following bean definition:

<bean id="userInfo" class="net.sandbox.sessionbeans.UserInfo" scope="session" /> 

Here is net.sandbox.controllers.RegistrationController , the controller class that needs access to this bean. I took the import for short.

 @Controller @RequestMapping("/register") public class RegistrationController { private UserInfo userInfo; // This should reference the session-scoped bean @RequestMapping(method = RequestMethod.GET) public String showRegForm(Model model) { RegistrationForm regForm = new RegistrationForm(); model.addAttribute("regform", regForm); return "regform"; } @RequestMapping(method = RequestMethod.POST) public String validateForm(@Valid RegistrationForm regForm, BindingResult result, Model model) { if (result.hasErrors()) { return "regform"; } userInfo.setUserName(regForm.getFirstName()); model.addAttribute("regform", regForm); return "regsuccess"; } } 

Is there a way to automatically bind a session-limited bean i to the private UserInfo userInfo variable private UserInfo userInfo member private UserInfo userInfo in RegistrationController ?

+4
source share
4 answers

Yes - see the section in section 3.4.5.4 of the Spring manual, "Braces of Beans as Dependencies . "

In short, you can ask Spring to wrap your scope bean in a singleton proxy that is looking for the right session when calling a method in the bean scope. This is called a โ€œscoped proxy" and uses the <aop:scoped-proxy> configuration macro. You can then enter the link, like any other (for example, <property> or @Autowired ). See the link above for more details.

+7
source

By default, Spring creates a proxy server, implementing an interface at runtime. Thus, the only methods available in the proxy module are those that are defined in any UserInfo interfaces (if any). You may need to create a suitable interface that includes the setUserName () method.

Alternatively, you will need to force a CGI-based proxy server (proxy is a subclass of the class created at runtime, so it does not need an interface). Indicate:

 <bean id="userInfo" class="net.sandbox.sessionbeans.UserInfo" scope="session" > <aop:scoped-proxy proxy-target-class="true"/> </bean> 
+1
source

About this comment:

I tried to apply this technique. I put inside the bean definition and I @ Autowired'd private UserInfo userInfo. This seems to work, but for some reason the installer bean function is not executed properly ... i.imgur.com/zkxVA.png - Pieter 1 hour ago

If you use an interface-based proxy, the configuration method is not available on the proxy if the interface does not have a setter method.

0
source

If you don't like XML, you can also use ObjectFactory<T> as follows:

 @RestController public class MyController { private final ObjectFactory<MySessionScopedComponent> OFSession; @Autowired public MyController(ObjectFactory<MySessionScopedComponent> OFSession) { this.OFSession = OFSession; } @RequestMapping(path = "/path", method = RequestMethod.GET) public String myMethod () { MySessionScopedComponent sessionBean = OFSession.getObject(); // Do some stuff return bean.myValue(); } } 

Note. Tested with Spring Download 1.5.6 (Spring 4.3)

0
source

All Articles