Book Image

Vaadin 7 Cookbook

Book Image

Vaadin 7 Cookbook

Overview of this book

Table of Contents (19 chapters)
Vaadin 7 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Vaadin and Spring injector


We will make a simple application that shows the username on the screen. Our goal in the recipe is to auto inject or maybe (in better terms) autowire Spring beans in the Vaadin application.

Getting ready

Create new Maven project from the Vaadin archetype.

mvn archetype:generate \
 -DarchetypeGroupId=com.vaadin \
 -DarchetypeArtifactId=vaadin-archetype-application \
 -DarchetypeVersion=LATEST \
 -Dpackaging=war \
 -DgroupId=com.packtpub.vaadin \
 -DartifactId=vaadin-spring-injector \
-Dversion=1.0

How to do it...

Carry out the following steps, in order to make a Vaadin application with autowiring via injector:

  1. First, we create a class representing a user.

    public class User {
      private String name;
      // generate getters and setters for name field
    }
  2. Create a service that returns a new user always when the getUser() method is called.

    public class UserService {
    
      public User getUser() {
        // TODO: here we could fetch data from e.g. database
        User user = new User();
     ...