访问控制器内的会话范围 bean
我正在 Spring 3 中试验会话范围的 bean。我有以下 bean 定义:
<bean id="userInfo" class="net.sandbox.sessionbeans.UserInfo" scope="session" />
这是 net.sandbox.controllers.RegistrationController
,一个需要访问此 bean 的控制器类。为了简洁起见,我删除了进口。
@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";
}
}
有没有办法自动将我定义的会话范围 bean 绑定到 RegistrationController
中的成员变量 private UserInfo userInfo
?
I'm experimenting with session-scoped beans in Spring 3. I have the following bean definition:
<bean id="userInfo" class="net.sandbox.sessionbeans.UserInfo" scope="session" />
Here is net.sandbox.controllers.RegistrationController
, a controller class that needs access to this bean. I've taken out the imports for brevity's sake.
@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 tie the session-scoped bean I defined to the member variable private UserInfo userInfo
in RegistrationController
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是 - 请参阅 第 3.4 节Spring 手册的 .5.4,“作用域 bean 作为依赖项”。
简而言之,您可以要求 Spring 将会话作用域 bean 包装在单例代理中,当您调用作用域 bean 上的方法时,该代理会查找正确的会话。这称为“范围代理”,并使用
配置宏。然后,您可以像注入任何其他引用一样注入引用(例如
或@Autowired
)。详情请参阅上面的链接。Yes - see section 3.4.5.4 of the Spring manual, "Scoped beans as dependencies".
Briefly, you can ask Spring to wrap your session-scoped bean in a singleton proxy, which looks up the correct session when you invoke a method on the scoped bean. This is called a "scoped proxy", and uses the
<aop:scoped-proxy>
config macro. You can then inject the reference as you would any other (e.g.<property>
, or@Autowired
). See the above link for details.默认情况下,Spring 通过在运行时实现接口来创建代理。因此,代理上唯一可用的方法是在 UserInfo 实现的任何接口(如果有)中定义的方法。您可能必须创建一个包含 setUserName() 方法的合适接口。
或者,您需要强制使用基于 CGI 的代理(该代理是在运行时创建的类的子类,因此不需要接口)。指定:
By default, Spring creates a proxy by implementing an interface at run-time. So, the only methods available on the proxy are those defined in any interfaces UserInfo implements (if any). You may have to create a suitable interface that includes the setUserName() method.
Alternatively, you will need to force a CGI based proxy (the proxy is a sub-class of your class created at run-time so it doesn't need an interface). Specify:
关于此评论:
除非接口有setter方法,否则setter方法在Proxy上不可用。
About this comment:
If you use interface-based proxies, the setter method is not available on the Proxy unless the interface has the setter method.
如果您不喜欢 XML,也可以使用
ObjectFactory
像这样:注意:使用 Spring Boot 1.5.6 (Spring 4.3) 进行测试
If you don't like XML, you can also use
ObjectFactory<T>
like this :Note: Tested with Spring Boot 1.5.6 (Spring 4.3)