Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Vaadin Spring UI init hell
I'm using Vaadin 7.6.4 and the spring-vaadin-portlet/0.0.5 for VaadinSpringPortlet and @VaadinPortletUI annotations from ru.xpoft.vaadin.
I keep running into situations where init(VaadinRequest request) and @PostConstruct init() essentially conflict with eachother. This is the latest one:-
public class AbstractUI {
public void init(VaadinRequest request){
request.getService().findVaadinSession(request).setAttribute("authenticatedUser",request.getUserPrincipal()));
}
}
public class UI extends AbstractUI {
public void init(VaadinRequest request){
super.init(request);// ensure all my boiler-plate security stuff is init'd
}
@PostConstruct
public void init(){
setContent(myComponentBean);
}
}
public class MyComponentBean {
@Autowired
SomeService someService; // We need this to be instantiated so wait till post-construct to use it.
@PostConstruct
public void init(){
// userSetting needs to be set if not set per-session, provide some UI to do that if not found!
UserSetting userSetting = UI.getCurrent().getSession().getAttribute("someUserSetting");
if (user == null){
// load user login UI
}else{
User user = UI.getCurrent().getSession().getAttribute("authenticatedUser"); /[b]/NPE here as UI.getCurrent() == null [/b]
userSetting = someService.getUserSetttingBasedOnUser(user);
UI.getCurrent().getSession().setAttribute("someUserSetting",userSettting);
}
}
Now I hear you saying already "just reverse it" and have the UI components attached in the init(VaadinRequest) method, but that gives me NPEs when attempting to access my @Autowired properties. I think that is likely the crux of my issue, and I need to investiage that more (some caught exceptions preventing init of lower-order components perhaps).
How is everyone else doing this? I'm avoiding just slamming in spring-boot / custom vaddin portlet / @Configuration based context for now.
Hi,
You need to ensure the UI has been instanciated before calling UI.getCurrent() at any point in your code. One way of doing that is by overriding the attach method in your MyComponentBean class (which in your snippet is not extending a Vaadin component but I'm sure it is in your real code).
As an alternative, you could try to lazyly initialize MyComponentBean.
Also, in case you are using defaults, making the beans prototype-scoped may be a good idea, but of course it depends on your requirements.