title: Implementing annotation based Spring AspectJ (JavaConfig)
body: <p>We're trying to implement AspectJ <code>@Aspect</code> into our existing software for executing some code after a service call is made.</p>
  
  <p><strong>Note:</strong></p>
  
  <ul>
  <li>We have service interfaces and implementations being <code>@Autowired</code>
  throughout the project via rest controllers as well as other service implementations.</li>
  <li>This project is entirely a Java Configuration with no XML whatsoever.</li>
  <li>We're using Spring 4.1.2 RELEASE deployed on Tomcat 7.0.54.</li>
  </ul>
  
  <p><strong>Issue:</strong></p>
  
  <p>When we added <code>@EnableAspectJAutoProxy</code> into our main <code>@JavaConfig</code>, we experience the following exception:</p>
  
  <blockquote>
    <p>Unresolvable circular reference. </p>
  </blockquote>
  
  <p>Which fails every <code>@Autowired</code> attempt on a long list of beans.</p>
  
  <p><strong>Tried:</strong></p>
  
  <ul>
  <li>Removed the <code>@EnableAspectJAutoProxy</code> annotation which autowires everything correctly but our @Aspect never gets invoked.</li>
  <li>Added the CGLIB support in the annotation by declaring
  <code>proxytargetclass=true</code> to no avail.</li>
  <li>We've tried following this documentation directly from Spring: <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/EnableAspectJAutoProxy.html" rel="nofollow">@EnableAspectJAutoProxy Javadoc</a></li>
  </ul>
  
  <p>This seems to be an issue with AspectJ's proxy mechanism dealing with autowired dependencies.</p>
  
  <p>Why does this occur when we add the <code>@EnableAspectJAutoProxy</code>?</p>
  
  <p>Our Java Config:</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  @EnableJpaRepositories(basePackages ={"com.company.product.persistence.repository"})
  @EnableTransactionManagement
  @EnableSwagger
  @EnableAspectJAutoProxy
  @PropertySource({"classpath:hibernate.properties",
                   "classpath:auth.properties",
                   "classpath:mail.properties",
                   "classpath:locations.properties"
                  })
  @ComponentScan(basePackages = {"com.company.product"})
  public class WebConfig extends WebMvcConfigurerAdapter {
      //Bean declarations here.
      //Note: All services/repos/controllers are annotation based.
  }
  </code></pre>
  
  <p>Aspect implementation:</p>
  
  <pre><code>@Aspect
  @Component
  public class PostMessageAspect {
  
  private final Logger logger = LoggerFactory.getLogger(this.getClass());
  
      @After("execution(*com.company.product.persistence.serviceImpl.event.eventServiceImpl.methodCall(..))")
      public void postMessageRun(final JoinPoint joinPoint) {
          logger.info("CALLED AFTER METHOD");
      }
  }
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>Managed to get AOP/AspectJ working perfectly fine on one dev machine only requiring a minor change to our Spring Security config. We are both using Intellij, openJDK 1.7.0_65 on Ubuntu 14.0.4 running on default instances of Tomcat 7.0.56. On the other machine running the same software stack, gets the following.</p>
  
  <p><strong>Stack Trace:</strong></p>
  
  <p><pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dispatchingMessageController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.apx.efm.persistence.service.event.DispatchingEventService com.apx.efm.controllers.message.DispatchingMessageController.dispatchingEventService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dispatchingEventServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.apx.efm.persistence.service.building.BuildingAd dressesService com.apx.efm.persistence.serviceImpl.event.DispatchingEventServiceImpl.buildingAddressesService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'buildingAddressServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.apx.efm.persistence.service.building.BuildingService com.apx.efm.persistence.serviceImpl.building.BuildingAddressServiceImpl.buildingService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'buildingServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.apx.efm.persistence.service.building.BuildingAddressesService com.apx.efm.persistence.serviceImpl.building.BuildingServiceImpl.buildingAddressesService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'methodSecurityInterceptor' defined in class path resource [org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.aopalliance.intercept.MethodInterceptor]: Factory method 'methodSecurityInterceptor' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.apx.efm.persistence.service.user.EfmUserService com.apx.efm.application.config.SecurityConfig.efmUserService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'efmUserServiceImpl' defined in file [/home/apxdev4/Development/GitRepositories/efim-restful-web-service/target/EFIM/WEB-INF/classes/com/apx/efm/persistence/serviceImpl/user/EfmUserServiceImpl.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'methodSecurityInterceptor': Requested bean is currently in creation: Is there an unresolvable circular reference?</pre></code></p>