Hi Scott,
When a @ApplicationScoped/@Singleton bean is designed to be initialized
after webapp starts (I.E. when first http request was fired) instead of being
initialized during startup stage, in given circumstance, it won't be
injected correctly.
This is my test case:
========== StartBean ===========
public class StartBean {
@Inject
FirstBean firstBean;
}
========== FirstBean ===========
public class FirstBean {
@Inject
SecondBean secondBean;
}
========== SecondBean ===========
public class SecondBean {
@Inject
ThirdBean thirdBean;
@Inject
FourthBean fourthBean;
}
========== ThirdBean ===========
public class ThirdBean {
@Inject
SingletonBean singletonBean;
}
========== FourthBean ===========
public class FourthBean {
@Inject
SingletonBean dao;
}
========== SingletonBean ===========
@Singleton
//@Startup
public class SingletonBean {
}
========== MyServlet ===========
public class MyServlet extends HttpServlet {
@Inject
StartBean startBean;
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
System.out.println("hello");
}
}
========= web.xml ===========
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet id="MyServlet">
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.mycompany.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>
=================================
The "http://localhost:8080/MyServlet" request will produce this exception:
com.mycompany.servlet.MyServlet.startBean:
com.mycompany.beans.StartBean.firstBean:
com.mycompany.beans.FirstBean.secondBean:
com.mycompany.beans.SecondBean.fourthBean:
com.mycompany.beans.FourthBean.singletonBean:
java.lang.IllegalArgumentException:
Can not set com.mycompany.beans.SingletonBean field
com.mycompany.beans.FourthBean.singletonBean
to com.mycompany.beans.FourthBean
When I annotate the SingletonBean as @Startup, it's fine. (uncomment
the //@Startup line in SingletonBean.java)
Either @Singleton or @ApplicationScoped throws the exception.
It's a weird bug, taking me nearly two days to reproduce it in such a
simple test case.
-Wesley
_______________________________________________
resin-interest mailing list
[email protected]
http://maillist.caucho.com/mailman/listinfo/resin-interest