0

I wrote a menubar as a custom layout in vaadin. It has a logout button which is autowired to custom a Session variable. The autowired variable can only be used after construction, so I added an init method annotated with PostConstruct:

public class Menu extends HorizontalLayout{
    private Button logout = new Button("Logout");

    @Autowired
    private Session session;

    @PostConstruct
    private void init(){
        logout.addClickListener((event) -> {
            session.unsetUser();
            Notification.show("Good bye");

            getUI().getNavigator().navigateTo(LoginView.NAME);
        });
        addComponent(logout);
    }

}

Since it is not a SpringBean, the PostConstruct will never run. How do I overcome this?

godzsa
  • 2,105
  • 4
  • 34
  • 56
  • 2
    You can either make your menu a spring bean, or [inject the required beans in a spring non managed object](http://stackoverflow.com/questions/310271/injecting-beans-into-a-class-outside-the-spring-managed-context) – Morfic Jul 20 '16 at 21:50
  • 1
    Btw. I recommend deriving from `CustomComponent` instead of `HorizontalLayout` because you don't want layout functionality do be visible to users of your component `Menu`. – Steffen Harbich Jul 21 '16 at 07:17
  • make `public void init()` not `private` – Nebras Aug 31 '16 at 09:46

0 Answers0