Results 1 to 4 of 4

Thread: Help Creating non singleton using ProxyFactoryBean

  1. #1
    Join Date
    Dec 2004
    Posts
    22

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    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

  3. #3
    Join Date
    Dec 2004
    Posts
    22

    Default

    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>


    Quote 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

  4. #4
    Join Date
    Dec 2004
    Posts
    22

    Default

    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>

    Quote 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>


    Quote 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

Similar Threads

  1. Replies: 4
    Last Post: Oct 5th, 2005, 11:04 AM
  2. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  3. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  4. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM
  5. DefaultAdvisorAutoProxyCreator skipping beans
    By youngm in forum Container
    Replies: 6
    Last Post: Apr 12th, 2005, 04:29 PM

Posting Permissions

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