Announcement Announcement Module
Collapse
No announcement yet.
[pebcak] Parent Bean values not being passed... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [pebcak] Parent Bean values not being passed...

    I'm using Spring for a small framework to be used with command line programs and a parent property value is null when I try to use it...

    Hierarchy:
    ICommandLine
    |-AbstractCommandLine
    |--HelloWorldCommandLine

    ICommandLine is just an interface with the method "execute".
    AbstractCommandLine implements ICommandLine (not concretely just in definition) and has a property (field) "item" along g/s.
    HelloWorldCommandLine has a field "message" and g/s.

    the item (which comes from the parent definition) is null... What am I doing wrong here?
    Last edited by functionform; Mar 28th, 2007, 02:50 PM. Reason: scenario code below

  • #2
    Originally posted by functionform View Post
    the item (which comes from the parent definition) is null... What am I doing wrong here?
    Are you saying that the item property isn't set as "true" it's null? Is it possible to have a look at the classes?

    Comment


    • #3
      that's exactly right; it's null. If you need the classes, I can get those in a few minutes...

      Comment


      • #4
        defs.xml
        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:flow="http://www.springframework.org/schema/webflow-config"
               xsi:schemaLocation="
                   http://www.springframework.org/schema/beans
                   http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
                   
        	<bean id="abstractCommandLine" abstract="true" class="com.poc.AbstractCommandLine">
        		<property name="item" value="true" />
        	</bean>
        
        	<bean id="helloWorldCommandLine" class="com.poc.HelloWorldCommandLine" parent="abstractCommandLine">
        		<property name="message" value="Hello World!" />
        	</bean>
        	
        </beans>
        ICommandLine.java
        Code:
        package com.poc;
        
        public interface ICommandLine {
        
        	public void execute();
        }
        AbstractCommandLine
        Code:
        package com.poc;
        
        import org.springframework.beans.factory.BeanFactory;
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        
        public abstract class AbstractCommandLine implements ICommandLine {
        
        	protected String item;
        	
        	public final void run() {
        		
        		ApplicationContext context = new ClassPathXmlApplicationContext("defs.xml");
        		BeanFactory factory = (BeanFactory) context;
        		String simpleName = this.getClass().getSimpleName();
        		String beanName = toBeanName(simpleName);
        		ICommandLine cl = (ICommandLine) factory.getBean(beanName);
        		cl.execute();
        		System.out.println("item parent: " + item);		
        	}
        	
        	private String toBeanName(String name) {
        
        		StringBuilder sb = new StringBuilder();
        		char[] chars = name.toCharArray();
        		
        		for (int i = 0; i < chars.length; i++) {
        			char ch = chars[i];
        			if (i == 0) {
        				sb.append(Character.toLowerCase(ch));
        			} else {
        				sb.append(ch);
        			}
        		}
        		
        		return sb.toString();
        	}
        
        	public String getItem() {
        		return item;
        	}
        
        	public void setItem(String item) {
        		this.item = item;
        	}	
        }
        HelloWorldCommandLine
        Code:
        package com.poc;
        
        public class HelloWorldCommandLine extends AbstractCommandLine {
        
        	private String message;
        
        	public static void main(String[] args) {
        		HelloWorldCommandLine hw = new HelloWorldCommandLine();
        		hw.run();
        	}
        	
        	public void execute() {
        		System.out.println("message: " + message);
        		System.out.println("item child: " + item);
        	}
        	
        	public String getMessage() {
        		return message;
        	}
        
        	public void setMessage(String message) {
        		this.message = message;
        	}
        }
        output:
        Code:
        message: Hello World!
        item child: true
        item parent: null
        Last edited by functionform; Mar 28th, 2007, 03:10 PM. Reason: added log in parent

        Comment


        • #5
          ok.... Java 101! Apparently what it was is that I was trying to reference the wrong scope if you will. One context initializing another, where the one runing the process didn't have a value for "item". Once I changed the cast to AbstractCommandLine and then used a prefix for it, it worked fine.

          Sheesh!

          Comment

          Working...
          X