title: Spring AOP at Service Layer
body: <p>I need some help with Spring AOP.
  I've the following code:</p>
  
  <hr>
  
  <pre><code>@Service
  public class UserSecurityService implements UserDetailsService {
  
      @Autowired
      private UserService userService;
      ....
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Service
  public class UserService extends CrudService&lt;User, UserRepository&gt; {
  
      public UserService() {
          super();
      }
  
      @Autowired
      public UserService(UserRepository repository) {
          super(repository);
          this.repository = repository;
      }
      ....
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Repository
  interface UserRepository extends JpaRepository&lt;User, String&gt; {
       ...
  }
  </code></pre>
  
  <hr>
  
  <p><strong>application-context.xml</strong></p>
  
  <pre><code>&lt;import resource="classpath*:spring/application-context-db.xml" /&gt;
  &lt;import resource="classpath*:spring/application-context-aop.xml" /&gt;
  &lt;import resource="classpath*:spring/application-context-mail.xml" /&gt;
  &lt;import resource="application-context-security.xml" /&gt;
  
  &lt;context:component-scan base-package="com.xpto"&gt;
      &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt;
      &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" /&gt;
  &lt;/context:component-scan&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>application-context-aop.xml</strong></p>
  
  <p></p>
  
  <pre><code>&lt;aop:aspectj-autoproxy /&gt;
  &lt;aop:config&gt;
      &lt;aop:aspect id="serviceLoggingAspect" ref="serviceLoggingAspectBean"&gt;
          &lt;aop:pointcut id="servicePointcut"
                  expression="@within(org.springframework.stereotype.Service)" /&gt;
  
          &lt;aop:before method="before" pointcut-ref="servicePointcut" /&gt;
          &lt;aop:after-returning method="afterReturning" pointcut-ref="servicePointcut" returning="result" /&gt;
          &lt;aop:after-throwing method="afterThrowing" pointcut-ref="servicePointcut" throwing="exception" /&gt;
      &lt;/aop:aspect&gt;
  &lt;/aop:config&gt;
  </code></pre>
  
  <hr>
  
  <p>When I try to load my application at Tomcat, I get the following exception:</p>
  
  <pre><code>Caused by: java.lang.IllegalArgumentException: Can not set com.xpto.user.service.UserService field com.xpto.user.security.service.UserSecurityService.userService to com.sun.proxy.$Proxy57
  at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
  at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
  at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
  at java.lang.reflect.Field.set(Field.java:680)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:510)
  ... 35 more
  </code></pre>
  
  <hr>
  
  <p>I've the same configuration at Web layer to Logging my application and it works fine, but when I put AOP at Service layer I get this exception.</p>
  
  <p>I'm using Spring MVC and at web.xml I configured to load two different contexts, one loads only @Controller and the other loads @Repository and @Service.</p>