0

I use vaadin-spring. I created public abstract class AbstractBasicVerticalLayoutWithMenu extends HorizontalLayout implements View and I have two normal classes extending it. In abstract class I @Autowire Menu component defined:

@UIScope @SpringComponent public class Menu extends CssLayout

both classes are annotated

@UIScope @SpringView

I have very strange issue in one of views, that my menu component is not being displayed.

I did some digging and added logging. On

@Override public void enter(ViewChangeEvent event)

method I added logging and tried many thigs and I found that when I do this.getComponentIndex(menu) that wrong view component has index -1!

Interesting thing is that if I remove correctly displayed view from navigator (i do it: getNavigator().addView(FirstTesterView.VIEW_NAME, firstTesterView);) then this incorrect view is back to correct.

I have no idea why, but it looks like only first of instantiated classes extending acstract gets @Autowired component but not next.

What do I do wrong?

Mateusz
  • 1,149
  • 1
  • 16
  • 33
  • I suspect that you menu is a [singleton](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes-singleton) which is the default scope in spring. At the same time, a Vaadin component can have only one parent at any time, eg you can not include the same menu instance in 2 different views. So, just make it a [prototype](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes-prototype) so you get a new instance each time. Otherwise please post more code or a [sscce](http://sscce.org) if possible – Morfic May 17 '17 at 06:37
  • You are completely right. Please make answer from this comment to let me approve it! – Mateusz May 17 '17 at 23:16

1 Answers1

1

Index -1 means that the component was not found among the children.

Now, if your component is 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 at any given time, so probably you've missed some IllegalStateException in the log.

To fix this, you can set the scope of your menu to prototype so you get a new instance each time.

Morfic
  • 15,178
  • 3
  • 51
  • 61