Results 1 to 7 of 7

Thread: trying to implement AOP with Spring

  1. #1
    Join Date
    Aug 2004
    Posts
    10

    Default trying to implement AOP with Spring

    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.

    Code:
        <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>


    Code:
    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;

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    musch, can you show your client code, and the log result.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Posts
    10

    Default

    in WebWork Action
    Code:
    			ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext&#40;ServletActionContext.getServletContext&#40;&#41;&#41;;
    			AgenciaServiceImpl imp = &#40;AgenciaServiceImpl&#41; ctx.getBean&#40;"agenciaServiceImpl"&#41;;
    			imp.gravaAgencia&#40;banco,agencia&#41;;
    Code:
    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]

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    You must retrieve the advised bean in your code
    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.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5
    Join Date
    Aug 2004
    Posts
    109

    Default

    I think you are confusing something here

    Code:
    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:
    Code:
            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]
    Thanks,
    Alex.

  6. #6
    Join Date
    Aug 2004
    Posts
    10

    Default

    I´m a idiot !!! :oops:

    but u forget indicate interface
    final solution
    Code:
    			ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext&#40;ServletActionContext.getServletContext&#40;&#41;&#41;;
    			IAgenciaService imp = &#40;IAgenciaService&#41; ctx.getBean&#40;"agenciaProxyBean"&#41;;
    			imp.gravaAgencia&#40;banco,agencia&#41;;
    thanks

  7. #7
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    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).
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. Spring MVC Web Framework versus Struts
    By biguniverse in forum Web Flow
    Replies: 27
    Last Post: Aug 29th, 2012, 03:57 AM
  2. Replies: 5
    Last Post: Aug 9th, 2008, 05:30 AM
  3. Replies: 3
    Last Post: Apr 14th, 2005, 11:59 PM
  4. Replies: 2
    Last Post: Mar 21st, 2005, 11:42 PM
  5. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM

Posting Permissions

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