Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Vaadin CDI and Tomcat 8
Hi folks,
can someone please give me all ivy dependencies to get this example working under Tomcat 8? https://github.com/peterl1084/cdiexample.
It's working under TomEE with these dependencies:
<dependency org="javax" name="javaee-web-api" rev="7.0"/>
<dependency org="org.apache.shiro" name="shiro-core" rev="1.2.3" />
<dependency org="org.apache.shiro" name="shiro-web" rev="1.2.3" />
<dependency org="org.apache.deltaspike.core" name="deltaspike-core-api" rev="1.3.0" />
<dependency org="org.apache.deltaspike.core" name="deltaspike-core-impl" rev="1.3.0" />
<dependency org="org.eclipse.persistence" name="eclipselink" rev="2.6.0" conf="default"/>
<dependency org="mysql" name="mysql-connector-java" rev="5.1.35" />
<dependency org="org.vaadin" name="cdi-helpers" rev="1.9" />
For Tomcat 8 i added the following dependencies:
<dependency org="com.sun.faces" name="jsf-api" rev="2.2.11"/>
<dependency org="com.sun.faces" name="jsf-impl" rev="2.2.11"/>
<dependency org="org.apache.openejb" name="openejb-core" rev="4.7.1"/>
But i get these Exceptions while starting:
java.lang.NullPointerException
at com.example.vaadincdi.backend.authentication.CDIAwareShiroEnvironmentLoader.createEnvironment(CDIAwareShiroEnvironmentLoader.java:32)
at org.apache.shiro.web.env.EnvironmentLoader.initEnvironment(EnvironmentLoader.java:133)
at org.apache.shiro.web.env.EnvironmentLoaderListener.contextInitialized(EnvironmentLoaderListener.java:58)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
java.lang.NullPointerException
at com.vaadin.cdi.internal.AnnotationUtil.getUiBeans(AnnotationUtil.java:80)
at com.vaadin.cdi.internal.ContextDeployer.discoverUIMappingsFromAnnotations(ContextDeployer.java:113)
at com.vaadin.cdi.internal.ContextDeployer.contextInitialized(ContextDeployer.java:63)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
For the second Exception i found this ticket: https://dev.vaadin.com/ticket/12388
But it is marked as fixed, so it must be something else i guess.
Thank you
Felix
I also tried to remove the Shiro Listener and Filter from the web.xml but the second Exception still occurs.
To make it simpler, i tried a new fresh Vaadin project and tried to get Vaadin CDI working. I added the following dependencies:
<dependency org="com.vaadin" name="vaadin-cdi" rev="1.0.2" />
<dependency org="javax" name="javaee-api" rev="7.0"/>
<dependency org="javax.enterprise" name="cdi-api" rev="1.2"/>
<dependency org="org.apache.openwebbeans" name="openwebbeans-impl" rev="1.5.0"/>
<dependency org="org.apache.openejb" name="openejb-core" rev="4.7.1"/>
I still get the same NPE:
java.lang.NullPointerException
at com.vaadin.cdi.internal.AnnotationUtil.getUiBeans(AnnotationUtil.java:80)
at com.vaadin.cdi.internal.ContextDeployer.discoverUIMappingsFromAnnotations(ContextDeployer.java:113)
at com.vaadin.cdi.internal.ContextDeployer.contextInitialized(ContextDeployer.java:63)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
There must be someone out there who managed to get CDI working under Tomcat and can tell me what i'm missing ;)
Maybe this is also important:
Exception sending context initialized event to listener instance of class com.vaadin.cdi.internal.ContextDeployer
It stands prior to the exception.
Can someone tell me please how do i check if CDI works in general?
Ok i found out, that with OpenWebBeans, CDI is not working at all in Tomcat for me.
But with WELD CDI is working.
Anyway, now ContextDeployer is working, but later the JNDI lookup of BeanManager in CDIUIProvider is not working:
SEVERE: Could not get BeanManager through JNDI
SEVERE:
java.lang.NullPointerException
at com.vaadin.cdi.internal.AnnotationUtil.getUiBeans(AnnotationUtil.java:80)
at com.vaadin.cdi.internal.AnnotationUtil.getRootUiBeans(AnnotationUtil.java:52)
at com.vaadin.cdi.CDIUIProvider.rootUI(CDIUIProvider.java:137)
at com.vaadin.cdi.CDIUIProvider.getUIClass(CDIUIProvider.java:109)
at com.vaadin.server.BootstrapHandler.synchronizedHandleRequest(BootstrapHandler.java:245)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1408)
at com.vaadin.cdi.server.VaadinCDIServletService.handleRequest(VaadinCDIServletService.java:92)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:350)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
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:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
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)
To confirm, that CDI is working, i created a simple test HttpServlet and some test bean and injected the test bean into the servlet. This works.
But really weird is, that if i try to do the same in an UI, injecting the test bean does not work inside the UI...
I'm really confused right now and any help would be highly appreciated as i am at the end of my knowlegde...
Since you are using Tomcat8, not a full pledged jee server, you must add a dependency for weld-servlet. This artifact is a reference cdi implementation for servlet containers like tomcat8. This will make cdi work. In tomcat8.
Then in your web.xml put:
<resource-env-ref>
<!-- Enable Weld CDI, also needs META-INF/context.xml entry -->
<resource-env-ref-name>BeanManager</resource-env-ref-name>
<resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>
In the context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/tomcat-test">
<Resource
name="BeanManager"
auth="Container"
type="javax.enterprise.inject.spi.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory"
/>
</Context>
As i wrote, WELD CDI is working, just not inside an UI...
This works:
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Inject
Test test;
public TestServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(test); <- Will not output null, hence CDI is working
}
}
This is not working:
public class TestUI extends UI {
@Inject
Test test;
@Override
protected void init(VaadinRequest request) {
System.out.println(test); <- Will output null
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
}
@Override
public void beforeClientResponse(boolean initial) {
System.out.println(test); <- Will output null
super.beforeClientResponse(initial);
}
}
I am not using Vaadin-CDI in this case, i just want to use CDI inside a Vaadin app and i don't understand why it is working in the HttpServlet but not in the UI...
EDIT:
It is not necessary to add the stuff you wrote in web.xml and context.xml, i have already tried that before.
Well this is because of the framework. Vaadim servlet uses a ui provider class. The ui provider class gives a new ui instance. That is the reason injection does not work on your ui because it is not injected and is not in the weld container. The ui provider must be managed in the weld container as well. You can check out the codes of vaadin cdi addon for references. Hope this helps.
I understand, thank you!
I found the problem why the JNDI lookup of BeanManager failed...i had just a stupid typo in my context.xml >,< I can't explain how it happened because i copypasted the lines, but anyway im glad i solved the problem :)
It was not necessary to add the lines you wrote to get CDI in my test servlet working, but for the JNDI lookup done by CDIUIProvider it is necessary.
So thank you very much, your post made me check my web and context.xml again and i found the typo :)
EDIT:
it is only necessary to add the resource in context.xml, it seems not to be necessary to add the resource ref in the web.xml
Hi,
Do you have a specific reason to use plain Tomcat instead of a real Java EE Server (like TomEE, Wildfly, GlassFish)?
__
m