Step 3 - Running a Spring Boot application with MPR and Flow
- Updating to the correct Spring version
- Handling of SpringUI
- Update imports
- What to do with SpringView
- Things to keep in mind
- Next step
Note
| This step is needed in case your Vaadin 7 or 8 application uses Spring Boot. If it isn’t the case, go back to the framework selection. |
Updating to the correct Spring version
Update parent org.springframework.boot:spring-boot-starter-parent
to 2.1.7.RELEASE
or newer.
The dependency com.vaadin:vaadin-spring-boot-starter
shouldn’t have a version defined as it comes from vaadin-bom
.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>
Note
| Also take a look at the Using Vaadin with Spring Boot tutorial on how Flow integrates with Spring. |
Handling of SpringUI
The @SpringUI
can be replaced with a @Route
. For example this UI:
@SpringUI
@Theme("valo")
public class TodoUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
setContent(new HorizontalLayout());
}
}
Can be replaced with:
@Route("")
public class TodoUI extends Div implements HasLegacyComponents {
@PostConstruct
private void buildLayouts() {
setSizeFull();
add(new HorizontalLayout());
}
}
Note
|
Annotations in the UI, such as @Theme and @Title and so on, are dealt with later on in the tutorial.
Most of them have similar counterpart in either Flow or MPR.
|
Update imports
Then any com.vaadin.spring.annotation
imports needs to be changed to com.vaadin.flow.spring.annotation
.
Note
|
The V14 Spring add-on doesn’t have a feature comparable with ViewScope
|
What to do with SpringView
Any @SpringView
should be updated to a Flow Route by wrapping them as a MprRouteAdapter<? extends View>
or re-writing it to be a Flow Component. See Upgrading Views to Flow Routes for details.
The easiest way to migrate a Spring view is to wrap it in a component that extends MprRouteAdapter<? extends View>
and then define a navigation target for it with @Route
.
No other annotation for the wrapper component or the wrapped view is needed.
Starting from Vaadin 21, @RouteScope
without @RouteScopeOwner
annotation can be used as a replacement for @ViewScope
.
The bean within @RouteScope
(without specified @RouteScopeOwner
) stays preserved until the current navigation target/view is active (attached).
It’s possible to use @RouteScopeOwner
explicitly, but that requires an extra line.
The following is an example of using @RouteScope
:
@Route("help")
public class HelpRoute extends MprRouteAdapter<HelpView> {
}
public class HelpView extends VerticalLayout implements View {
@Autowired
private ApplicationContext context;
@Override
public void enter(ViewChangeEvent event) {
HelpService service = context.getBean(HelpService.class);
// every time when {@code context.getBean(HelpService.class)} called
// the HelpService instance is the same until we're inside HelpView/HelpRoute
Label label = new Label(service.getHelp());
addComponent(label);
}
}
@RouteScope
public class HelpService {
public String getHelp(){
return "some help";
}
}
Things to keep in mind
-
When porting the UI to a flow component, you lose the ability to use UI methods, such as
setErrorHandler
. You can still access those by usingUI.getCurrent()
. The methodsetContent
isn’t supported though - you should use theadd
method from your Flow layout instead. -
When running MPR with Spring, the Spring integration is done with Flow (and not anymore with Vaadin 7 or 8), so sometimes you need to import classes from the old
vaadin-spring
project to make your MPR project to compile, since those classes aren’t present anymore in the new versions ofvaadin-spring
. The source code ofvaadin-spring
can be found on GitHub. Examples of such classes:-
com.vaadin.spring.access.SecuredViewAccessControl
; -
com.vaadin.spring.access.ViewAccessControl
; -
com.vaadin.spring.internal.SpringBeanUtil
; -
com.vaadin.spring.internal.VaadinSpringComponentFactory
; -
com.vaadin.spring.server.SpringVaadinServletService
;
-
-
If your routes are defined in a different package than the Spring application itself, you need to annotate your application with
@EnableVaadin
, to Spring to scan the appropriate folders for beans. For example:
// Assuming that Application is in a different package than the classes
// annotated with @Route
@SpringBootApplication
@EnableVaadin("com.mycompany.views")
public class Application extends SpringBootServletInitializer {