访问控制器内的会话范围 bean

发布于 2024-11-06 06:31:51 字数 1168 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

誰認得朕 2024-11-13 06:31:51

是 - 请参阅 第 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.

森林散布 2024-11-13 06:31:51

默认情况下,Spring 通过在运行时实现接口来创建代理。因此,代理上唯一可用的方法是在 UserInfo 实现的任何接口(如果有)中定义的方法。您可能必须创建一个包含 setUserName() 方法的合适接口。

或者,您需要强制使用基于 CGI 的代理(该代理是在运行时创建的类的子类,因此不需要接口)。指定:

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

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:

<bean id="userInfo" class="net.sandbox.sessionbeans.UserInfo" scope="session" >
   <aop:scoped-proxy proxy-target-class="true"/>
</bean>
唔猫 2024-11-13 06:31:51

关于此评论:

我尝试应用这种技术。我把
豆子里面
定义和我 @Autowired'd 私有
用户信息 用户信息。似乎有效,
但由于某种原因,豆子的设置者
函数没有正确执行...
i.imgur.com/zkxVA.png – 彼得 1 小时

除非接口有setter方法,否则setter方法在Proxy上不可用。

About this comment:

I tried applying this technique. I put
inside the bean
definition and I @Autowired'd private
UserInfo userInfo. It seems to work,
but for some reason the bean's setter
function isn't executed properly...
i.imgur.com/zkxVA.png – Pieter 1 hour
ago

If you use interface-based proxies, the setter method is not available on the Proxy unless the interface has the setter method.

森末i 2024-11-13 06:31:51

如果您不喜欢 XML,也可以使用 ObjectFactory像这样:

@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();
    }
}

注意:使用 Spring Boot 1.5.6 (Spring 4.3) 进行测试

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

@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 Boot 1.5.6 (Spring 4.3)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文