首页 > 解决方案 > 方面未运行 | 弹簧启动

问题描述

我写了一个包含 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)而是直接执行服务逻辑,谁能帮我找出我做错了什么?

标签: springspring-bootspring-webfluxspring-aop

解决方案


看起来你做的一切都是正确的,但它仍然不起作用,所以请检查以下内容(它应该写成评论,而不是答案,但它的文字太多,所以总比没有好: ) ):

  1. 确保带有方面的 jar 本身不是 Spring Boot 应用程序(转到目标文件夹并查看 maven/gradle 从该 jar 创建的工件不包含BOOT-INF内部文件夹之类的东西。这是因为你不能使一个 Spring Boot 应用程序依赖于另一个。

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


推荐阅读