Results 1 to 9 of 9

Thread: Subclassing a bean

  1. #1
    Join Date
    Sep 2004
    Location
    London
    Posts
    311

    Default Subclassing a bean

    hello all,
    i have a question: does subclassing work in Spring?

    i have declared in spring bean MYFIRSTBEAN which has property X.
    i have declared also MYEXTENDEDBEAN that extends MYFIRSTBEAN.

    in applicationcontext.xml i have declared property X only for MYFIRSTBEAN.

    In my application, i am retrieving MYEXTENDEDBEAN, but when i try to retrieve property X, i got a null pointer exceptionj.

    Is it so that if i want it to work, i have to declare the property also on MYEXTENDEDBEAN?

    thanx in advance and regards
    marco

  2. #2
    Join Date
    Aug 2004
    Location
    Sydney
    Posts
    503

    Default

    Can you post your Spring bean definitions?

  3. #3
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    Quote Originally Posted by gmatthews
    Can you post your Spring bean definitions?
    I Agree.

    What do you mean with 'declare property y'? Properties are not declared in Spring (unless you are doing something with AOP I guess). Properties can be accessed, not declared.

  4. #4
    Join Date
    Sep 2004
    Location
    London
    Posts
    311

    Default

    Hello all,
    sorry for not being clear enough.
    Ok here are my bean

    public class SimpleBean {
    private Manager mgr;

    public void setManager(Manager manager)

    public Manager getManager()

    }

    public class ExtenedBean extends SimpleBean {

    public void execute(...) {
    Manager mgr = getManager();
    // do something
    }


    appContext.xml

    <bean name="Manager" class=".. Manager"/>

    <bean name="SimpleBean" class="... SimpleBean">
    <property name="manager@>
    <ref local="manager"/>
    </property>
    </bean>

    <bean name="ExtendedBean" class=".. ExtendedBean"/>


    now, Manager get set correctly on SimpleBean, but when i try to get it from ExtendedBean (by calling 'getManager()') i got a null pointer exception.

    I guess i need to specify the property "manager" also for ExtendedBean if i don't want to have a null pointer exception..

    is that (specifying property in appContext.xml also for ExtendedBean) the only choice?

    thanx in advance and regars
    marco

  5. #5
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    I guess i need to specify the property "manager" also for ExtendedBean if i don't want to have a null pointer exception..
    I`m not sure what you mean by this. But you don`t have to do anything special for subclassing. I think something else is going wrong.. please test your code.

  6. #6
    Join Date
    Aug 2004
    Location
    Germany
    Posts
    24

    Default

    Yes, you also have to inject the dependency "manager" into "ExtendedBean".

    Your beans "SimpleBean" and "ExtendedBean" are two seperate instances, and your configuration only injects the manager in one instance.

    Hope, this helped you,
    SotA

  7. #7
    Join Date
    Sep 2004
    Location
    London
    Posts
    311

    Default

    Hello again and thank you for your fast reply.
    Ok, maybe i can summarize my goal. I have a set of beans beanN which needs all to use Manager bean for doing different operations. each of beanN does a different operation, but all beanN use Manager to accomplish their operation.
    So, i was wondering, if i define a baseBeanN, which has as property bean Manager, then i thought that if all of my beanN subclass baseBeanN, they all will have automatically an instance of Manager to be used.

    So, as in example posted, i have defined a SimpleBean (which will be my baseBean) and a ExtendedBean (which you can view as one of the beanN discussed above).

    for some weird reasons, when i call getManager() from my ExtendedBean (which should return the Manager bean of SimpleBean), i get back a null pointer exception.

    So, my assumption was that this applicationContext,xml

    <bean name="SimpleBean" class="...">
    <property name="manager">
    <ref local=".."/>
    </property>
    </bean>
    <bean name="ExtendedBean" class="..."/>

    will inject the Manager ONLY in SimpleBean, and it will NOT inject automatically the 'manager' bean in ExtendedBean, no matter if ExtendedBean subclasses SimpleBean.

    Is that a true assertion?


    regards
    marco

  8. #8
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote Originally Posted by MmarcoM
    will inject the Manager ONLY in SimpleBean, and it will NOT inject automatically the 'manager' bean in ExtendedBean, no matter if ExtendedBean subclasses SimpleBean.

    Is that a true assertion?
    Yes it is.

    Maybe the following code may help you to avoid assigning the manager for all of your beans explicitly:

    Code:
    <bean name="AbstractBean" abstract="true">
    <property name="manager">
    <ref local=".."/>
    </property>
    </bean>
    
    <bean name="ExtendedBean" class="..." parent="AbstractBean"/>
    Regards,
    Andreas

  9. #9
    Join Date
    Aug 2004
    Location
    Germany
    Posts
    24

    Default

    You could try it this way:

    Code:
    <bean name="SimpleBean" class="...">
        <property name="manager">
              <ref local=".."/>
       </property>
    </bean>
    <bean name="ExtendedBean" class="..." parent="SimpleBean" />
    So Spring knows that all the extended beans should get the same properties set as the parent "SimpleBean".

    Additionaly you can keep Spring from creating an instance of SimpleBean by defining it abstract:
    Code:
    <bean name="SimpleBean" class="..." abstract="true">
    ...
    </bean>

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •