Hi,
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.
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet( urlPatterns = { "/test" } )
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = -2770072595708915532L;
private TestBean testBean;
@Inject
public void setTestBean( TestBean testBean ) {
System.out.printf( "inject: %s%n" , this );
this.testBean = testBean;
}
public TestBean getTestBean() {
return testBean;
}
@PostConstruct
public void postConstruct() {
System.out.printf( "post construct: %s, %s%n", testBean, this );
}
@Override
public void init() throws ServletException {
super.init();
System.out.printf( "init: %s, %s%n", testBean, this );
}
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp
) throws ServletException, IOException {
resp.setContentType( "text/plain" );
resp.getWriter().printf( testBean == null ? "is null" :
testBean.get() );
}
}
The order and the output of the methods is the following:
post construct: null, com.eurospider.cpm.kyc.crif.TestServlet@733058ac
inject: com.eurospider.cpm.kyc.crif.TestServlet@733058ac
init: com.eurospider.cpm.kyc.crif.TestBean@52f8f92b,
com.eurospider.cpm.kyc.crif.TestServlet@733058ac
I see that the class org.apache.catalina.core.DefaultInstanceManager on
line 226 calls the post construct annotated method of my servlet. But at
this point Weld didn't inject any beans into the servlet.
Is there a way to configure tomcat or weld so that the order of the
called method is correct according to the CDI 2.0 specification in
chapter 19.3.2.
Kind regards,
Roger Wegmann
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org