Mantis - Resin
Viewing Issue Advanced Details
3863 minor always 01-28-10 09:19 02-10-10 17:12
ferg  
ferg  
normal  
closed 4.0.2  
fixed  
none    
none 4.0.4  
0003863: @Singleton injection
(rep by Wesley Wu)

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.

Notes
(0004425)
ferg   
02-10-10 17:12   
ioc/0150