Embed for Vaadin
Run your Vaadin applications in an embedded server
Embed for Vaadin is a library allowing you to quickly initialize Vaadin from your IDE. It provides an easy-to-use API to initialize an embed Tomcat container running a particular component or Vaadin application.
The API also allows you to open the default browser automatically and allocates an available HTTP port if none is provided. The API is extensible so that you can suit it you to your own needs.
This add-on works best when used with a build tool such as Maven as it requires Tomcat to operate. Make sure to add this dependency in scope "test" or make it optional such as these libraries do not get transmitted in your project. The add-on is available on the central repository so you don't need to configure an extra repository for it.
To use this add-on with Vaadin 6, please use:
com.bsb.common.vaadin com.bsb.common.vaadin.embed 0.5For Vaadin 7, please use:
com.bsb.common.vaadin com.bsb.common.vaadin7.embed 0.6There is also a simple archetype to quickly get you started. For more information, have look to the documentation.
Sample code
public static void main(String[] args) { EmbedVaadin.forApplication(MyApplication.class) .openBrowser(true).withContextRootDirectory("src/main/webapp").start(); }
/** * A simple {@link EmbedVaadinServer} builder that adds Spring integration. * <p/> * Assumes that the application is deployed with a web application root * directory holding a Spring <tt>ApplicationContext</tt> at the default * location. */ public class CustomEmbedVaadin extends EmbedVaadinServerBuilder<CustomEmbedVaadin, EmbedVaadinServer> { private EmbedVaadinConfig config; private final String beanName; private CustomEmbedVaadin(String beanName) { this.beanName = beanName; withConfigProperties(EmbedVaadinConfig.loadProperties()); } /** * Creates a new instance to manage a vaadin {@link com.vaadin.Application} defined by the * specified <tt>Spring</tt> bean. * <p/> * A root context directory must be specified holding the default application application * context to load, i.e. <tt>WEB-INF/applicationContext.xml</tt> * * @param beanName the name of the spring bean * @return an instance handling that application */ public static CustomEmbedVaadin forSpringBean(String beanName) { return new CustomEmbedVaadin(beanName); } @Override protected CustomEmbedVaadin self() { return this; } @Override public CustomEmbedVaadin withConfigProperties(Properties properties) { // We are happy with the default configuration but we could create our own here if needed this.config = new EmbedVaadinConfig(properties); return self(); } @Override protected EmbedVaadinConfig getConfig() { return config; } @Override public EmbedVaadinServer build() { return new EmbedVaadinWithSpring(getConfig(), beanName); } /** * An {@link EmbedVaadinServer} implementation that starts and use * Spring behind the scene. */ private static class EmbedVaadinWithSpring extends AbstractEmbedVaadinTomcat { private final String beanName; private EmbedVaadinWithSpring(EmbedVaadinConfig config, String beanName) { super(config); this.beanName = beanName; } @Override protected void configure() { initConfiguration(); // Setup vaadin servlet - the SpringAwareApplicationServlet is an extension that takes // the name of a Spring bean to initialize a new application final Wrapper wrapper = initializeVaadinServlet(new SpringAwareApplicationServlet()); wrapper.addInitParameter("applicationBean", beanName); // Make sure Spring starts - this basically add an extra listener to the webapp getContext().addApplicationListener(ContextLoaderListener.class.getName()); } } }
import com.bsb.common.vaadin.embed.support.EmbedVaadin; import com.vaadin.ui.Button; /** * Example main class launching a server and starting the browser. */ public class SampleMain { public static void main(String[] args) { EmbedVaadin.forComponent(new Button("Hello")).openBrowser(true).start(); } }
Links
Compatibility
Was this helpful? Need more help?
Leave a comment or a question below. You can also join
the chat on Discord or
ask questions on StackOverflow.
Version
#8: Initial support of Vaadin 7 #7: customization of the url to use when opening the browser #6: added simple Maven archetype
- Released
- 2012-05-07
- Maturity
- BETA
- License
- Apache License 2.0
Compatibility
- Framework
- Vaadin 6.2+
- Vaadin 7.0+
- Vaadin 6.8+ in 0.5
- Vaadin 7.1+ in 0.6
- Browser
- Browser Independent