Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-2734

Weld CDI, PostContruct Annotated Method Called Before Objects Are Injected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Minor Minor
    • None
    • 3.1.9.Final, 5.1.0.Final
    • None

      I use Apache Tomcat 9.0.68 and Weld 3.19.Final. I created a Servlet that uses @Inject to inject a simple application scoped Java object. I also annotated a method with @PostConstruct. The problem is now, that the method annotated with @PostConstruct is called before the Java object has been injected.

      I attached both, the Servlet and the Java object.

      The order and the output when I call /test is the following:

      1) post construct: null, com.eurospider.cpm.kyc.crif.TestServlet@2d9d797b

      2) inject: com.eurospider.cpm.kyc.crif.TestServlet@2d9d797b

      3) init: com.eurospider.cpm.kyc.crif.TestBean@18820347

      4) com.eurospider.cpm.kyc.crif.TestServlet@2d9d797b

      The problem is, that Apache Tomcat calls the @PostConstruct annotated method in its class org.apache.catalina.core.DefaultInstanceManager on line 226. But this is too early because the injection of the objects into the Servlet is done by Weld after this call.

      In the method newInstance(String fqcn) in the class org.jboss.weld.environment.tomcat.WeldForwardingInstanceManager the call super.newInstance(fqcn) triggers the call of the PostConstruct method of the servlet, but the injection of the objects happens one line after with secondProcessor.newInstance(a).

      Is there a way to solve this issue? According to chapter 19.3.2 of the CDI specification, the injection of objects should happen before a method annotated with @PostConstruct is called.

      Thanks for your help.

      Roger Wegmann

       

        1. TestBean.java
          0.2 kB
          Roger Wegmann
        2. TestServlet.java
          1 kB
          Roger Wegmann

              manovotn Matěj Novotný
              roger.wegmann@eurospider.com Roger Wegmann (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: