我已经编写了一个包含 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),而是直接执行服务逻辑,任何人都可以帮助我找到我做错了什么吗?
最佳答案
看起来你已经做对了一切,但仍然不起作用,所以请检查以下内容(它应该写成评论,而不是答案,但文字太多,所以还是更好总比什么都没有:) ):
确保带有某个方面的 jar 本身不是 Spring Boot 应用程序(转到目标文件夹并查看 maven/gradle 从此 jar 创建的工件不包含诸如
BOOT 之类的内容-INF
文件夹内。这是因为你不能让一个 Spring Boot 应用程序依赖于另一个应用程序。确保该方面在启动过程中被 spring 识别:添加一个无操作构造函数并从该方面记录一些内容/放置一个断点。
关于spring - 方面未运行 | Spring Boot ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64998840/