会话未锁定时无法从父项中删除

cannot remove from parent when the session is not locked

当我用 2 个会话测试我的应用程序时,在一个浏览器中一切正常,在另一个浏览器中出现异常。

@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@PreserveOnRefresh
@SpringComponent
public class TestUI extends UI {

    @Autowired
    MainLayout content;

    @Autowired
    DetailView detailView;

    @Override
    protected void init(VaadinRequest request) {
        VaadinSession.setCurrent(VaadinSession.getCurrent());
        Navigator navigator = new Navigator(this, detailView);
        navigator.addView("tests", TestView.class);
        setContent(content);
    }
}

java.lang.IllegalStateException: Cannot remove from parent when the session is not locked. Furthermore, there is another locked session, indicating that the component might be about to be moved from one session to another. at com.vaadin.ui.AbstractSingleComponentContainer.removeFromParent(AbstractSingleComponentContainer.java:175) at com.vaadin.ui.AbstractSingleComponentContainer.setContent(AbstractSingleComponentContainer.java:148) at com.vaadin.ui.UI.setContent(UI.java:1312)

将 Vaadin 组件定义为 Spring 托管 bean 时,必须始终为它们定义一个非单例范围。例如:

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class MainLayout extends VerticalLayout {

这是因为一个 Vaadin 组件只能有一个父组件。其他可能的范围是 @UIScope@ViewScope.