Announcement Announcement Module
Collapse
No announcement yet.
simple aop in spring application --> problem ! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • simple aop in spring application --> problem !

    Hi,

    I have a main class that instantiates a method in BusinessLogic class. I have three advisors to print me log messages before and after the method is called. It works fine if use an interface and then make my BusinessLogic implement the interface. But now im trying to just use BusinessLogic.java without any interface and i have configured my springconfig.xml. I am getting the following the exception when i run the code.

    The code is as follows :

    Code:
    public class BusinessLogic
    {
        
    	public void foo() 
         {
           System.out.println("Inside BusinessLogic.foo()");
         }
         
         public void bar() throws BusinessLogicException
         {
            System.out.println("Inside BusinessLogic.bar()");
            
         }
    }
    Code:
    import org.springframework.aop.ThrowsAdvice;
    import java.lang.reflect.Method;
    
    public class LoggingThrowsAdvice implements ThrowsAdvice
    {
       public void afterThrowing(Method method, Object[] args, Object target, Throwable subclass)
       {
          System.out.println("Logging that a " + subclass + "Exception was thrown.");
       }
    }
    
    ****************************************************
    
    
    import java.lang.reflect.Method;
    import org.springframework.aop.AfterReturningAdvice;
    
    public class TracingAfterAdvice implements AfterReturningAdvice
    {
       public void afterReturning(Object object, Method m, Object[] args, Object target) throws Throwable
       {
           System.out.println("Hello world! (by " + this.getClass().getName() + ")");
       }
    }
    
    
    *******************************************************
    
    import java.lang.reflect.Method;
    import org.springframework.aop.MethodBeforeAdvice;
    
    public class TracingBeforeAdvice implements MethodBeforeAdvice
    {
       public void before(Method m, Object[] args, Object target) throws Throwable
       {
          System.out.println("Hello world! (by " + this.getClass().getName() + ")");
          System.out.println("******"+m.getDeclaringClass());
         
       }
    }
    I have done my configurations in the springconfig.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC  "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
    	<!--CONFIG-->
    	
    	
    	
    	<bean id="beanTarget1" class="org.springframework.aop.framework.ProxyFactoryBean">
    		
    		<property name="advice">
    			<ref local="theTracingBeforeAdvice"/>
    		</property>
    		<property name="target">
    			<ref local="beanTarget"/>
    		</property>
    		
    		<property name="interceptorNames">
    			<list>
    				<value>tracingBeforeAdvisor</value>
    				<value>tracingAfterAdvisor</value>
    				<value>loggingThrowsAdvisor</value>
    			</list>
    		</property>   
    	</bean>
    
    	<!--CLASS -->
    	<bean id="beanTarget" class="src.BusinessLogic"/>    
    		
    	
    	
    	<!-- Advisor pointcut definition for before advice -->
    	<bean id="tracingBeforeAdvisor"
    	class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    		<property name="advice">
    			<ref local="theTracingBeforeAdvice"/>
    		</property>
    		<property name="pattern">
    			<value>.*</value>
    		</property>
    	</bean>
    	
    	<!-- Advisor pointcut definition for after advice -->
    	<bean id="tracingAfterAdvisor"
    	class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    		<property name="advice">
    			<ref local="theTracingAfterAdvice"/>
    		</property>
    		<property name="pattern">
    			<value>.*</value>
    		</property>
    	</bean>
    	
    	<!-- Advisor pointcut definition for throws advice -->
    	<bean id="loggingThrowsAdvisor"
    	class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    		<property name="advice">
    			<ref local="theLoggingThrowsAdvice"/>
    		</property>
    		<property name="pattern">
    			<value>.*</value>
    		</property>
    	</bean>
    
    	<!--ADVICE-->
    	<bean id="theTracingBeforeAdvice"
    	class="src.TracingBeforeAdvice"/>
    	<bean id="theTracingAfterAdvice"
    	class="src.TracingAfterAdvice"/>
    	<bean id="theLoggingThrowsAdvice"
    	class="src.LoggingThrowsAdvice"/>
    </beans>
    Code:
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    public class MainApplication
    {
       public static void main(String [] args)
       {
    
    	   
    	   
          ApplicationContext ctx
              = new FileSystemXmlApplicationContext("src/springconfig.xml");
          
          BusinessLogic testObject = (BusinessLogic) ctx.getBean("beanTarget1");
    
          //Execute the public methods of the bean
          testObject.foo();
          
          try
          {
        	  
        	testObject.bar();
           
             
          }
          //catch(BusinessLogicException ble)
          catch(Exception e)
          {
             System.out.println("Caught BusinessLogicException");
          }
       }
    }

    I get the following exception :

    Code:
    log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
    log4j:WARN Please initialize the log4j system properly.
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'beanTarget1' defined in file [D:\SDE\workspace\AOP\springLoggingAspect\src\springconfig.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'advice' of bean class [org.springframework.aop.framework.ProxyFactoryBean]: Bean property 'advice' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'advice' of bean class [org.springframework.aop.framework.ProxyFactoryBean]: Bean property 'advice' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:751)
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:608)
    	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValue(AbstractPropertyAccessor.java:49)
    	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:74)
    	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:970)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:729)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:416)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:287)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
    	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:89)
    	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:74)
    	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:65)
    	at src.MainApplication.main(MainApplication.java:15)
    Exception in thread "main"

    Can someone tell me why am i getting the following exception ? Do i always need to have an interface and have my class implement the interface. Cos my logging worked fine when i had an interface(IBusinessLogic) and my class(BusinessLogic) implementing the interface and my springconfig.xml had the following mappign,
    Code:
    bean id="businesslogicbean"
    	class="org.springframework.aop.framework.ProxyFactoryBean">
    		<property name="proxyInterfaces">
    			<value>src.IBusinessLogic</value>
    		</property>  
    		
    		<property name="target">
    			<ref local="beanTarget"/>
    		</property>
    		<property name="interceptorNames">
    			<list>
    				<value>tracingBeforeAdvisor</value>
    				<value>tracingAfterAdvisor</value>
    				<value>loggingThrowsAdvisor</value>
    			</list>
    		</property>
    	</bean>
    Can some one gimme a suggestion ? Do i always need to make use of interfaces ? My requirement is to just have a class and no interface and achieve the same type of logging using advice.
    Last edited by pradeep_scorpion; Dec 7th, 2006, 06:21 AM.

  • #2
    The exception you are getting is nothing to do with Interfaces vs Classes, you are specifying a property that does not exist.

    Remove this.
    Code:
    <property name="advice">
        <ref local="theTracingBeforeAdvice"/>
    </property>
    If you don't want to proxy interfaces, have a look at proxyTargetClass.
    http://www.springframework.org/docs/...i.html#aop-pfb

    Comment


    • #3
      Hi,

      Thanks a lot for your suggestion. Thanks for the link for creating proxy classes. I shall look into it.

      Comment


      • #4
        Hi,

        If i were to proxy my class Business logic and use all the three advices and print me log messages , wut small change do i hafto make in my springconfig.xml ? can u please suggest ?

        Comment


        • #5
          Hi,

          Thanks a lot for your inputs. I am able to proxy my class now and it is working fine.

          I added a CGLIB.jar file to my classpath and made the following changes to my springconfig.xml file
          Code:
          <bean id="beanTarget1"  class="org.springframework.aop.framework.ProxyFactoryBean">
          	<property name="proxyTargetClass"><value>true</value></property>
          	 <property name="target">
          			<ref local="beanTarget"/>
          	</property>
              <property name="interceptorNames">
                <list>
                  <value>tracingBeforeAdvisor</value>
                  <value>tracingAfterAdvisor</value>
                  <value>loggingThrowsAdvisor</value>
                </list>
              </property>
            </bean>

          Hoooooray it worked.

          Now i have just one small problem left. If u can see my mainapplication.java, i instantiate methods in BusinessLogic and the logging is done perfect. If i have to put one log message "entering into the main" using AOP, where should i do the configuration and where should i do ?

          thanks in advance

          Comment

          Working...
          X