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 {
public ProxyBeforeAdvice() {
}
private boolean loaded = false;
public void before(Method method, Object[] attributes, Object target) throws Throwable {
String name = method.getName();
if ("getId".equals(name)) {
// no need to reload
}
....
}
else if (!loaded) {
reconnect((Entity)target);
}
}