Announcement Announcement Module
Collapse
No announcement yet.
Help Creating non singleton using ProxyFactoryBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help Creating non singleton using ProxyFactoryBean

    Hi

    I am trying to use ProxyFactoryBean to create non singleton instances of bean. But it seems to ignore the non singleton flag and i get the handle to the same bean every time i call getBean ..

    Am i doing something wrong.

    Here is my spring configuration file.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="bean1" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="singleton"><value>false</value></property>
    <property name="proxyTargetClass"><value>true</value></property>
    <property name="proxyInterfaces">
    <value>com.performixtech.test.IBean1</value>
    </property>

    <property name="target">
    <ref local="beanTarget1"/>
    </property>

    </bean>

    <bean id="beanTarget1" class="com.performixtech.test.Bean1" singleton="false">
    <property name="bean2">
    <ref local="bean2id"/>
    </property>
    <property name="bean3">
    <ref local="bean3id"/>
    </property>

    </bean>

    <bean id="bean2id" class="com.performixtech.test.Bean2" singleton="false">

    </bean>

    <bean id="bean3id" class="com.performixtech.test.Bean3" singleton="false">

    </bean>


    </beans>


    My test code


    public class SpringTry
    {

    public static void main(String[] args)
    {
    ClassPathXmlApplicationContext appContext =
    new ClassPathXmlApplicationContext(
    new String[] {"spring-config.xml"});

    BeanFactory factory = (BeanFactory) appContext;

    // Try 1
    {
    IBean1 bean1=(IBean1)factory.getBean("bean1");
    Bean2 bean2=bean1.getBean2();
    bean2.setBean2Name("bean21");
    System.out.println(bean2.getBean2Name());
    bean1=null;
    }
    // Try 2
    {
    Bean2 bean2=(Bean2)factory.getBean("bean2id");
    bean2.setBean2Name("abc");
    System.out.println(bean2.getBean2Name());
    }

    // Try 3
    {
    IBean1 bean1=(IBean1)factory.getBean("bean1");
    Bean2 bean2=bean1.getBean2();
    System.out.println(bean2.getBean2Name());
    }

    }
    }


    It prints

    bean21
    abc
    bean21 // this should have been null as i am not setting any thing..

    Thanks
    Kevin

  • #2
    Kevin,

    In the case of FactoryBeans, the singleton attribute governs the lifecycle of the FactoryBean itself, not the bean it is creating. For ProxyFactoryBean you can indicate whether it should create a singleton or not using the singleton property (setSingleton(false)).

    Rob

    Comment


    • #3
      Hi Rob,

      Thanks for answering my queries ...
      But i am not sure what i need to do to create non singleton target objects using proxy.

      Isn't the highlighted part of the mapping sufficient to create non singletons.

      I am still getting back the handle to the same bean inspite of setting the value to false.

      What am i doing wrong or is it a bug in the release i have..

      I am using 1.1.2 version of SF

      Thanks
      Kevin.

      <bean id="bean1" class="org.springframework.aop.framework.ProxyFact oryBean">
      <property name="singleton"><value>false</value></property> <property name="proxyInterfaces">
      <value>com.performixtech.test.IBean1</value>
      </property>

      <property name="target">
      <ref local="beanTarget1"/>
      </property>

      </bean>


      Originally posted by robh
      Kevin,

      In the case of FactoryBeans, the singleton attribute governs the lifecycle of the FactoryBean itself, not the bean it is creating. For ProxyFactoryBean you can indicate whether it should create a singleton or not using the singleton property (setSingleton(false)).

      Rob

      Comment


      • #4
        Getting rid of the target and adding interceptorNames made it work

        Thanks
        Navin

        <beans>
        <bean id="bean1" class="org.springframework.aop.framework.ProxyFact oryBean">
        <property name="singleton"><value>false</value></property>
        <property name="proxyInterfaces">
        <value>com.performixtech.test.IBean1</value>
        </property>
        <!--
        <property name="target">
        <ref local="beanTarget1"/>
        </property>
        -->
        <property name="interceptorNames">
        <list>
        <value>beanTarget1</value>
        </list>
        </property>

        </bean>

        Originally posted by kevin_405
        Hi Rob,

        Thanks for answering my queries ...
        But i am not sure what i need to do to create non singleton target objects using proxy.

        Isn't the highlighted part of the mapping sufficient to create non singletons.

        I am still getting back the handle to the same bean inspite of setting the value to false.

        What am i doing wrong or is it a bug in the release i have..

        I am using 1.1.2 version of SF

        Thanks
        Kevin.

        <bean id="bean1" class="org.springframework.aop.framework.ProxyFact oryBean">
        <property name="singleton"><value>false</value></property> <property name="proxyInterfaces">
        <value>com.performixtech.test.IBean1</value>
        </property>

        <property name="target">
        <ref local="beanTarget1"/>
        </property>

        </bean>


        Originally posted by robh
        Kevin,

        In the case of FactoryBeans, the singleton attribute governs the lifecycle of the FactoryBean itself, not the bean it is creating. For ProxyFactoryBean you can indicate whether it should create a singleton or not using the singleton property (setSingleton(false)).

        Rob

        Comment

        Working...
        X