Spring 4 AOP @Aspect not starting for @RestController

I created an Aspect that performs a basic identity mapping to ensure that the user belongs to the same group that created the requested object. I had success related to my aspect with @Service methods, but this does not make sense at the service level, and I need it to be attached to @RestController methods. When I try to do this, everything seems good, but my Aspect never starts and the logs do not work.

pom.xml

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.7.RELEASE</version> </dependency> 

spring context

 <context:annotation-config/> <context:component-scan base-package="my.pkg"/> <aop:aspectj-autoproxy/> <aop:config proxy-target-class="true"/> 

Format

 @Aspect @Component public class MyAspect { @Pointcut("within(@org.springframework.stereotype.Controller *)") public void controller() {} @Pointcut("within(@org.springframework.web.bind.annotation.RestController *)") public void restController() {} @Pointcut("args(java.security.Principal,..)") public void principalArgPointcut() {} @Around("(controller() || restController()) && principalArgPointcut()") public Object validate(ProceedingJoinPoint point) throws Throwable { doValidationBefore(); Object result = point.proceed(); doValidationAfter(); return result; } } 

where "doValidationBefore ()" and "doValidationAfter ()" throw an exception if the check fails.

And finally, my RestController

 @RestController @RequestMapping("/my-path") public class MyController { @RequestMapping(value = "/{entityId}", method = RequestMethod.GET) public @ResponseBody ResponseEntity<MyEntity> getEntityDetails(Principal principal, @PathVariable("entityId") Long entityId) { return new ResponseEntity(HttpStatus.OK); } } 

Some notes:

  • This exact aspect works when I change the execution template to match the services and place it in my service package.
  • Aspect and RestController are in the same context
  • I use the IDEA IDE, and when I use the "go to recommended methods" icon on Aspect, the method I'm testing is listed in the list of methods.
  • None of the methods listed in the Navigating to Recommended Methods section work.

Things I tried:

  • I added 3 of my libraries pom.xml: org.aspectj: aspectjrt: 1.8.6, org.aspectj: aspectjtools: 1.8.6, cglib: cglib: 2.2.2. None of them made any difference.
  • I tried to define my Aspect and PointCuts directly in the xml context and remove the annotations, no difference.
  • I tried to set my execution pattern for all ALL methods, and it still didn't work.
  • I tried adding an interface to my RestController, without any changes.

I would have liked some help here, as I have been trying to solve this for quite some time. I know this is possible.

+7
spring-aop spring-mvc spring-4 spring-restcontroller
source share
2 answers

As it turned out, my Aspect and my controllers were NOT really in the same context.

While I assumed that my controllers would be included in the context scan of my web-context.xml, they were actually checked in WEB-INF / servlet-context.xml

As soon as I moved the Aspect configuration to WEB-INF / servlet-context.xml, my Aspect started to run as expected.

Thanks to everyone who reflected on my problem.

+7
source share

Determine where your service is configured in spring. If you have one, there is an icon that you can click to go to the spring bean configuration. Then make sure the aspect is configured in the same file.

If they are definitely defined in the same file, note that the pointcutcut tips correspond to the method in which you expect it to run. Again, Idea has an icon next to a pointcut that will focus on the appropriate methods.

0
source share

All Articles