Webinar: Java web apps with Spring and Vaadin

What is Spring? How can dependency injection help you to build better UI code? How does Spring Boot change the way you develop Java web apps? This and much more is answered in this Spring + Vaadin webinar, where Stéphane Nicoll (Pivotal) and Matti Tahvonen (Vaadin) show you how to build a modern Spring Boot application, that connects to a traditional database using Spring Data JPA and shows a nice single-page UI using Vaadin.

Webinar takes place on Tuesday October 18, 2016 @ 2PM CEST

embedyoutube=6FjjVKmEdY4

See
https://vaadin.com/webinars
for other past and upcoming webinars.

You can post your questions below, thank you!

Hi, how can we launch an application built in HTML5 and JS inside our vaadin server application (js audio recorder for example) ?

Where can we get the code you are working with?

Thank you so much.

What about the Spring security and it’s support with vaadin Spring (SpringUiProvider and SpringView) ??

How we can configure multipe database connection and how to handle them automatically on backend service

How we can implement MVP architecture with Spring

Can we have link to examples

Hi, I think this is not that much Spring related actually. Practically you should just create a “wrapper” for your web component, a Vaadin add-on with client side extension. The
documentation site
and wiki has examples on how to get started. I suggest to use my
new enhanced add-on archetype
and share the output in
Vaadin Directory
, for others who might have similar needs. This is a good way to give somethign back for our awesome OS community.

I added lots of links to the main post to the top of the thread, let me know if I forgot something!

See the
Vaadin4Spring
project, it contains an example and helpers.


Here is one example with multiple databasis

When I use Autowire, in one Window or view, how can I reset the window or view, for sample:

I have one view to show my person details, So today in my constructor I receive the Id from my person, return the details from my Db, and then bind to my form.

But when I use springboot I cant use the constructor, so how can I call the method to get my details from db, every time my view show in screen, or if I put this view into a window, how can I execute this method every time I show my window? using Springboot

tks

Hi Fabio,

Don’t know exatly if I got your question right, but indeeed you can’t use new keyword and in values as constructurs, but you can of course use normal methods to pass parameters to beans. See the crud example, there we have the PersonForm and its setEntity method form AbstractForm superclass. The PersonForm is technically not a Window, but the principle is exactly the same if you would be extending form Window class.

Depending on the situation you can also use e.g. PostConstruct annotation and an initialization method (and other wired in beans) or use events to load data to your UI components.

I hope this helps and I really suggest to watch out
the dependency injection webinar
we just held with Petter Holmström or going through some basic Spring Core tutorial/book. DI is not easy in the beginning, but will be a powerful tool once you get the basic concepts.

cheers,
matti

Hello there,

Great show!

I have a question regarding live reloading.
So I added devtools to my vaadin/spring boot application and I installed LiveReload plugin.
I develop my application with IntelliJ.
However, when I change something in my vaadin view class (or any other class) and do Build → Compile class (or Make Project) my whole application is stopped and reloaded. This of course takes time and since I use spring security I need to login…

Do you know what should be the problem?

Cheers,
Milos

How to load balance spring-boot-vaadin application ? Is there any example?

Hi Milos, the devtools indeed toes a “restart”, it is not a replacement for JRebel, but it most cases the restat should be fast enough as the JVM caches and dependency jar files are maintained (JVM itself don’t restart).

Based on discussion with Stephane, devtools should also configure tomcat so that it persists the session. If your session persists properly (and login is stored there, not in UI scope), new login shouldn’t be needed. My first guess is that there is something in your session that doesn’t serialize → new login is needed.

But, I guess the restart is still a lot faster than cold start, especially if you are using Hibernate (JPA).

See this documentation for more information about devtools:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools-restart

I don’t know of any existing examples, but this pretty much works as with plain old servlet containers. Any front proxy should do fine. I have a plan to do full JVM example of clustering setup, by using undertow as load balancer.

Matti, my problem is that in my work, we have one designer that create all views on designer, so I dont have a lot of facility that viritrin brings to me, and one problem is: I have one view and need to open in a window, so I run this code:

Window window = new Window(titulo); window.setCaption(titulo); window.setWidth(largura, Sizeable.Unit.PERCENTAGE); window.setHeight(altura, Sizeable.Unit.PERCENTAGE); window.setModal(true); window.setContent(new MyView(someProp); So I creat one new MyViw everty time I call this method, but with Springboot my MyView will be create when my app start, so I need to change everery thing in my constructor to my @PostConstructor.

But so, every time that I need to show this view, I will need to run ?

myView.someMethod(someProp);
window.setContent(myView)

and exists one method that are called everty time any view (extends VerticalLayout or any other layouts) appears in my screen? When extends View I have the enter method, but in my sample have one method that I can use to clear my view for example?

tks

Yes, that is a current limitation that you cannot use Window as a root for you designs. But if you often need to open you designs in a new window, I’d do a similar “open in a window” helper as I have done for the form in Viritin. You can do that directly to a class extending your design or probably even to a generic helper class.

cheers,
matti

Matti, I try to use EventBus in my app, when I in develope mode, works fine, but when I build one .war and input in tomcat8, my eventBus are not autowired, and I got this error:

Nov 08, 2016 10:03:53 AM com.vaadin.server.DefaultErrorHandler doDefault
SEVERE: 
java.lang.NullPointerException
    at br.com.lumera.cartoriointeligente.CartorioUI.init(CartorioUI.java:83)
    at com.vaadin.ui.UI.doInit(UI.java:684)
    at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:214)
    at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1422)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:379)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

in this line:

@Autowired
private EventBus.UIEventBus eventBus;
@Override protected void init(VaadinRequest vaadinRequest) {
  eventBus.subscribe(this);
  setContent(login);
}

this is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <groupId>br.com.lumera</groupId>
    <artifactId>cartoriointeligente</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>cartoriointeligente</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <version.vaadin>7.7.3</version.vaadin>
        <start-class>br.com.lumera.cartoriointeligente.CartorioInteligenteApplication</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.vaadin</groupId>
            <artifactId>viritin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.vaadin.teemu</groupId>
            <artifactId>switch</artifactId>
        </dependency>

        <!-- MongoDB -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

        <!-- Jackson -->
               <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson2-provider</artifactId>
            <version>3.0.19.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>3.0.19.Final</version>
        </dependency>

        <!--  Util -->
        <dependency>
            <groupId>org.vaadin.addons</groupId>
            <artifactId>vaadin-grid-util</artifactId>
            <version>1.0.8</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        <dependency>
            <groupId>br.com.caelum.stella</groupId>
            <artifactId>caelum-stella-core</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>br.com.caelum.stella</groupId>
            <artifactId>caelum-stella-bean-validation</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.2.0.Final</version>
        </dependency>

        <!-- Addon -->
        <dependency>
            <groupId>org.vaadin.addons</groupId>
            <artifactId>brtextfield</artifactId>
            <version>1.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.vaadin.addons</groupId>
            <artifactId>masketfield</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>de.steinwedel.vaadin.addon</groupId>
            <artifactId>messagebox</artifactId>
            <version>3.0.19</version>
        </dependency>
        <dependency>
            <groupId>eu.maxschuster</groupId>
            <artifactId>vaadin-autocompletetextfield</artifactId>
            <version>1.0-alpha-4</version>
        </dependency>
        <dependency>
            <groupId>org.vaadin.teemusa</groupId>
            <artifactId>sidemenu</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.vaadin.spring.addons</groupId>
            <artifactId>vaadin-spring-addon-eventbus</artifactId>
            <version>LATEST</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.vaadin.spring.addons</groupId>
            <artifactId>vaadin-spring-addon-sidebar</artifactId>
            <version>LATEST</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-bom</artifactId>
                <version>${version.vaadin}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-spring-boot-starter</artifactId>
                <version>1.0.2</version>
            </dependency>
            <dependency>
                <groupId>org.vaadin.spring.extensions</groupId>
                <artifactId>vaadin-spring-ext-boot</artifactId>
                <version>0.0.7.RELEASE</version>
            </dependency>
        <dependency>
                <groupId>org.vaadin</groupId>
                <artifactId>viritin</artifactId>
                <version>1.57</version>
            </dependency>
            <dependency>
                <groupId>org.vaadin.teemu</groupId>
                <artifactId>switch</artifactId>
                <version>2.0.2</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- Using client side Vaadin add-ons (Switch in this example)
                 require a custom built "widgetset". Vaadin maven plugin does
                 that automatically, here using a handy cloud service with CDN
                 hosting.
            -->
            <plugin>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-maven-plugin</artifactId>
                <version>${version.vaadin}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>update-widgetset</goal>
                            <goal>compile</goal>
                            <goal>compile-theme</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <widgetsetMode>cdn</widgetsetMode>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.6.1</version>
                <!-- Clean up also any pre-compiled themes -->
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>src/main/webapp/VAADIN/themes</directory>
                            <includes>
                                <include>**/styles.css</include>
                                <include>**/styles.scss.cache</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>vaadin-addons</id>
            <url>http://maven.vaadin.com/vaadin-addons</url>
        </repository>
    </repositories>

</project>