title: spring mvc + spring aop + aspectj
body: <p>I struggle to use aspect in Spring MVC project.</p>
  
  <p>Method that is a pointcut is running fine, but without advise.</p>
  
  <p>Here is class, that starts whole spring boot and that is root of spring context:</p>
  
  <pre><code>@Lazy
  @SpringBootApplication
  @EnableAspectJAutoProxy(proxyTargetClass=true)
  @Configuration
  public class MainSpringBootClass{
      public static void main(String[] args)
      {
          SpringApplication.run(MainSpringBootClass.class, args);
      }
  }
  </code></pre>
  
  <p>Here is class with method, that is pointcut.</p>
  
  <pre><code>@Component
  @Log
  @Aspect
  @EnableAspectJAutoProxy(proxyTargetClass=true)
  public class MyExampleClass
  {
      public void example()
      {
          System.out.println("example");
      }
  }
  </code></pre>
  
  <p>And here is my aspect:</p>
  
  <pre><code>@Aspect
  @Component
  @EnableAspectJAutoProxy(proxyTargetClass=true)
  public class MyAspect implements MethodBeforeAdvice
  {
      @Pointcut("execution(* com.example.MyExampleClass.example())")
      public void asd()
      {
          // pointcut
      }
  
      @Before("asd()")
      public void login()
      {
          System.out.println("im am logging in");
      }
  
      @Before("execution(* com.example.MyExampleClass.example())")
      public void login2()
      {
          System.out.println("im am logging in2");
      }
  
      @Override
      public void before(Method method, Object[] args, Object target) throws Throwable
      {
          System.out.println("aop before");
      }
  }
  </code></pre>
  
  <p>And here is my controller:</p>
  
  <pre><code>@RestController
  @EnableAspectJAutoProxy(proxyTargetClass=true)
  public class MyExampleController
  {
      private final MyExampleClass myExampleClass;
  
      @Inject
      public AdController(MyExampleClass myExampleClass)
      {
          this.myExampleClass = myExampleClass;
      }
  
      @RequestMapping("/")
      public String index()
      {
          myExampleClass.example();
  
          return "x";
      }
  }
  </code></pre>
  
  <p>As You can see, I have been trying to bruteforce correct result with annotations.
  I have also seen on some website, that I need specific dependencies, so here are mine (pasting only those, related to aspects):</p>
  
  <pre><code>compile 'org.springframework:spring-aop:+'
  compile 'org.aspectj:aspectjrt:+'
  compile 'org.aspectj:aspectjweaver:+'
  compile 'cglib:cglib:+'
  compile 'cglib:cglib-nodep:+'
  </code></pre>
  
  <p>All dependencies have been successfully downloaded, project compiles and runs fine.
  When I hit localhost:8080 then I see returned value "x", and inside logs I see "example".
  However, I do not see any advices from spring aop nor aspectj - what am I doing wrong?</p>
  
  <p>I am just using this project as sandbox to learn aspects, so I would be eager to learn, how to do it with each of Spring AOP and AspectJ.
  The most important thing for me is to do it all without XML.</p>
  
  <p>EDIT:
  I have added simple constructor to MyAspect with println to check, if it is created (as it is normal spring bean with @Component after all) and it does - it is correctly created by spring.</p>
  
  <p>EDIT 2:
  IntelliJ IDEA tells me about methods login and login2: "This advice advices no method", but at the same time, I am able to jump (with control-click) from string, that is value in annotations to correct implementations.</p>