Hi,
when trying out CDI dependency injection with Glassfish 3.1.1, I ran into
some problems with constructor injection. Here's one:
These are my classes (see code below):
* InjectedServlet - a servlet that uses an injected EJB
* GreeterEjb - an EJB with constructor injection
* Greeter - a very simple example class
When I call the servlet, I get an Exception
"org.jboss.weld.exceptions.WeldException: WELD-000033 Could not instantiate
client proxy for Session bean [class
com.example.ejbconstructorinjection.GreeterEjb with qualifiers [@Any
@Default]; local interfaces are [GreeterEjb]" (see stacktrace below).
* If GreeterEjb uses field injection or method injection instead of
constructor injection, it works fine.
* If I add an additional no-argument constructor to GreeterEjb, it works
fine. I don't think the additional no-argument constructor is actually
used to instantiate the GreeterEjb, because when the servlet calls it, the
dependency is injected properly.
* If I make GreeterEjb a non-EBJ by removing the @Stateless annotation, it
works fine.
Tests with an Interface GreeterInterface (see code below). GreeterEjb
implements GreeterInterface. In InjectedServlet changed type of field
greeterEjb from GreeterEjb to GreeterInterface:
* @Remote annotation on GreeterInterface: Test still fails, but I get a
different exception at deployment:
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied
dependencies for type [GreeterInterface] with qualifiers [@Default] at
injection point [[field] @Inject private
com.example.ejbconstructorinjection.InjectedServlet.greeterEjb]"}}
* @Local annotation on GreeterInterface: Now the test works fine
* no annotation on GreeterInterface: Now the test works fine
I find this quite confusing. Any ideas why constructor injection would not
work in this example? Am I missing something?
@WebServlet(urlPatterns = "/injectedservlet") public class InjectedServlet
extends HttpServlet { @Inject private GreeterEjb greeterEjb; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
IOException { PrintWriter out = resp.getWriter();
out.write(greeterEjb.greet()); out.close(); } } // comment @Stateless
annotation and the servlet works fine @Stateless public class GreeterEjb {
private Greeter greeter; @Inject public GreeterEjb(Greeter greeter) {
this.greeter = greeter; } // un-comment this no-argument constructor and the
servlet works fine // protected GreeterEjb() { // } public String greet() {
return greeter.greet(); } } public class Greeter { public String greet() {
return "Hello, World!"; } } public interface GreeterInterface { String
greet(); }
[#|2011-11-04T15:51:37.415+0100|WARNING|glassfish3.1.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=74;_ThreadName=Thread-2;|StandardWrapperValve[com.example.ejbconstructorinjection.InjectedServlet]:
PWC1382: Allocate exception for servlet
com.example.ejbconstructorinjection.InjectedServlet
com.sun.enterprise.container.common.spi.util.InjectionException: Error
creating managed object for class: class
com.example.ejbconstructorinjection.InjectedServlet at
com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:315)
at
com.sun.enterprise.web.WebContainer.createServletInstance(WebContainer.java:717)
at
com.sun.enterprise.web.WebModule.createServletInstance(WebModule.java:1959)
at
org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1263)
at
org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1070)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:189)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at
org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98) at
com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at
org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at
com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725) at
com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019) at
com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at
com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at
com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at
com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at
com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at
com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at
com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Unknown Source) Caused by:
org.jboss.weld.exceptions.WeldException: WELD-000033 Could not instantiate
client proxy for Session bean [class
com.example.ejbconstructorinjection.GreeterEjb with qualifiers [@Any
@Default]; local interfaces are [GreeterEjb] at
org.jboss.weld.bean.SessionBean.create(SessionBean.java:343) at
org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:67)
at
org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:690)
at
org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:772)
at
org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:138)
at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:872) at
org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:884) at
org.jboss.weld.manager.SimpleInjectionTarget$1.proceed(SimpleInjectionTarget.java:120)
at
org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:134)
at
org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:50)
at
org.jboss.weld.manager.SimpleInjectionTarget.inject(SimpleInjectionTarget.java:114)
at
org.glassfish.weld.services.JCDIServiceImpl.createManagedObject(JCDIServiceImpl.java:278)
at
org.glassfish.weld.services.JCDIServiceImpl.createManagedObject(JCDIServiceImpl.java:229)
at
com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl.createManagedBean(ManagedBeanManagerImpl.java:470)
at
com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl.createManagedBean(ManagedBeanManagerImpl.java:420)
at
com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:299)
... 28 more Caused by: java.lang.InstantiationException:
com.example.ejbconstructorinjection.org$jboss$weld$bean-ejbconstructorinjectionbugforum-SessionBean-GreeterEjb_$$_WeldProxy
at java.lang.Class.newInstance0(Unknown Source) at
java.lang.Class.newInstance(Unknown Source) at
org.jboss.weld.util.reflection.SecureReflections$16.work(SecureReflections.java:395)
at
org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:54)
at
org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInstantiation(SecureReflectionAccess.java:216)
at
org.jboss.weld.util.reflection.SecureReflections.newInstance(SecureReflections.java:390)
at org.jboss.weld.bean.SessionBean.create(SessionBean.java:332) ... 43 more
--
[Message sent by forum member 'arendvr.com']
View Post: http://forums.java.net/node/860661