I’m switching from version 8 to 11. Looks like @SpringUI annotation is not available any more and the class that extends UI not visible to SpringBoot application. What is the equivalent to @SpringUI in version 11 ?
Thanks you
I think the correct scope in V10 and V11 is @UIScope.
Bellow, there is a tutorial about spring scopes:
https://vaadin.com/docs/v10/flow/spring/tutorial-spring-scopes.html
You don’t need the @SpringUI annotation any more, all you need is a @Route annotation.
In the full stack app “BackeryUI” example there is a property “server.servlet.context-parameters.UI=com.example.test.ui.BakeryUI”. This points to a UI class. It is used to check every request for a UserLogin and if it isn’t set, reroute to the Login page.
This is similar to the Simple App example (which uses Jetty/WAR).
I’d like to switch the Simple App example from Jetty to SpringBoot using the idea from the BackeryUI example. In that case, I need to set the server.servlet.context-parameters to my UI class.
But this class isn’t part of the SpringContext - it is “new’t” somewhere outside Spring. So I cannot inject with @Autowired a UserService. Before Vaadin Flow it was possible to use @SpringUI to get into the SpringContext. I’m not sure if UIScope helps here.
I didn’t find a solution to configure the behaviour of Vaadin (UI) within a SpringContext. I didn’t find an example.
I had the same problem and solved it by using a Servlet with a SpringVaadinServletService, in order to let Spring instantiate the view, so that it can actually autowire services.
package my.company;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.server.ServiceException;
import com.vaadin.flow.server.VaadinServlet;
import com.vaadin.flow.server.VaadinServletService;
import com.vaadin.flow.spring.SpringVaadinServletService;
public class MyServlet extends VaadinServlet {
@Override
protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException {
VaadinServletService service = new SpringVaadinServletService(this, deploymentConfiguration, ApplicationContextProvider.getApplicationContext());
service.init();
return service;
}
}
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext context;
public static ApplicationContext getApplicationContext() {
return context;
}
private static EntityManager em;
@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
context = ac;
}
public static EntityManager getEntityManager() {
if (em == null) {
EntityManagerFactory manager = ApplicationContextProvider.getApplicationContext().getBean("entityManagerFactory", EntityManagerFactory.class);
em = manager.createEntityManager();
}
return em;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>My Vaadin Application Servlet</servlet-name>
<servlet-class>my.company.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>My Vaadin Application Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>