如何在 Scala 中填充 Wicket @SpringBeans?

发布于 2024-11-15 22:52:26 字数 518 浏览 0 评论 0原文

我有一个带有 @SpringBean 的 Wicket Page

class ScreenDetailsPage(parameters: PageParameters) extends BasePage(parameters) {
    @SpringBean(name = "screenDAO") protected var screenDao: DAO[Screen] = null
    assertNotNull(screenDao)

,但我发现 @SpringBean 未填充。 screenDao 是 val 或 var、protected 或 private 都没有区别。

查找树,我发现 Component 的构造函数(间接)代表其子类初始化 @SpringBeans,但随后对 null 的赋值将取消初始化它。但 Scala 要求进行赋值。

我怎样才能防止这种行为?

I have a Wicket Page with a @SpringBean

class ScreenDetailsPage(parameters: PageParameters) extends BasePage(parameters) {
    @SpringBean(name = "screenDAO") protected var screenDao: DAO[Screen] = null
    assertNotNull(screenDao)

and I find that the @SpringBean is not populated. It makes no difference if screenDao is val or var, protected or private.

Looking up the tree I find that the constructor of Component (indirectly) initialises @SpringBeans on behalf of its subclasses, but then the assignment to null is uninitializing it. But the assignment is required by Scala.

How can I prevent this behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

月亮坠入山谷 2024-11-22 22:52:26

尝试一下

class ScreenDetailsPage(parameters: PageParameters) extends BasePage(parameters) {
    @SpringBean(name = "screenDAO") protected var screenDao: DAO[Screen] = _
    assertNotNull(screenDao)

我还没有尝试过这个,但我也在考虑启动一个 Wicket/Scala 项目,并看到 此博客条目,它在其他方面也可能有用。

该博客引用的相关部分是

此外,请注意该字段被分配给下划线 (_),这告诉 Scala 编译器不要初始化,而是将其保留为默认状态(在本例中为 null)。这是注射起作用所必需的。如果显式地将其分配为 null,则
将覆盖 Spring bean,因为注入将在执行 MyPage 的构造函数之前发生。

Try

class ScreenDetailsPage(parameters: PageParameters) extends BasePage(parameters) {
    @SpringBean(name = "screenDAO") protected var screenDao: DAO[Screen] = _
    assertNotNull(screenDao)

I have not tried this, but am also thinking about starting a Wicket/Scala project, and saw this blog entry, which might be useful in other ways as well.

The relevant section quoted from that blog is

In addition, note that the field is assigned to an underscore (_), which tells the Scala compile to NOT initialize, but leave it at the default state (null in this case). This is required for the injection to work. If you assign it to null explicitly, you
will overwrite the Spring bean as the injection will occur before the constructor of MyPage is executed.

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