PDA

View Full Version : trying to implement AOP with Spring



musch
Sep 2nd, 2004, 01:03 PM
I try to implement AOP with Spring, but itīs not work, my advise is not call, below is my code and my configuration.

agenciaServiceImpl is a Pojo, and work fine.



<bean id="agenciaServiceImpl" class="com.teste.dao.AgenciaServiceImpl">
<property name="agenciaDao"><ref bean="agenciaDao"/></property>
<property name="bancoDao"><ref bean="bancoDao"/></property>
</bean>

<bean id="makeDaoLog" class="com.teste.util.MakeDaoLog" />

<bean id="agenciaProxyBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.teste.dao.IAgenciaService</value>
</property>
<property name="interceptorNames">
<list>
<value>makeDaoLog</value>
<value>agenciaServiceImpl</value>
</list>
</property>
</bean>





package com.teste.util;

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class MakeDaoLog implements MethodBeforeAdvice &#123;

public void before&#40;Method m, Object&#91;&#93; args, Object target&#41; throws Throwable &#123;
System.out.println&#40;" pass here "&#41;;
&#125;

&#125;

irbouho
Sep 2nd, 2004, 01:37 PM
musch, can you show your client code, and the log result.

musch
Sep 2nd, 2004, 01:51 PM
in WebWork Action


ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContex t&#40;ServletActionContext.getServletContext&#40;&#41;&#41;;
AgenciaServiceImpl imp = &#40;AgenciaServiceImpl&#41; ctx.getBean&#40;"agenciaServiceImpl"&#41;;
imp.gravaAgencia&#40;banco,agencia&#41;;




public class AgenciaServiceImpl implements IAgenciaService &#123;

private AgenciaDaoImpl agenciaDao;
private BancoDaoImpl bancoDao;

public void gravaAgencia&#40;Banco banco, Agencia agencia&#41; &#123;
bancoDao.create&#40;banco&#41;;
agenciaDao.create&#40;agencia&#41;;
&#125;

public AgenciaDaoImpl getAgenciaDao&#40;&#41; &#123;
return agenciaDao;
&#125;
public void setAgenciaDao&#40;AgenciaDaoImpl agenciaDao&#41; &#123;
this.agenciaDao = agenciaDao;
&#125;
public BancoDaoImpl getBancoDao&#40;&#41; &#123;
return bancoDao;
&#125;
public void setBancoDao&#40;BancoDaoImpl bancoDao&#41; &#123;
this.bancoDao = bancoDao;
&#125;
&#125;

and none error is showing in TOMCAT console[/code]

irbouho
Sep 2nd, 2004, 02:10 PM
You must retrieve the advised bean in your code


AgenciaServiceImpl imp = &#40;AgenciaServiceImpl&#41; ctx.getBean&#40;"agenciaProxyBean"&#41;;
imp.gravaAgencia&#40;banco,agencia&#41;;


"agenciaServiceImpl" is your base bean, if you get it from applicationContext, Spring will return an instance of your POJO.

"agenciaProxyBean" is "agenciaServiceImpl" wrapped by your advisor, so when you call methods of your bean, the advisor will kick and intercept the method call.

bpolka
Sep 2nd, 2004, 02:11 PM
I think you are confusing something here



AgenciaServiceImpl imp = &#40;AgenciaServiceImpl&#41; ctx.getBean&#40;"agenciaServiceImpl"&#41;;
imp.gravaAgencia&#40;banco,agencia&#41;;


You understand that this will get the bean that is not proxied at all, hence AOP is not working. What you probably mean to do is:


AgenciaServiceImpl imp = &#40;AgenciaServiceImpl&#41; ctx.getBean&#40;"agenciaProxyBean"&#41;;
imp.gravaAgencia&#40;banco,agencia&#41;;


note the bean name I retrieve from the application context has changed, now you are retrieving proxied bean that will apply interceptor code in the chain you specified.

HTH

[/code]

musch
Sep 2nd, 2004, 02:34 PM
Iīm a idiot !!! :oops:

but u forget indicate interface
final solution


ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContex t&#40;ServletActionContext.getServletContext&#40;&#41;&#41;;
IAgenciaService imp = &#40;IAgenciaService&#41; ctx.getBean&#40;"agenciaProxyBean"&#41;;
imp.gravaAgencia&#40;banco,agencia&#41;;


thanks

Colin Sampaleanu
Sep 2nd, 2004, 02:41 PM
Also, consider wrapping the target as an inner bean, with the proxy, so this sort of mistake can't happen. Generally, I only recommend keeping the target as a separate top-level bean if more than one other bean needs to refer to it (the unwrapped target).