<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.
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
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.
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 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