I have a CDI bean, and a interceptor is added to one method of this bean. If this bean doesn't have a default constructor(I use the Constructor Injection). At runtime, I get the exception
java.lang.Class.newInstance0(Class.java:357)
java.lang.Class.newInstance(Class.java:325)
org.apache.webbeans.proxy.javassist.JavassistFactory.createProxy(JavassistFactory.java:79)
org.apache.webbeans.proxy.ProxyFactory.createProxy(ProxyFactory.java:241)
org.apache.webbeans.proxy.ProxyFactory.createDependentScopedBeanProxy(ProxyFactory.java:412)
org.apache.webbeans.component.AbstractInjectionTargetBean.createDefaultInstance(AbstractInjectionTargetBean.java:140)
org.apache.webbeans.component.AbstractInjectionTargetBean.createInstance(AbstractInjectionTargetBean.java:116)
org.apache.webbeans.component.AbstractOwbBean.createNewInstance(AbstractOwbBean.java:233)
org.apache.webbeans.portable.creation.AbstractProducer.produce(AbstractProducer.java:77)
org.apache.webbeans.component.InjectionTargetWrapper.produce(InjectionTargetWrapper.java:136)
If I add a default constructor in this bean, or remove the interceptor, it will be OK. In fact, I can use the field injection to handle this question.
But I want to know: if I add a default constructor, that means this bean has two constructors -- one is non-parametric, the other with @Inject
ed parameters. In this case (with interceptor), will The container create the instance twice?
Edit: I use Tomee1.5, and WebShpere8.5 is same as Tomee1.5 and it seems that GlassFish3.1.2 does not have this issue.
Edit: i found an answer in Tomee User Forum that is The CDI container will invoke the @Inject annotated ct for your bean but will use the default ct for creating the proxies. So, I think that means if you want to use constructor injection, u also need a default constructor for being proxyable.
Edit:
According to http://docs.jboss.org/weld/reference/latest/en-US/html/injection.html#d0e1443 unless a bean has the default scope @Dependent, the container must indirect all injected references to the bean through a proxy object.
According to //openejb.979440.n4.nabble.com/RequestScoped-CDI-constructor-td4661541.html The CDI container will invoke the @Inject annotated constructor for your bean but will use the default constructor for creating the proxies. Therefor The default constructor is needed for all 'NormalScoped' (@RequestScoped @SessionScoped @ApplicationScoped @ConversationScoped) beans because they will always get proxied.
If a bean uses interceptor, according to the error message, OpenWebBean uses Proxy to deal with interceptor, so the intercepted classes must have the default construct. But Weld uses subclasses for interceptors and decorators. https://issues.jboss.org/browse/WELD-437?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel The subclassing solution is superior to proxification, since it avoids dealing with a number of issues such as: intercepted classes without a no-arg constructor and field access on proxified instances. So, if I use Tomee and Websphere, it needs a default ct, but GlassFish doesn't.
Thanks brandizzi, It is my first question here. Thank you for your help. And it seems I can not answer my own question and post more than two links without 10 reputation, so I edited here.