Hello there! I'm working on a simple demo to use Spring and AOP to make HATEOAS on rest services simpler, but I'm having some issues with aspect and autowiring.
This is driving me crazy for the past hours. I've searched around but can't find a reason why my dependencies are not being wired to my aspects.
The EtagService is null on the aspect. Spring does not inject the service. The service is created by Spring as I can see on the logs the bean creation, and It gets injected in another place with no problems.
Here's the aspect and the config that starts the app.
I tried to add the aspect package to the component scan, tried to add @configurable to it, nothing seems to work.
I really would love some help here.
Regards
Code:@Configuration @ComponentScan(basePackages={"com.fb.restbucks.services","com.fb.restbucks.aspects"},excludeFilters={@Filter(Configuration.class)}) @EnableAspectJAutoProxy(proxyTargetClass=true) public class AppConfig { } @Configurable @Aspect public class ResponseEnhancerAspect { @Autowired private ETagService eTagService; @Around("execution(public ResponseEntity com.fb.restbucks.controllers.*.*(..)) && @annotation(javax.ws.rs.GET)") public Object addEtag(ProceedingJoinPoint pjp) throws Throwable { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); Object retVal = pjp.proceed(); ResponseEntity entity = (ResponseEntity)retVal; HttpHeaders headers = new HttpHeaders(); String url = ServletUriComponentsBuilder.fromRequest(request).build().toString(); String tag = null; try{ tag = eTagService.get(url); }catch (InvalidTagException e) { tag = eTagService.generate(url, entity.getBody()); } headers.add("Etag", tag); return new ResponseEntity(entity.getBody(),headers,entity.getStatusCode()); } }


Reply With Quote