Announcement Announcement Module
Collapse
No announcement yet.
Setting properties in subclass of abstract class through XML version 3.1.1.RELEASE Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting properties in subclass of abstract class through XML version 3.1.1.RELEASE

    I'm relatively new to Spring, but have read documentation, and looked for examples, and may be hitting a bug (or I might just be confused).

    I've got the following entries in my "services.xml" file:

    Code:
    <bean id="workBean" class="com.myCompany.WorkBean">
    		<property name="workSteps">
    			<list>
    				<ref bean="firstStep" />
    				<ref bean="secondStep" />
    				<ref bean="thirdStep" />
    			</list>
    		</property>
                    ...
                    <property>
                       ...
                    </property>
    </bean>
    
    <bean id="abstractStep" abstract="true" class="com.myCompany.AbstractWorkStep">
    		<property name="name" value="Abstract Step - This should not happen!"/>
    		...
    		<property>
    		   ...
    		<property>
    </bean>
    
    <bean id="firstStep" parent="abstractStep" class="com.myCompany.WorkStepOne">
    		<property name="name" value="Step 1"/>
    </bean>
    
    <bean id="secondStep" parent="abstractStep" class="com.myCompany.WorkStepTwo">
    		<property name="name" value="Step 2"/>
    </bean>
    
    <bean id="thirdStep" parent="abstractStep" class="com.myCompany.WorkStepThree">
    		<property name="name" value="Step 3"/>
    </bean>
    Given that this is a command-line launched application (a migration "tool"), the main() function looks as follows:

    Code:
    public static void main(String[] args)
    {
       ...
       
       GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
       context.load("classpath:/services.xml");
       context.refresh();
    
       ...
    
       WorkBean worker = ctx.getBean("workBean", com.myCompany.WorkBean.class);
    
       ...
    }
    My worker class contains an ArrayList<AbstractWorkStep> steps list, with the appropriate getters and setters.

    When I debug (in Eclipse) during a test, and I look at the worker object, and examine the steps list in the worker class, I find that the name attribute in the instantiated instance of each of the subclasses of AbstractWorkStep is null, which is causing problems with code later in the application.

    My understanding is that the subclasses, because they reference the abstractStep as their parent, should override the name initially set in the abstractStep class, and the names should be firstStep, secondStep, and thirdStep, respectively. At a minimum, I would expect that the name for each of them would be "Abstract Step - This should not happen!", which would give me a bit more information than the null is giving me.

    My first question is, am I correct in the assumption that, given the configuration above, I should see firstStep, secondStep, and thirdStep, or should I see "Abstract Step - This should not happen!" instead?

    My second question is, given that I'm currently seeing a null in the name value for all of the instances, what am I missing in my configuration or my instantiation of the bean that is causing it simply not to have a value?

    I'm pretty sure (given my relative inexperience) that this is a problem on my end, but I'm not sure where to start to look for the solution. Any assistance would be greatly appreciated.

  • #2
    I'm not sure if this is just a typo but in your main you load the xml into context but get the bean from ctx.

    Do you have a more complete test case you can share on github or similar?

    // Pontus

    Comment


    • #3
      You're right; the discrepancy between context and ctx was a typo. My apologies... I'm the world's worst transcriptionist.

      Turns out, in creating the example to post to github, I was unable to replicate the problem... which tells me that the problem is elsewhere in the code. I''ll see if I can track down where the real problem is, but for now, I'm gonna approach the app in a different manner.

      Thanks for the interest here!

      Comment

      Working...
      X