0

I have problem with Constructor Injection in CDI. Field or property injection works fine. Constructor Injection of Field injected instances works fine as well. The only thing is not working - constructor injection in Servlet Classes.

Configuration: empty beans.xml in WEB-INF, BeanManager defined in context.xml.

Environment: Tomcat 9.0.24, CDI, WELD implementation.

Example code:

@WebServlet(urlPatterns = "/servlet/*")
public class SimpleServlet extends HttpServlet implements Serializable {

// @Inject
private TestService testService;

@Inject
public SimpleServlet(TestService testService) {
    this.testService = testService;
}

// Without this constructor (even protected) I have Exception java.lang.NoSuchMethodException: SimpleServlet.<init>()
public SimpleServlet() {
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // This prints null when using constructor injection
    // Prints instance hashcode when using field injection
    resp.getWriter().write(testService + "");
}

@PostConstruct
public void onPostConstruct() {
    System.out.println("This is invoked everytime");

    // Null when constructor injection
    System.out.println(testService);
}
}

POM.xml

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>8.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>2.0.SP1</version>
    </dependency>

    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet-shaded</artifactId>
        <version>3.1.2.Final</version>
    </dependency>
</dependencies>

StackTrace of ConstructorInjection inside Servlet:

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:151)
at org.jboss.weld.environment.tomcat.ForwardingInstanceManager.newInstance(ForwardingInstanceManager.java:26)
at org.jboss.weld.environment.tomcat.WeldForwardingInstanceManager.newInstance(WeldForwardingInstanceManager.java:71)

And ConstructorInjections inside other class:

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:119)
at org.jboss.weld.injection.ConstructorInjectionPoint.invokeAroundConstructCallbacks(ConstructorInjectionPoint.java:92)
at org.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:78)

So the WELD inside Servlet use ForwardingIntranceManager, but inside other classes ConstructorInjectionPoint. Bug inside WELD maybe?

Krzysztof
  • 517
  • 1
  • 4
  • 11

1 Answers1

0

you have already tried downgrading the CDI version? try changing these dependencies in your pom.xml.


<dependency>
  <groupId>javax.enterprise</groupId>
  <artifactId>cdi-api</artifactId>
  <version>1.2</version>
</dependency>

<dependency>
  <groupId>org.jboss.weld.servlet</groupId>
  <artifactId>weld-servlet</artifactId>
  <version>2.2.9.Final</version>
</dependency>  
  • Downgrading? Why? You hopefully mean to check if it is a bug that was not present in an earlier version so ot can be reported as such? – Kukeltje Nov 09 '19 at 15:21
  • just to check if this bug exists in other versions – Mattheus Cassundé Nov 09 '19 at 18:04
  • Then mention this in the 'answer' (and effectively it is not a real answer (=solution or clear explanation why it does not work) but more a comment (or am I wrong?) cheers. – Kukeltje Nov 09 '19 at 18:53
  • Thanks for your answer. Tried some different versions - problem is still the same. Added stack trace to my post. – Krzysztof Nov 11 '19 at 23:01