Announcement Announcement Module
Collapse
No announcement yet.
trying to implement AOP with Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • trying to implement AOP with Spring

    I try to implement AOP with Spring, but its 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
    musch, can you show your client code, and the log result.

    Comment


    • #3
      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]

      Comment


      • #4
        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.

        Comment


        • #5
          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]

          Comment


          • #6
            Im 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

            Comment


            • #7
              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).

              Comment

              Working...
              X