Hello,
I’m using Vaadin8 + Spring Boot and i’m facing the following problem:
I have a main view, that contains a menu in the top, and a tabsheet (empty at the startup). Each menu options, will add a new tab in the tabsheet, showing a component (Imagine like a browser, with the possibility to switch from a view to another using tabs).
The problem is that since each component that I need to show in the tab, is a Spring injected component, I have no chance to open this component in more than one tab.
if I click on the same menu item more than one time, it will not add a second tab in my TabSheet, cause the same istance has already beed attached.
To explain with minimal code:
MainLayout.java
[code]
@SpringUI
public class MainLayout extends UI {
VerticalLayout verticalLayout = new VerticalLayout();
MenuBar menu = new MenuBar();
TabSheet tabSheet = new TabSheet();
@Autowired
ComponentA componentA;
@Autowired
ComponentB componentB;
@Override
protected void init(VaadinRequest vaadinRequest) {
createMenu();
tabSheet.setSizeFull();
verticalLayout.addComponents(menu, tabSheet);
setContent(verticalLayout);
}
private void createMenu() {
MenuBar.MenuItem mainMenu = menu.addItem(“Components”, null);
mainMenu.addItem(“ComponentA”, p → {
tabSheet.addTab(componentA, “ComponentA”).setClosable(true);
tabSheet.setSelectedTab(componentA);
});
mainMenu.addItem(“ComponentB”, p → {
tabSheet.addTab(componentB, “ComponentB”).setClosable(true);
tabSheet.setSelectedTab(componentB);
});
}
}
[/code]ComponentA.java (similat to ComponentB.java)[code]
@SpringUI
public class PatientUI extends VerticalLayout {
@Autowired
ComponentAService service;
@Autowired
EventBus.UIEventBus eventBus;
@PostConstruct
public void init() {
//initialize and do something with this view
}
}
[/code]Now, since ComponentA and ComponentB, needs autowired components like a service injected or the eventBus, i cannot create them with the “new” operator in the MainLayout class, something like:
mainMenu.addItem("ComponentA", p -> {
tabSheet.addTab(new ComponentA(), "ComponentA").setClosable(true);
});
or I can, if i pass the service and all the other components needed in the constructor of ComponentA. But this would mean that in the MainLayout class I have to autowire all the objects needed by all the components and then passing them in each constructor during the addTab operation. It doesn’t seem a really clean solution and also I don’t know if I’m in the right way with this, or if there is another better approach.