Results 1 to 5 of 5

Thread: Multiple instances of proxied bean

  1. #1
    Join Date
    Nov 2006
    Posts
    16

    Question Multiple instances of proxied bean

    Hey,

    I have a small situation with AOP. I am trying to create a proxy for one of my beans which incidentally is a shared object that we are wiring to two other objects. Now, from what I have noticed, these two objects are getting two separate instances of the shared object even though the object itself has been defined with scope of singleton and the proxy being declared as singleton.

    Is there something I am missing or is this the intended behavior of spring. I am using 2.0.1

    Thanks in anticipation to all the geniuses.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    It should have just one instance, can we see the configuration?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Nov 2006
    Posts
    16

    Default

    Quote Originally Posted by mdeinum View Post
    It should have just one instance, can we see the configuration?
    Sure.... here is the relevant portions
    Code:
    <bean id="sampleInterceptor" class="com.test.SampleInterceptor"/>
    
    <bean id="nameMatchPointCut" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
    	<property name="mappedName">
    		<value>get</value>
    	</property>
    	<property name="advice">
    		<ref bean="sampleInterceptor"/>
    	</property>
    </bean>
    
    <bean id="sampleObjectTarget" class="com.test.SampleObjectImpl" scope="singleton" />
    	
    < bean id="sampeObect" class="org.springframework.aop.framework.ProxyFactoryBean" scope="singleton">
    	<property name="proxyInterfaces">
    		<value>com.test.SampleObject</value>
    	</property>
    	<property name="target" ref="sampleObjectTarget"/>
    	<property name="interceptorNames">
    		<list>
    			<value>nameMatchPointCut</value>
    		</list>
    	</property>
    </bean>
    The one thing I thought of was to try with
    Code:
    <bean id="sampleInterceptor" class="com.test.SampleInterceptor" scope="singleton"/>
    but did not think this would make a difference.

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    What makes you think they are actually different instances? I would check the target to make sure you are seeing the actual object.

  5. #5
    Join Date
    Nov 2006
    Posts
    16

    Default

    Quote Originally Posted by karldmoore View Post
    What makes you think they are actually different instances? I would check the target to make sure you are seeing the actual object.
    I say they are different objects because when i print the object to the log... say using logger.debug(sampleObect). It gives me different references at the two locations where it is supposed to be one. Plus, when I did an obj1==obj2 check it fails. where obj1 and obj2 are the wired object at first and second locations.

    The other test I did was update the object in the first location and checked the value in the second.

    However, when I print the beanName in either of the locations it prints the name of the same target bean.

Posting Permissions

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