title: Spring @AspectJ: Advice not applied
body: <p>I'm trying to weave in code before the call of <code>start();</code></p>
  
  <p>This is the TestClass I want to advice:</p>
  
  <pre><code>package com.test;
  
  public class TestClass {
  
      public static void main(String[] args) {
  
          new TestClass().start();
      }
      private void start() {
  
          System.out.println("Test started");
      }
  }
  </code></pre>
  
  <p>This is the aspect including the advice:</p>
  
  <pre><code>package com.test;
  
  import org.aspectj.lang.JoinPoint;
  import org.aspectj.lang.annotation.Aspect;
  import org.aspectj.lang.annotation.Before;
  import org.springframework.stereotype.Component;
  
  @Aspect
  @Component
  public class LogAspect {
  
      @Before("call(void com.test.TestClass.start())")
      public void logBefore(JoinPoint joinPoint) {
          System.out.println("logBefore() is running");
      }
  
  }
  </code></pre>
  
  <p>This is my spring.xml:</p>
  
  <pre><code>    &lt;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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "&gt;
  
  &lt;aop:aspectj-autoproxy/&gt;
  &lt;context:annotation-config/&gt;
  &lt;context:component-scan base-package="com.test"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>I have also tried to name the beans explicitly in the XML like this:</p>
  
  <pre><code>&lt;aop:aspectj-autoproxy/&gt;
  &lt;bean id="test" class="com.test.TestClass" /&gt;
  &lt;bean id="aspect" class="com.test.LogAspect" /&gt;
  </code></pre>
  
  <p>This is my project setup (I'm using the Spring Tool Suite version 3.1.0):</p>
  
  <p><img src="https://i.stack.imgur.com/ch5Dz.png" alt="Project setup"></p>
  
  <p>The result is that TestClass.start is called just fine, but the advice is not applied.</p>
  
  <p>What do I have to change so the advice is applied?</p>
  
  <p>Thanks.</p>
  
  <p><strong>Edit</strong>: I finally got it to work:</p>
  
  <p>After editing my code according to your suggestions I had a look at this <a href="http://www.mkyong.com/spring3/spring-aop-aspectj-annotation-example/" rel="nofollow noreferrer">tutorial</a>. This led me to let my TestClass <strong>implement an interface</strong>. And that fixed the problem.</p>
  
  <p>Here is my final setup:</p>
  
  <p><img src="https://i.stack.imgur.com/bPV39.png" alt="final setup"></p>
  
  <p>The TestClass including its interface:</p>
  
  <pre><code>package com.test;
  
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  interface TestClass {
      void start();
  }
  
  public class TestClassImpl implements TestClass {
  
      public static void main(String[] args) {
          ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
                  "Spring-Config.xml");
  
          TestClass t1 = (TestClass) appContext.getBean("myTest");
          t1.start();
      }
  
      @Override
      public void start() {
          System.out.println("test");
      }
  }
  </code></pre>
  
  <p>The aspect:</p>
  
  <pre><code>package com.test;
  
  import org.aspectj.lang.JoinPoint;
  import org.aspectj.lang.annotation.Aspect;
  import org.aspectj.lang.annotation.Before;
  
  @Aspect
  public class LogAspect {
      @Before("execution(void com.test.TestClass.start(..))")
      public void logBefore(JoinPoint joinPoint) {
  
          System.out.println("logging before "
                  + joinPoint.getSignature().getName());
      }
  }
  </code></pre>
  
  <p>And the Spring-Config.xml</p>
  
  <pre><code>&lt;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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "&gt;
  
      &lt;aop:aspectj-autoproxy/&gt;    
      &lt;bean id="myTest" class="com.test.TestClassImpl" /&gt;
      &lt;bean id="logAspect" class="com.test.LogAspect" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Thanks for your help.</p>