Results 1 to 3 of 3

Thread: JdkDynamicAopProxy overwrites equals()

  1. #1
    Join Date
    Nov 2004
    Posts
    2

    Default JdkDynamicAopProxy overwrites equals()

    First, thanks everybody for that springframework.
    I am converting an old project to test AOP, and stumbled about an error enhancing my POJOs. I tried to build lazy-loading and caching aspects and overwrote equals() and hashCode(). But JdkDynamicAopProxy overwrites equals() as well (to compare it to other JdkDynamicAopProxy) thus breaking the POJOs.
    To me the question is wether it is necessary to overwrite equals while passing through hashCode(), toString(), etc.. It breaks existing code and results in strange behaviour with Maps, e.g. two keys(Proxies) are not equal() but might return the same value(via the proxied.hashCode()).
    Or is there a thing i missed (besides rewriting my code :wink: )

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Equality is deemed to be meet object and all advisors equal. That is the same target object with different advice will not be considered equal. If your advisors/advice are a shared instance or equal, equal should behave as you expect. If it doesn't, please give a simple example.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Nov 2004
    Posts
    2

    Default Proxy example

    Thank you for your fast reply, i will try to give a short example. (By the way, i am a newbie to AOP :wink: )

    Let's say i have a database with cars and persons. Every car has an owner (person), and maybe other relations. So when i load a car from the database, i do not query for the complete owner but create an OwnerProxy just filled with its ID. When an owner method other than getId (or related, like hashCode(), equals()) is invoked, the object reconnects to the database and fills up the remainding values.
    So, there are arbitrary OwnerProxies enclosing the same owner-entity. Do two cars belong to the same owner? car1.getOwner().equals(car2.getOwner()) works for the ID, not the OwnerProxy, because every query creates a new prototype of the OwnerProxy.


    Code:
      <!-- Advisor pointcut definition for before advice   -->
       <bean id="proxyBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" singleton="false">
          <property name="pattern"><value>.*</value></property>
          <property name="advice"><ref local="proxyBeforeAdvice"/></property>
       </bean>
    
       <!-- Advice classes -->
       <bean id="proxyBeforeAdvice" class="test.advice.proxying.ProxyBeforeAdvice" singleton="false"/>
    
      <bean id="owner" class="org.springframework.aop.framework.ProxyFactoryBean">
       	<property name="proxyInterfaces"><value>test.Owner</value></property>
            <property name="singleton"><value>false</value></property>
            <property name="interceptorNames">
                <list>
                	<value>proxyBeforeAdvisor</value>
    	        <value>_owner</value>
                </list>
            </property>
       </bean>
    
       <bean id="_owner" class="test.OwnerImpl" singleton="false" />
    All Objects, advisors and advice are prototypes, because I mix 'loaded ' state into the owner:
    Code:
    public class ProxyBeforeAdvice
        implements MethodBeforeAdvice &#123;
    
        public ProxyBeforeAdvice&#40;&#41; &#123;
        &#125;
    
        private boolean loaded = false;
    
        public void before&#40;Method method, Object&#91;&#93; attributes, Object target&#41; throws Throwable &#123;
            String name = method.getName&#40;&#41;;
            if &#40;"getId".equals&#40;name&#41;&#41; &#123;
    		// no need to reload
            &#125;
    	....
    	&#125;
    	else if &#40;!loaded&#41; &#123;
                	reconnect&#40;&#40;Entity&#41;target&#41;;
            &#125;
       &#125;

Similar Threads

  1. Replies: 7
    Last Post: Dec 22nd, 2011, 05:11 AM
  2. Replies: 2
    Last Post: Oct 17th, 2005, 08:41 PM
  3. Replies: 0
    Last Post: Aug 16th, 2005, 08:33 AM

Posting Permissions

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