Vaadin 14 with spring core, not spring boot

Hi,

I try to user @Autowired but the Vaadin classes can’t have access to spring context. So always get a null value.

Tx.

In vaadin you can only use autowire in [route targets]
(https://vaadin.com/docs/v14/flow/routing/tutorial-routing-annotation.html), [router layouts]
(https://vaadin.com/docs/v14/flow/routing/tutorial-router-layout.html), and [exception targets]
(https://vaadin.com/docs/v14/flow/routing/tutorial-routing-exception-handling.html). From there, you can pass the autowired instance down to any subcomponent that needs it.

You can remember better by knowing that whenever you create an instance using the new keyword (–> new MyFooGrid()), any dependency injection within that instance will not work.

More about CDI with Vaadin can be found [here]
(https://vaadin.com/docs/v14/flow/cdi/tutorial-cdi-instantiated-beans.html)

Kaspar Scherrer:
In vaadin you can only use autowire in [route targets]
(https://vaadin.com/docs/v14/flow/routing/tutorial-routing-annotation.html), [router layouts]
(https://vaadin.com/docs/v14/flow/routing/tutorial-router-layout.html), and [exception targets]
(https://vaadin.com/docs/v14/flow/routing/tutorial-routing-exception-handling.html). From there, you can pass the autowired instance down to any subcomponent that needs it.

You can remember better by knowing that whenever you create an instance using the new keyword (–> new MyFooGrid()), any dependency injection within that instance will not work.

More about CDI with Vaadin can be found [here]
(https://vaadin.com/docs/v14/flow/cdi/tutorial-cdi-instantiated-beans.html)

Tx Kaspar,

I found a workaround to access spring-context from Vaadin (@Route).

We need to create a new ApplicationContextAware implementation ej:

@Component
public class ApplicationContextProvider implements ApplicationContextAware {

    private static ApplicationContext context;

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
}

Then we can use form any Vaadin class (@Route):

ApplicationContextProvider.getApplicationContext().getBean(xxx.class);