Announcement Announcement Module
Collapse
No announcement yet.
using commons-beanutils on Spring beans Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • using commons-beanutils on Spring beans

    I have a need to populate beans of unkown types with dynamic variables. More precisely, I have XML comming into a Spring MVC Controller that uses a helper class (got via Spring IoC) to parse the XML and populate other beans based on the XML. I'd like to get the beans from the application context and then use Jakarta Commons BeanUtils to populate properties.

    Originally I had the helper class maintain its own list of prototype beans. It would make a copy of the prototype that matched an XML node. Then it would populate the copy using org.apache.commons.beanutils.BeanUtils.copyPropert y(bean, name, value). (The name and value came from the XML.)

    The original design set up the prototypes in the no-arg constructor of the helper class, but when I started using Spring I got exceptions saying the class could not be instanciated.

    Now the helper class is BeanFactoryAware and looks up the beans from the BeanFactory as non-singletons. My tests show that that is working fine, but when I try to populate with BeanUtils.copyProperty(bean, name, value) the system just seems to go off into limbo: no exceptions, no nothing.

    Is org.apache.commons.beanutils.BeanUtils unable to work with Spring loaded beans? If so, why? and how do I work around it?

    I don't see a copyProperty(bean, name, value) equivalent in Spring. Is there one?

    TIA
    ,boz

  • #2
    http://www.springframework.org/docs/...BeanUtils.html

    Comment


    • #3
      Thanks for the reply. But Spring BeanUtils does not do what I need. Spring's BeanUtils.copyProperties(bean, bean) requires that I have a bean of the type I want to copy. For my case, the bean type is dynamic so I can't make one before I've made one before I've made one :-)

      I did some more tests, though, and they've got me more confused. As far as i can tell using commons BeanUtils fails if I call it from a bean that has been created by Spring. From in my Spring loaded helper class I tried expicitely creating a bean -- new MyBean() -- and populating it with commons BeanUtils.setProperty(myBean, "propertyName", "value") and it failed. If I do the same thing in a stand-alone test class with no Spring hooks it works.

      How could calling a static method on a utility class be affected by how my helper bean is created? I'm baffled. ... Sounds like user error to me.

      sigh
      ,boz

      Comment


      • #4
        I user common-beanutils with spring without problem - Your problem isn't spring

        regards

        Comment


        • #5
          Ahhhh. User error indeed.

          The commons BeanUtils jar was not in the WEB-INF/lib directory and the NoClassDefFoundException was getting swollowed by my code somewhere!

          ...
          ,boz

          Comment


          • #6
            boz,

            Springs equivalent to Commons.BeanUtils is BeanWrapperImpl.

            Code:
            BeanWrapper bw = new BeanWrapperImpl(myBean);
            
            bw.setPropertyValue("prop1", value1);
            
            Map propMap = new HashMap();
            propMap.put("prop1", value1);
            propMap.put("prop2", value2);
            bw.setPropertyValues(propMap);
            
            MutablePropertyValues propValues = new MutablePropertyValues();
            propsValues.addPropertyValue("prop1", value1);
            propsValues.addPropertyValue("prop1", value1);
            bw.setPropertyValues(propValues);
            Ollie

            Comment


            • #7
              Thanks for the tip Ollie!

              Comment

              Working...
              X