Announcement Announcement Module
Collapse
No announcement yet.
How to put a parent/child bean in singletonCache? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Well it simply seems as if your file doesn't get processed, are you loading it correctly?

    Comment


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

      Comment


      • #4
        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>

        Comment


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

          Comment


          • #6
            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?

            Comment


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

              Comment


              • #8
                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, 09:26 AM.

                Comment


                • #9
                  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, 10:12 AM. Reason: Mistyping

                  Comment


                  • #10
                    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.

                    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

                    Comment

                    Working...
                    X