Announcement Announcement Module
Collapse
No announcement yet.
Help with BeanNameAutoProxyCreator Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with BeanNameAutoProxyCreator

    I am trying to use an interceptor and the BeanNameAutoProxyCreator. I can get the interceptor to work on a class using the ProxyFactoryBean but not if it is sepcified for the BeanNameAutoProxyCreator. I have spring 2.0.4, cglib 2.1.3, asm 1.5.3 in my classpath. What else am I not doing? Thanks much.

    Here is my spring configuration.
    Code:
    <beans>
    
      <bean id="SpcfMethodLogInterceptor" class="com.intuit.spc.foundations.componentSpecific.activityLog.SpcfMethodLogInterceptor"/>
     
      <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
        <list>
          <value>comFoo2</value>
        </list>
        </property>
        <property name="interceptorNames">
          <list>
            <value>SpcfMethodLogInterceptor</value>
          </list>
        </property>
        <property name="proxyTargetClass">
          <value>true</value>
        </property>
      </bean>
      
      <bean id="comFoo2" class="com.intuit.spc.foundations.componentSpecific.activityLog.ComponentFoo">
        <property name="default">
          <value>false</value>
        </property>
      </bean> 
    
      <bean id="comFoo3" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target">
          <bean class="com.intuit.spc.foundations.componentSpecific.activityLog.ComponentFoo">
            <property name="default"><value>true</value></property>
          </bean>
        </property>
        <property name="interceptorNames">
          <list>
            <value>SpcfMethodLogInterceptor</value>
          </list>
        </property>
        <property name="proxyTargetClass">
            <value>true</value>
          </property>
      </bean>
    
    </beans>
    The following is my interceptor. This gets called ok for the ProxyFactoryBean.
    Code:
    import java.lang.reflect.Method;
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    
    /**
     * The Interceptor to log method api calls
     */
    public class SpcfMethodLogInterceptor implements MethodInterceptor
    {
    	/**
    	 * The logger to use for Method logging
    	 */
      private final SpcfLogger logger = SpcfLogManager.getLogger();
    
      /**
       * Method which will be called when the method is called
       * @param methodInvocation the method called
       * @return Object the return value from the method call
       * @throws Throwable the exception if the method throws an exception
       */
      public Object invoke(MethodInvocation methodInvocation) throws Throwable
      {
    	  Method m = methodInvocation.getMethod();
    	  Object[] argArray = methodInvocation.getArguments();
    	  
    	  logger.info("Beginning method: " + m.getDeclaringClass() + "::" + m.getName());
    	  for (Object arg : argArray)
    	  {
    		  logger.info("\tmethod arg: " + arg.getClass().getCanonicalName() + "::" + arg.toString());
    	  }
    	  try
    	  {
    		  Object retVal = methodInvocation.proceed();
    		  if (retVal != null)
    			  logger.info("\tmethod return value: " + retVal.getClass().getCanonicalName() + "::" + retVal.toString());
    		  return retVal;
    	  }
    	  finally
    	  {
    		  logger.info("Ending method: "  + m.getDeclaringClass() + "::" + m.getName());
    		  }
      }
    
    }
    Here is a junit test I am using to try to get this working. The testActivityLog3 gets proxied fine, but the testActivityLog1 code just calls the methods directly.
    Code:
    	public void testActivityLog1()
    	{
    		mLog.fatal("just log something1");
    		ISpcfComponentFactory cf = SpcfTestUtil.getComponentFactoryInstance();
    		ComponentFoo foo = cf.<ComponentFoo>getComponent("comFoo2");
    		assertNotNull(foo);	
    		foo.isDefault();
    		Boolean bDefault = foo.getDefault();
    		foo.setDefault(bDefault);
    		foo.logMessage("logComFoo2");
    	}
    
    	/**
    	 *
    	 */
    	public void testActivityLog3()
    	{
    		mLog.fatal("just log something3");
    		ISpcfComponentFactory cf = SpcfTestUtil.getComponentFactoryInstance();
    		ComponentFoo foo = cf.<ComponentFoo>getComponent("comFoo3");
    		assertNotNull(foo);	
    		foo.isDefault();
    		Boolean bDefault = foo.getDefault();
    		foo.setDefault(bDefault);
    		foo.logMessage("logComFoo3");
    		}
    And here is the class I am trying to set up the proxy for.
    Code:
    /**
     * test class
     */
    public class ComponentFoo implements ISpcfComponent
    {	
    	/**
    	 * private boolean variable
    	 */
    	private boolean mDefaultComponent = true;
    	
    	/**
    	 * get a boolean value
    	 * @return boolean value
    	 */
    	public boolean getDefault()
    	{
    		return mDefaultComponent;
    	}
    	
    	/**
    	 * set a boolean value
    	 * @param defaultComponent a boolean value
    	 */
    	public void setDefault(boolean defaultComponent)
    	{
    		mDefaultComponent = defaultComponent;
    	}	
    	
    	/**
    	 * implement method of ISpcfComponent
    	 * @return boolean value
    	 */
    	public boolean isDefault()
    	{
    		return mDefaultComponent;
    	}
    	
    	/**
    	 * log a string
    	 * @param message a string value
    	 */
    	public void logMessage(String message)
    	{
    		SpcfLogManager.getLogger().fatal(message);
    	}	
    	
    }
Working...
X