Results 1 to 10 of 10

Thread: How to put a parent/child bean in singletonCache?

  1. #1

    Default How to put a parent/child bean in singletonCache?

    I have parent/child bean,

    Code:
    	<bean id="abstractATG" class="poll.atg.AbstractATG" abstract="true" >
    	</bean>
    	
    	<bean id="veederRootATG" parent="abstractATG" >
    		<property name="atgType"><value>test</value></property>
    	</bean>
    public class AbstractATG {
    private String atgType = null;
    /*ignore its setter/getter methods here*/
    }
    And Java code to retrieve child bean based on bean id,

    Code:
    public class AtgManagerImpl implements ApplicationContextAware {	private ApplicationContext       applicationContext    = null;
    	public <V> V getAtgById(String beanId) {
    		V atg = (V)getApplicationContext().getBean(beanId);
    		return atg;
    	}
    
    	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    		this.applicationContext=applicationContext;
    		
    	}
    
    	public ApplicationContext getApplicationContext() {
    		return applicationContext;
    	}
    }
    But I always got error about no child(concrete) bean found,

    appfuse] INFO [http-8080-Processor24] ExecuteQuery.info(51) | Exec[0]: AtgManager.getAtgById()
    [appfuse] WARN [http-8080-Processor24] ExecuteQuery.warn(67) | Method execution failed:
    org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'veederRootATG' is defined
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:355)

    I am using Spring 2.0 and tracked code in getBean(String, Class, final Object[]) of AbstractBeanFactory.java. Problem is in that method code tried to retrieve child bean from singletonCache as below, but neither parent bean or child bean exists in such cache.

    Code:
    public Object getSingleton(String beanName) {
    		synchronized (this.singletonCache) {
    			return this.singletonCache.get(beanName);
    		}
    	}
    My question is how I can configure parent/child bean so they can be located in singletonCache? What class should I extend for AbstractATG? Thanks!

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Well it simply seems as if your file doesn't get processed, are you loading it correctly?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    It gets loaded correctly if I use getApplicationContext().getBeansOfType(AbstractATG .class). From its return I can see concrete bean there.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    If I'm not mistaken you NEED to have a class property on each bean... Your 'concrete' implementation doesn't have that...

    No class element
    Code:
    <bean id="veederRootATG" parent="abstractATG" >
    	<property name="atgType"><value>test</value></property>
    </bean>
    should be

    Code:
    <bean id="veederRootATG" parent="abstractATG" class="poll.atg.AbstractATG">
    	<property name="atgType"><value>test</value></property>
    </bean>
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    Can you use getBeanDefinitionNames() to see which bean names are visible at all?

  6. #6

    Default

    I don't think that is the reason. I worked with another set of parent/child beans before and child bean can be located properly with getApplicationContext().getBean(beanId). The difference is parent bean in last case is extended from SimpleFormController, but this parent bean just implements ApplicationContextAware. How come getApplicationContext().getBeansOfType() in this implement shows me concrete beans properly?

  7. #7

    Default

    I will try getBeanDefinitionNames() later, but from getBeansOfType() I do see concrete bean's name(not parent bean's name) in its return.

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Well at least I would try it because I really think that that is the issue. If it is correct is debatable...

    The getType would be explained because it is an instance of it's parent (remember parent declaration is something else as the extend keyword!), however your concrete class doesn't have a type.
    Last edited by Marten Deinum; Jul 17th, 2007 at 09:26 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Quote Originally Posted by mdeinum View Post
    If I'm not mistaken you NEED to have a class property on each bean... ...]
    You are mistaken, Spring reference stated (Chapter 3.6)
    "A child bean definition will use the bean class from the parent definition if none is specified, but can also
    override it. In the latter case, the child bean class must be compatible with the parent, that is it must accept the
    parent's property values."

    Another story, that it is not what likely kkus will, as (concluding from parallel thread http://forum.springframework.org/sho...d=1#post132270) classes from his abstract anc concrete beans shall be different.

    Regards,
    Oleksandr
    Last edited by al0; Jul 17th, 2007 at 10:12 AM. Reason: Mistyping

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I was merely reflecting our own issues we had when not specifying a class parameter on a beans element. Might work now (at least according to the ref guide it should . Thanks for pointing that out btw.

    Quote Originally Posted by al0
    Another story, that it is what likely kkus will, as (concluding from parallel thread http://forum.springframework.org/sho...d=1#post132270) classes from his abstract anc concrete beans shall be different.
    That is something I don't see at least not with the opening post of this thread. Confusing having 2 threads about the same issue :s
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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