spring - 方面未运行 | 弹簧启动
问题描述
我写了一个包含 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 识别了方面:添加一个无操作构造函数并从方面记录一些内容/放置一个断点。
推荐阅读
- php - 当我从 laravel 本地上传时,数字海洋 403 被禁止
- javascript - 如何添加用 javascript
- arrays - AsStreams FireDAC 从 MemoryStream 获得 nil 值
- clang - 在 VS 2019 中链接 dpdk 应用程序时出错 - 在哪里可以找到丢失的 per_lcore__lcore_id 符号
- spring-boot - 从数据库中选择查询记录是否需要使用实体注释
- azure - 如何使用 PowerShell 将 Azure 中的 blob 容器条目导出到 SQL Server?
- java - 如何通过 SDK 获取 Kafka 消费者的 ip 地址?
- python - 如何在 Pandas 的查询字符串中引用未命名的列?
- google-sheets - 从 HTM 网格中的 Google 工作表返回记录
- r - 如何在 ggplot ( R studio) 中选择饼图的顺序