Vaadin7 + CDI

Hi guys,

is there already a way to use injection with vaadin 7?
I used this guide to do the CDI stuff with Vaadin 6.x
https://vaadin.com/wiki/-/wiki/Main/Creating%20JEE6%20Vaadin%20Applications
but now the entiy point is Root and not Application so there is no way to copy this guide to Vaadin 7 - any ideas?

Could I bump this question? Do all JEE6 CDI specs work in Vaadin? (Obviously only if Vaadin is hosted in a jee6 spec container.)

See:
JSR-299 Spec

HI,

there are two projects that help you with CDI:
The great cdi-utils Addon https://vaadin.com/directory#addon/cdi-utils which shows you how to use CDI in Vaadin 6. Only Root Application acces has changed for Vaadin 7, main design will stay the same.

I have started with a CDI + Vaadin 7 based application framework which is just started and no official addon yet. You can find it here:
https://github.com/thomasletsch/javaee-addon
With a sample app here:
https://github.com/thomasletsch/javaee-addon-sample

In this application I use CDI with events, JPA 2. Tomi uses in his cdi-utils also custom scopes and so on. Since you program inside your container, everything will work until you start to work inside widgets.

Regards,
Thomas

You can find the default setup for V7 apps for CDI (as for V6) over
here
.
In the RootApplication you can define all of you beans.

The only change thereafter is then to change your Root to pick up the beans by using getApplication().get()

B)

? I don’t get it - sorry!

Assuming that this is the basic we have:

public class MyApplicationRoot extends Root {

    public static class RootApplication extends Application {
        private static final long serialVersionUID = -1099516579868329607L;
        
        @Override
        protected Root getRoot(WrappedRequest request) throws RootRequiresMoreInformationException {
            return new MyApplicationRoot();
        }
    }
    
    @WebServlet(urlPatterns="/*")
    public static class Servlet extends AbstractApplicationServlet {
        private static final long serialVersionUID = -1386739418720898885L;

        @Override
        protected Application getNewApplication(HttpServletRequest request) throws ServletException {
            return new RootApplication();
        }

        @Override
        protected Class<? extends Application> getApplicationClass() throws ClassNotFoundException {
            return RootApplication.class;
        }
    }
    
    private static final long serialVersionUID = 7428504147931595620L;

    @Override
    protected void init(WrappedRequest request) {
        VerticalLayout view = new VerticalLayout();
        view.addComponent(new Label("Hello Vaadin!"));
        setContent(view);
    }
}

how do we have to modify this code to enable CDI for at least the MyApplicationRoot class?

I just started a blog about Vaadin 7 and CDI (Guice here). Maybe this helps.

http://codeturm.blogspot.de/2012/08/eclipse-gae-guice-and-vaadin-7-part-one.html
and
http://codeturm.blogspot.de/2012/08/eclipse-gae-guice-and-vaadin-7-part-two.html

Have fun,
Christian

Also would like to note Adam Bien’s Weblog about CDI and Vaadin.


http://www.adam-bien.com/roller/abien/entry/vaadin_java_ee_no_xml

Something similar should work with Vaadin 7 too .

I did this with Vaadin 6.8 and it works like a charm…

Hi there,

I tried to include Vaadin 7 beta 5 in a Multi-Module Maven Application with a war, jar and ear that will be deployed to JBoss AS 7.1.1.

So i tried following Code, but somehow CDI is not working.
The Variables that have @Inject are Null and i get a NullPointer Exception.

I have following FileStructure:

vaadin-war

  • src/main/resources/WEB-INF
    • beans.xml
    • web.xml

and in the app-jar

  • src/main/resources
    - beans.xml
    - persistence.xml (Hibernate)

Got follwongin Contents:

beans.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" 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/beans_1_0.xsd">
</beans>

UIProvider


public class CdiUiProvider extends DefaultUIProvider implements Serializable{
	
	private static final long serialVersionUID = 8043964411848504777L;

	@Inject
	private VaadinUI vaadinUI;

	@Override
	public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {
		return VaadinUI.class;
	}

	@Override
	public UI createInstance(UICreateEvent event) {
		return vaadinUI;
	}

}

My own CdiVaadinServlet


public class CdiVaadinServlet extends VaadinServlet{

	private static final long serialVersionUID = -8833005846998449164L;
	
	@Inject
	private Instance<CdiUiProvider> applicationProvider;
	
	@SuppressWarnings("serial")
	private final SessionInitListener sessionInitListener = new SessionInitListener() {
		
		@Override
		public void sessionInit(SessionInitEvent event) throws ServiceException {
			
			@SuppressWarnings("unused")
			VaadinService service = event.getService();
            final VaadinServiceSession session = event.getSession();
            session.addUIProvider(applicationProvider.get());
			
		}
	};
	
	public void init(ServletConfig servletConfig) throws ServletException {
		super.init(servletConfig);
		getService().addSessionInitListener(sessionInitListener);
	}

}

The View


@Named
@ApplicationScoped
public class VaadinUI extends UI implements ClickListener {
	
	private static final long serialVersionUID = 1L;
	
	@Inject
	private ShopUI shopUI;
	
	@Override
	protected void init(VaadinRequest request) {
		buildMainLayout();
	}
	
	private void buildMainLayout() {
		getPage().setTitle("Vaadin UI");

		layout = new VerticalLayout();
		layout.setSizeFull();

		setComponent(shopUI);

		setContent(layout);
	}
}

problem is that shopUI is null and therefore nothing is set.
And also when i set a breakPoint in the Servlet nothing happens there so still think i ueses the Default-VaadinServlet instead of mine.

here is my web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>Vaadin Web Application</display-name>
	<context-param>
		<description>Vaadin production mode</description>
		<param-name>productionMode</param-name>
		<param-value>false</param-value>
	</context-param>

	<servlet>
		<servlet-name>Vaadin CDI Servlet</servlet-name>
		<servlet-class>com.swissAs.htmlEval.vaadin.cdi.CdiVaadinServlet</servlet-class>
		<init-param>
			<description>Vaadin UI to display</description>
			<param-name>UI</param-name>
			<param-value>com.swissAs.htmlEval.vaadin.VaadinUI</param-value>
		</init-param>
	</servlet>
</web-app>

anybody an Idea what goes wrong?
I really would like to use jee6 as the backend, but that is useless if i can not inject the backend with the logic

Thanks

I also tried this GitHubProject without Luck
https://github.com/vaadin/vaadin-cdi-integration

anybody knows on how i have to config this one to get it wrork?

Sorry bit clueless here

found my Problem.

the Project had to web.xml, one under resources and one in webapp. deleted the resources one and configured the one in webapp. within the same war it works now.
only Injections to other jars are not working yet.

Hi Marc,

at the first glance your solution seems to work. But when I pressed “reload” in my browser and got an exception.

javax.servlet.ServletException: java.lang.IllegalStateException: UI id has already been defined
	com.vaadin.server.VaadinServlet.handleServiceException(VaadinServlet.java:592)
	com.vaadin.server.VaadinServlet.service(VaadinServlet.java:357)
	com.vaadin.server.VaadinServlet.service(VaadinServlet.java:215)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

I am not sure whether it is correct to annotate the VaadinUI class as @ApplicationScoped, since Book of Vaadin states

“A Vaadin application is essentially a user session, and a session is created for each user who uses the application. In the context of our HelloWorld application, it is sufficient to know that the underlying session is created when the user first accesses the application by opening the page, and init() method is invoked at that time.”

I would assume @SessionScoped is more appropriate here but I get the same Exception when using it…

I think @SessionScoped and @ApplicationScoped in vaadin 7 is more or less the same thing (Not identical, but by default they should resemble the same scope.
You need a custom scope for your VaadinUI implementation since you can have multiple VaadinUI instances ( multiple tabs or windows ) for the same session and application (depending on your application design).

In cdi-utils addon Tomi uses @VaadinScoped( VaadinScope.WINDOW) to tie the component to the current request via @RequestScoped , you will probably need the same approach for VaadinUI since VaadinUI is more synonymous with Window in vaadin 6.x from my understanding.

The UI’s should be annotated with @VaadinUI which is a scope in itself, and if you only have one UI in it, then annotate that want also with @Root.

Clarification. In Vaadin 6 anyways when an Application closes Vaadin does not invalidate the Session. I think they added a policy in Vaadin 7 to close the session (since feature request was closed: http://dev.vaadin.com/ticket/6093) but I haven’t verified it.

In Vaadin 6 the same session is used if a new Application is created in the same window so you need to be careful about what is bound to the session scope.

Building snapshot … fails with selenium

Hi

I cloned the project from github and start “mvn clean install”. Selenium tests are integrated, firefox starts and the build fails. Is there a way to skip selenium for local builds? Is there an official release?

Jan

The official release is coming a little later (currently plan is beta in March, final in April) - there will be some changes, most likely including a better UI scope that allows injecting components into injected components etc.

You should be able to skip tests the usual way - if I remember correctly:

-DskipTests to skip running unit tests or -Dmaven.test.skip=true to also skip compiling them, -DskipITs to skip integration tests. There are similar properties (with somewhat different names) to the failsafe plug-in if you want to configure these in your POM.

Just in case anyone is still struggling with Guice and Vaadin, David Sowerby and myself have created an addon which hopefully should make it easier.

https://vaadin.com/directory#addon/v-guice

CDI with Java EE 7 always throws NPE. can you please provide us a full working solution or a video on how we can configure that.

Don’t know where you’re doing your injections and having the NPE, but I’m using Vaadin 7.4 alpha with Java EE 7 and CDI is working just fine. You might want to elaborate. Also you can simply use Adam Bien’s POM in this simple layout and build on it. It works just fine https://github.com/AdamBien/vaadin-with-javaee-pom/

Just clone and use as template for Vaadin 7 with Java EE 7 as backend

I’m doing the injection of the EJB in the MyVaadin class which extends UI and i have also added the CDI adds-on. I don’t know what i 'm doing wrong here. If possible can you explain me in simple steps what you did in configuration in order to achieve that. I’m using NetBeans 8 and Vaadin plugin. If possible show some configuration steps you took to succed.