Java EE 7 CDI - Injection doesn't work, sending NullPointerException

醉酒当歌 提交于 2019-12-04 09:31:39
Normegil

So, I was able to fix my problem, here is what I've done :

Maven dependencies

I was assuming that javax:javaee-api was enough to use the functionnalities of Java EE 7. I've added dependencies to javax.enterprise:cdi-api, javax.inject:javax.inject and javax.annotation:javax.annotation-api

beans.xml

It's only one big project build into a war archive. I've put my beans.xml under src/main/webapp/WEB-INF (Is it necessary to have it at all with Wildfly ? Don't know)

Vaadin CDIUI

That's what solved my issue. I've tried to use Vaadin CDIUI without success. Vaadin was actually initializing my UI class with a new operation. Since it was not managed by the container, every Injection points didn't work (I've first fix it by doing new operations on my views and controllers, falling on the problem I've described)

I've refactored everything so injection is used in the views and controllers, and I've added @CDIUI to my Vaadin UI. Last, I've also added a new parameter to my servlet configuration (UIProvider) for this result :

@Theme("chameleon-green")
@Title("Data Manager")
@CDIUI("")
public class DataManagerUI extends UI {
    @WebServlet(value = "/*",
            asyncSupported = true,
            initParams = {@WebInitParam(
                    name = "session-timeout",
                    value = "60"
            ),@WebInitParam(
                    name = "UIProvider",
                    value = "com.vaadin.cdi.CDIUIProvider"
            )}
    )
    @VaadinServletConfiguration(productionMode = false,
            ui = DataManagerUI.class,
            closeIdleSessions = true
    )
    public static class Servlet extends VaadinServlet {
    }

    [...]
}

When normally instantiated with new operation from Vaadin framework, the UI class should be given by the UIProvider when using Injection.

Thanks for the help !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!