Thanks a lot for having a look.
I have not used github before , i will paste the code here - i have created a small use case for this
This is the output of teh program
Get called from AccountDAO
creating proxy now
accountcom.test.entity.account.Account@e45076
Before advice called
com.test.entity.account.Account@fd68b1 :old a value100
com.test.entity.account.Account@10382a9:new a value0
That is the setter value has been set on the old objectrather than teh new one.
This is my spring xml
Code:
<bean id="accountDao" class="com.test.dao.account.AccountDAO">
<property name="advisor" ref="settersAdvisor" />
</bean>
<bean id="swapableAroundAdvice" class="com.test.framework.SwappableMethodInvoker"/>
<bean id="settersAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<!-- <ref local="swapableBeforeAdvice" /> -->
<ref local="swapableAroundAdvice" />
</property>
<property name="patterns">
<list>
<value>com.test.entity.account.*.set.*</value>
<value>com.test.entity.account.*.increment.*</value>
</list>
</property>
</bean>
This is my account class
[code]
public class Account implements IAccount {
private boolean writable;
private int a;
private String b;
public Account(int a, String b) {
super();
this.a = a;
this.b = b;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public boolean isWritable() {
return writable;
}
public void setWritable(boolean writable) {
this.writable = writable;
}
public void incrementa() {
this.a = a + 1;
// return this.a;
}
@Override
public void asset() {
// TODO Auto-generated method stub
}
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
[code]
This is my account dao
Code:
public class AccountDAO {
private Advisor advisor;
public IAccount get(long id) {
System.out.println("Get called from AccountDAO");
IAccount a = new Account(0, "suji");
ProxyFactory pf = new ProxyFactory();
pf.setExposeProxy(true);
pf.addInterface(IAccount.class);
// pf.setTargetSource(new SwappableTargetSource(a));
pf.setTargetSource(new HotSwappableTargetSource(a));
pf.addAdvisor(advisor);
System.out.println("creating proxy now");
return (IAccount) pf.getProxy();
}
public void setAdvisor(Advisor advisor) {
this.advisor = advisor;
}
This is my swappable method invoker
Code:
public class SwappableMethodInvoker implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// TODO Auto-generated method stub
System.out.println("Before advice called");
Account clonedobj = (Account) ((Account) invocation.getThis()).clone();
((HotSwappableTargetSource) ((Advised) AopContext.currentProxy())
.getTargetSource()).swap(clonedobj);
/*
If i uncomment these lines everything works fine
return invocation.getMethod().invoke(clonedobj,
invocation.getArguments()); */
return invocation.proceed();
}
}
An this is my main program
Code:
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:META-INF/spring/aop-poc-module-context.xml");
AccountDAO accountDao = (AccountDAO) ctx.getBean("accountDao");
IAccount account = accountDao.get(1);
System.out.println("account" + account);
IAccount accountOld = (Account) ((HotSwappableTargetSource) ((Advised) account)
.getTargetSource()).getTarget();
account.setA(100);
IAccount accountnew = (Account) ((HotSwappableTargetSource) ((Advised) account)
.getTargetSource()).getTarget();
System.out.println(accountOld + " :old a value" + accountOld.getA());
System.out.println(accountnew + ":new a value" + accountnew.getA());
}