CDI getting the instance of a view in Platform 14


On my framework 8 CDI project I obtained the instance of a view/context using the com.vaadin.cdi.CDIViewProvider by injecting the view provider to my UI like this.

    private CDIViewProvider viewProvider;

After that you got the view instance by using viewProvider.getView("name-of-view").

I’m trying to accomplish the same thing on Platform 14. For this, the class for which I want to get the instance of is like this.

@Route(value = "country-view", layout = MainView.class)
public class CountryView extends AbstractFormLayoutView<Country> {

The documentation for Platform 14 suggests to use the BeanProvider (in Deltaspike). When I try to get it, I get a valid Bean, but the context is null. I’m doing this on my main layout after construction.

@Theme(value = Lumo.class)
public class MainLayout extends AppLayout {

    private GreetService greetService;

    private MongoDBDataConnection mongoDBConn;

    public void init() {

	 //This returns a valid Bean with the correct class
	 Bean<?> bean = CDI.current().getBeanManager().getBeans(CountryView.class).stream().findFirst().get(); 
	 //Now to get the instance of the view
	 Context cntx = CDI.current().getBeanManager().getContext(RouteScoped.class);
	 Object instance = cntx.get(bean); //This returns null 
	 Object obj = BeanProvider.getContextualReference("country-view", true); // THIS RETURNS NULL

Any ideas or alternatives that work are welcome!


I was able to solve this using

MyClass myClass = BeanProvider.getContextualReference(MyClass.class, false);

This gives you the instance of the view/route/context you are trying to access.