Announcement Announcement Module
Collapse
No announcement yet.
Spring AOP Inner Method Aspect Question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Spring AOP is proxy based and you can't proxy yourself. Refactor logThisMessage() to a helper class or use AspectJ

    Cheers

    Comment

    Working...
    X