Results 1 to 2 of 2

Thread: Spring AOP Inner Method Aspect Question

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Posts
    2

    Default Spring AOP Inner Method Aspect Question

    I have the following class:

    Code:
    public class SomeClass  {
    
        public void doSomething()  {
            logThisMessage("SomeClass wants to log");
        }
    
        public void logThisMessage(String inputStr) {}
    }
    With the following Spring xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    ">
    
        <bean id="someClassBean" class="com.loggingserviceframework.app.SomeClass"/>
    
        <bean id="loggingAspect" class="com.loggingserviceframework.aop.LoggingAspect"/>
    
        <aop:config>
          <aop:aspect ref="loggingAspect">
             <aop:pointcut id="myCutLogging" expression="execution(* logThisMessage(..))"/>
             <aop:around pointcut-ref="myCutLogging" method="log"/>
          </aop:aspect>
       </aop:config>
    
    </beans>
    My Aspect is the following:
    Code:
    public class LoggingAspect
    {
        public void log(ProceedingJoinPoint call) throws Throwable
        {
            System.out.println("Entering Aspect...");
            System.out.println(call.getArgs()[0]);
        }
    
    }
    My question is, how do I monitor the logThisMessage call? My goal is to set up an aspect that monitors every time a logThisMessage is called from all my classes which could be hundreds. I'm am very new to Spring AOP or AOP in general, so not even sure if this can be done.

    The implementing class is:
    Code:
    public class AppLauncher {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            ApplicationContext appContext = new ClassPathXmlApplicationContext(
                    new String[] { "dosomethingtest.xml" });
    
            SomeClass someClass = (SomeClass) appContext.getBean("someClassBean");
            someClass.doSomething("SomeClass wants to log");
    
        }
    
    }

  2. #2
    Join Date
    Apr 2008
    Location
    Seville, Spain
    Posts
    132

    Default

    Spring AOP is proxy based and you can't proxy yourself. Refactor logThisMessage() to a helper class or use AspectJ

    Cheers
    Jose Luis Martin
    Freelance Senior Consultant
    JDAL - Java Database Application Library

Posting Permissions

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