Announcement Announcement Module
Collapse
No announcement yet.
ProxyBeanFactory and setting bean properties Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ProxyBeanFactory and setting bean properties

    Hello: I'm going up the learning curve for Spring, and have run into a slight road-block for something I want to do.

    Here's an XML configuration file which captures the essence of my problem (interceptors not shown). I have a counter class (test.CounterImpl), which I wish to proxy so I can apply interceptors to its interface (test.Counter). No problems there, everything works.

    But, I then want to create a proxied Counter bean, and set it's name to "A" using Counter.setName(String), before using it later on in the configuration. I can't find a way to do this: the example code shown below fails because the "parent" gets the factory, not a new instance of the proxied Counter.

    I've dug through the code, documentation, and googled, and I don't see a solution to this. I'm considering writing a DelegateFactoryBean which would take a <ref bean="counter"> while allowing properties to be set on the delegate. Any suggestions appreciated.

    Thanks...

    Simon.


    Code:
    package test;
    public interface Counter &#123;
    
        public void setName&#40;String name&#41;;
    
        public void count&#40;&#41;;
    
    &#125;
    
    <beans>
        <bean id="counter" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="proxyInterfaces">
                <value>test.Counter</value>
            </property>
            <property name="target">
              <ref bean="counterImpl"/>
            </property>
            <property name="singleton">
                <value>false</value>
            </property>
        </bean>
    
        <bean id="counterImpl" class="test.CounterImpl" singleton="false" />
    
        <bean id="counterA" parent="counter">
          <property name="name" value="A"/>
        </bean>
    </beans>
    
    
    org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'counterA' defined in resource loaded through InputStream&#58; Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException&#58; Invalid property 'name' of bean class &#91;org.springframework.aop.framework.ProxyFactoryBean&#93;&#58; Bean property 'name' is not writable or has an invalid setter method&#58; Does the parameter type of the setter match the return type of the getter?
    org.springframework.beans.NotWritablePropertyException&#58; Invalid property 'name' of bean class &#91;org.springframework.aop.framework.ProxyFactoryBean&#93;&#58; Bean property 'name' is not writable or has an invalid setter method&#58; Does the parameter type of the setter match the return type of the getter?
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValue&#40;BeanWrapperImpl.java&#58;796&#41;
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValue&#40;BeanWrapperImpl.java&#58;716&#41;
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValue&#40;BeanWrapperImpl.java&#58;850&#41;
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValues&#40;BeanWrapperImpl.java&#58;877&#41;
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValues&#40;BeanWrapperImpl.java&#58;866&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues&#40;AbstractAutowireCapableBeanFactory.java&#58;905&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean&#40;AbstractAutowireCapableBeanFactory.java&#58;707&#41;
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapableBeanFactory.java&#58;316&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;222&#41;
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;146&#41;
    	at test.CounterImpl.main&#40;CounterImpl.java&#58;26&#41;

  • #2
    I've got to hand it to you guys: Spring is powerful. A bit more hacking around and reading the source code got me to this, which does exactly what I want!

    Code:
        <bean id="counterA" factory-bean="&amp;counter" factory-method="getObject">
          <property name="name" value="A"/>
        </bean>
    Keep up the good work!

    Comment

    Working...
    X