2

I have written a jar which contains an Aspect, Please find the classes and xml below:-

package com.foo.bar;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface APILock {
    int lockAtMostFor() ;
    int lockAtLeastFor() default 0;
}
package com.foo.bar.aspect;
@Aspect
@Component
@Slf4j
public class APILockAspect {
    @Around("@annotation(com.foo.bar.APILock)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
       System.out.println("Inside Aspect");
       joinPoint.proceed();
   }
}

spring-boot-aop dependency

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

along with other spring-webflux starters

And I use this jar as an dependency in another boot application, please see my boot application below:-

package com.bla.application;

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@SpringBootApplication
@ComponentScan(basePackages = {"com.foo.bar","com.bla"})
public class ReactiveApplication {

    public static void main(String[] args) {
        SpringApplication.run(ReactiveApplication.class, args);
        System.out.println("--------STARTED------------");
    }
}
package com.bla.service;

@Service
public class CityService {
    @Autowired
    private CityRepository cityRepository;

    public Flux<City> getAllEmployees() {
        return cityRepository.findAll();
    }
    @APILock(lockAtLeastFor = 30,lockAtMostFor = 60)
    public Mono<City> updateCity( City city, APILockKey key) {
        try {
        return cityRepository.save(city);
        }catch (Exception e) {
            return Mono.error(e);
        }
    }
}

But when I hit my request for updateCity, it's not executing my aspect(APILockAspect) but directly executing the service logic, can anyone help me find what I am doing wrong?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Kumar-Sandeep
  • 202
  • 1
  • 4
  • 14
  • How is the instance of `CityService` obtained and `updateCity()` method triggered ? – R.G Nov 25 '20 at 13:11
  • There is a controller which is auto wiring cityService and updateCity is called from that controller – Kumar-Sandeep Nov 25 '20 at 14:22
  • No other case I can think of . As pointed out in the answer from Mark , do confirm if the Aspect bean is created during startup. – R.G Nov 26 '20 at 02:15
  • Your `@Around` advice does not return anything. It should not even compile. (Posting pseudo code is not particularly helpful.) If you do not wish to change the target method's original result, just use `return joinPoint.proceed();`. – kriegaex Nov 30 '20 at 01:20

1 Answers1

2

It looks you've done everything right, but still it doesn't work, so please check the following (it should have been written as a comment, not as an answer, but its too much text, so its still better than nothing :) ):

  1. Make sure that the jar with an aspect is not a spring boot application by itself (go to target folder and see that the artifact created out of this jar by maven/gradle does not include stuff like BOOT-INF folder inside. This is because you can't make one spring boot app depend on another.

  2. Make sure that the aspect is recognized by spring during the startup: Add a no-op constructor and log something from the aspect / place a breakpoint.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97