开发者

Spring Config file consisting of List of Implementations

开发者 https://www.devze.com 2023-01-27 10:35 出处:网络
I Am very new to Spring. I have an Interface (MessageHandler ) which has a get method, this method returns a list of Implementations of another interface (messageChecker).

I Am very new to Spring. I have an Interface (MessageHandler ) which has a get method, this method returns a list of Implementations of another interface (messageChecker).

public interface MessageHandler {
   public void process(BufferedReader br);
   public void setMessageCheckerList(List mcList);
   [B]public List getMessageCheckerList();[/B]
 }

In my Spring XML configuration , i have something like this ,along with other beans

<bean id="messageHandler" class="com.XXX.messagereceiver.MessageHandlerImpl">
  <property name="messageCheckerList" ref="checkerList"/>
 </bean>

 <bean id="checkerList" class="java.util.ArrayList">
  <constructor-arg>
   <list>
    <ref bean="HL7Checker"/>
   </list>
  </constructor-arg>
 </bean>

 <bean id="HL7Checker" class="com.XXX.messagereceiver.HL7CheckerImpl">
     <property name="messageExecutor" ref="kahootzExecutor"/>
 </bean>

Here i am passing a checkerlist - which is a list of Implementations ( For now i have only 1) of the Interface (messageChecker)

Checkerlist is containing references to Bean Id's which are actual implementaions.

HL7Checker is an implementation of an Interface mes开发者_运维技巧sageChecker.

But when i run the main program, When i inject the bean "messageHandler" and call the getMessageCheckerList, It returns a null value. These getter and setter methods are working fine without using spring.

I am not sure what seems to be the problem.


I don't know the answer for you troubles, but I would check:

  • is the setter setMessageCheckerList(List) in messageHandler bean called? (either using some debugger or some trace output like System.out...). If it's not, there's probably something wrong with your Spring XML configuration setup. The bean definition you posted requires the property to be set and Spring wouldn't create the messageHandler bean without setting the property.
  • who calls the setMessageCheckerList(List) setter? Or even more precise, what code writes to the field which stores the value of the property? Maybe the field is initialized properly by Spring but gets overwritten to null later on?
  • are you sure you call the getMessageCheckerList on the very same object Spring has configured for you (that is, the messageHandler bean). The definition you have posted clearly states an instance of MessageHandlerImpl is created by Spring, but it doesn't prevent other instances to be created in other ways. So maybe the instance created by Spring holds the proper value, but you run the get... on a wrong instance?
0

精彩评论

暂无评论...
验证码 换一张
取 消