Results 1 to 7 of 7

Thread: How to link method's return value to hashmap's value?

  1. #1
    Join Date
    Jul 2007
    Posts
    9

    Default How to link method's return value to hashmap's value?

    Hi,

    Question:
    Is there a way by which I can link a hashmap's value to the return value of a bean's method in the config file?

    I am looking for something like
    <bean id="test" class="test.SomClass">
    <property>
    <map>
    <entry key="test1">
    <value>beanName.getValueFromMethod</value>
    </entry>
    </map>
    </bean>

    Thanks in advance
    Last edited by yasser; Jul 30th, 2007 at 02:23 PM.

  2. #2
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    It's covered by the reference. Combine both.

    Joerg

  3. #3
    Join Date
    Jul 2007
    Posts
    9

    Default

    If my understanding is right, this would mean that I have to write a factory method which would set the map values from the bean getter methods explicitly.

    I am looking for a map which gets created on the fly from a bean instance in the container. The config file will link the key to the method names of the bean, so that there is no Java code written to link them.

    If I misinterpreted the links that you have provided, please help me by giving an example.

    Thanks a lot for the reply.
    Last edited by yasser; Jul 31st, 2007 at 03:54 PM.

  4. #4
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by yasser View Post
    If my understanding is right, this would mean that I have to write a factory method which would set the map values from the bean getter methods explicitly.
    No, it's pure config:

    Code:
    <bean id="test" class="test.SomClass">
      <property>
        <map>
          <entry key="test1">
          <bean class="beanName" factory-method="getValueFromMethod"/>
        </entry>
      </map>
    </bean>
    That was what I had in mind.

    Quote Originally Posted by yasser View Post
    I am looking for a map which gets created on the fly from a bean instance in the container. The config file will link the key to the method names of the bean, so that there is no Java code written to link them.
    I don't know if the above already matches it. I don't get it completely.

    Joerg

  5. #5
    Join Date
    Jul 2007
    Posts
    9

    Default

    I tried the way you have suggested... It is giving an exception that Factory method cannot be found...

    Config File
    <bean id="TEST" class="com.service.TEST">
    <property name="applicationMap">
    <map>
    <entry key="attribute1">
    <bean class="com.service.TESTAPP" factory-method="getAttribute1" />
    </entry>
    <entry key="attribute2">
    <bean class="com.service.TESTAPP" factory-method="getAttribute2" />
    </entry>
    </map>
    </property>
    </bean>


    Both TEST and TESTAPP are simple POJOs. I made TESTAPP extend the AbstractBeanFactory class too. but in vain...

    Exception Trace
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'TEST' defined in class path resource [b2b-core-context/beans-b2b-core-extracts_test.xml]: Cannot create inner bean 'com.service.TESTAPP#22299c2c' while setting bean property 'applicationMap' with key [attribute1]; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'com.service.TESTAPP#22299c2c' defined in class path resource [b2b-core-context/beans-b2b-core-extracts_test.xml]: Cannot find matching factory method 'getAttribute1' on class [com.service.TESTAPP]
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'com.service.TESTAPP#22299c2c' defined in class path resource [b2b-core-context/beans-b2b-core-extracts_test.xml]: Cannot find matching factory method 'getAttribute1' on class [com.service.TESTAPP]
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateUsingFactory Method(AbstractAutowireCapableBeanFactory.java:599 )
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:354)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveInnerBeanDefinition(Bea nDefinitionValueResolver.java:151)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:97)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveManagedMap(BeanDefiniti onValueResolver.java:235)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:118)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1046)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:857)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:378)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:233)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:145)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:283)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:313)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:87)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:72)
    at org.springframework.test.AbstractSpringContextTest s.loadContextLocations(AbstractSpringContextTests. java:135)
    at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.loadContextLocations(Abstract DependencyInjectionSpringContextTests.java:224)
    at org.springframework.test.AbstractSpringContextTest s.getContext(AbstractSpringContextTests.java:115)
    at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.setUp(AbstractDependencyInjec tionSpringContextTests.java:192)
    at junit.framework.TestCase.runBare(TestCase.java:125 )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at junit.framework.TestResult.run(TestResult.java:109 )
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:436)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:311)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:192)

    Thanks a lot for the help you are providing.. Appreciate it.

  6. #6
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    My fault, copied the wrong stuff from the reference. It's the static factory method. You need the instance factory method:

    Code:
    <bean id="test" class="test.SomClass">
      <property>
        <map>
          <entry key="test1">
          <bean factory-bean="beanName" factory-method="getValueFromMethod"/>
        </entry>
      </map>
    </bean>
    
    <bean id="beanName" class="test.OtherClass"/>
    Hope this works now

    Joerg

  7. #7
    Join Date
    Jul 2007
    Posts
    9

    Default

    Got it working this time. Thanks a lot for your timely help.

    However, I noticed that if the value of the getter method (factory method) is null, an excpetion is thrown saying that null cannot be used to instaniate the value. This shouldn't be a problem anyway. Just thought of pointing it out.
    Thanks again.

Posting Permissions

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