title: aspectj check broken layer access in spring boot
body: <p>I try to use aspectj for checking whether the layers of the architecture are broken, e.g. the access from controller to repository without using a service between</p>
  
  <p>Now this is marking every method call to a autowired annotated within de.fhb.controller.</p>
  
  <p>but how can i then limit that to Repositories for example?</p>
  
  <p>The package structure looks like this: </p>
  
  <ul>
  <li>de.fhb.controller</li>
  <li>de.fhb.service</li>
  <li>de.fhb.entity  and  and so on. </li>
  </ul>
  
  <p>my aspect looks like this </p>
  
  <pre><code>@Aspect
  public class Layer {
  
  @DeclareError("within(de.fhb.controller..*) &amp;&amp; @annotation(org.springframework.beans.factory.annotation.Autowired) " )
  private static final String typeWarning = "You try to access through many layers";
  
  }
  </code></pre>
  
  <p>the repository:</p>
  
  <pre><code>@Repository
  public interface BoxRepository extends JpaRepository&lt;Box, Long&gt;{
  
      public List findByPrio(long prio);
  }
  </code></pre>
  
  <p>and the controller that should be checked by aspects:</p>
  
  <pre><code>@RequestMapping("/box")
  @Controller
  public class BoxController {
  
  @Autowired
  private BoxRepository boxRepository; // negative example
  
  @Autowired
  private BoxService boxService;
  
  @RequestMapping(value = "/")
  public String list(Model model) {
      List boxes = boxRepository.findAll(); // negativ example
      model.addAttribute("boxes", boxes);
      return "box/list";
  }
  </code></pre>
  
  <p>for further look: <a href="https://github.com/svenhornberg/MDSD" rel="nofollow">repository at github</a></p>