看点不 运行 | Spring-开机

Aspect Not Running | Spring-Boot

我写了一个包含 Aspect 的 jar,请在下面找到 类 和 xml:-

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>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

与其他 spring-webflux 启动器一起

并且我在另一个启动应用程序中使用这个 jar 作为依赖项,请在下面查看我的启动应用程序:-

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);
        }
    }
}

但是当我请求updateCity时,它并没有执行我的方面(APILockAspect)而是直接执行服务逻辑,谁能帮我看看我做错了什么?

看起来你已经做对了,但还是不行,请检查以下内容(应该写成评论,而不是回答,但文字太多,所以它总比没有好 :) ):

  1. 确保带有 aspect 的 jar 本身不是 spring 引导应用程序(转到目标文件夹并查看由 maven/gradle 不包括像 BOOT-INF 文件夹这样的东西。这是因为你不能让一个 spring 启动应用依赖于另一个。

  2. 确保方面在启动期间被spring识别:添加一个无操作构造函数并从方面记录一些东西/放置一个断点。