自动装配的 vaadin 组件获取索引 -1

autowired vaadin component gets index -1

我使用 vaadin-spring。我创建了 public abstract class AbstractBasicVerticalLayoutWithMenu extends HorizontalLayout implements View 并且有两个正常的 classes 扩展它。摘要 class I @Autowire 菜单组件定义:

@UIScope @SpringComponent public class Menu extends CssLayout

两个 class 都有注释

@UIScope @SpringView

我在其中一个视图中遇到了一个非常奇怪的问题,即我的菜单组件没有显示。

我做了一些挖掘并添加了日志记录。在

@Override public void enter(ViewChangeEvent event)

方法我添加了日志记录并尝试了很多东西,我发现当我这样做时 this.getComponentIndex(menu) 那个错误的视图组件有索引 -1!

有趣的是,如果我从导航器中删除正确显示的视图(我这样做:getNavigator().addView(FirstTesterView.VIEW_NAME, firstTesterView);),那么这个不正确的视图就会恢复正确。

我不知道为什么,但看起来只有第一个扩展 acstract 的实例化 classes 获得 @Autowired 组件,而不是下一个。

我做错了什么?

索引 -1 表示在子项中找不到该组件。

现在,如果您的组件在任何给定时间是 singleton, which is the default scope for a spring bean, then you always get the same instance. Furthermore, a Vaadin component can only have one parent,那么您可能在日志中遗漏了一些 IllegalStateException

要解决此问题,您可以将菜单的范围设置为 prototype,这样您每次都能获得一个新实例。