Announcement Announcement Module
No announcement yet.
@Resource not injected sometimes Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Resource not injected sometimes

    Hi all

    I have a very strange issue when I use @Resource to inject beans.
    I am using Struts2 with provided Spring plugin with Spring 3.0.0. (I am not able to upgrade to the newest version of Spring without knowing the proper cause because all programs are in production)

    The issue or observed problem is when an Struts2 Action is created, the fields annotated with @Resource are supposed to have resources injected by Spring. However, some times and only sometimes one of the annotated resources is not inject or the value is simply null and therefore causes NullPointerException. The point where problem occurs is undetermined which mean the same set of programs running in different environment will result in different behaviors. Also the resource that is not injected is not always the same.
    For example, if there are actions A, B, C, and environment E1 and E2, in E1 the A action might have this problem sometimes and in E2 it might be action B that's having the problem. One thing that's certain is that if in E2 B is having problem it will happen from time to time and A and C wont have problem or at least the problem just not observed on A and C. Moreover, if A has 5 @Resource fields, when the problem occurs the NPE may be thrown when accessing the first resource however the next time may be the second resource.

    Here is what I mean by the problem only happens "sometimes". Suppose A is having this kind of problem, and I start web server (tomcat or WAS) and I go access A for the first time if the problem occurs it will occur throughout the time of this server start-up. If the problem doesn't occur the first time I access A then throughout the time of this server start-up the problem wont occur. Also if this time it's the first resource not injected then it will be the same for this start-up.

    Here is a bit of my application setup:
    I use XML inter-mixed with annotation scanning. Basically all Action, Service, Dao classes are defined in XML however all property definitions are left out for Spring to scan the actual class.

    Sample definition:

    <!-- have this in all XML files -->
    <!-- an action definition, all actions are scoped prototype. It will use adm.common.admBranchesManager in the action with field annotated with @Resource -->
    <bean id="adm.common.chooseBranchAction" class="" scope="prototype"></bean>
    <!-- all service and dao are singleton and do not have any problem, all service/Manager are annotated with @Transactional. In Action we only code against interface and not actual concrete class -->
    <bean id="adm.common.admBranchesManager" class=""/>
    <bean id="adm.common.admBranchesDao" class=""/>
    Also for all actions they all extends an abstract action that has a resource field that's session-scoped.

    <bean id="base.wms.login" class="" destroy-method="logout" scope="session">
         <aop:scoped-proxy />
         <property name="admUserSessionsManager" ref="adm.operation.admUserSessionsManager"/>
    Here is a part of a sample action:
    //this class is just a sample not the actually one thats having the problem, AbstractWmsAction is the class that have a session-scoped bean
    public class AdmWmsControlAction extends AbstractWmsAction
        @Resource(name = "adm.operation.admWmsBatchGroupsManager")
        private AdmWmsBatchGroupsManager admWmsBatchGroupsManager;
        @Resource(name = "adm.operation.admWmsControlManager")
        private AdmWmsControlManager admWmsControlManager;
    //sometimes we use setters for injecting but that doesnt stop the problem from happening
    Don't know if anyone had this kind of issue.
    If additional information is needed, I will do my best to provide.