Mateusz
Mateusz

Reputation: 1197

autowired vaadin component gets index -1

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?

Upvotes: 0

Views: 138

Answers (1)

Morfic
Morfic

Reputation: 15498

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.

Upvotes: 1

Related Questions