我有一个简单的 Gradle Spring Boot 项目,大致结构如下:
myproject
├───core
│ └───src
│ │ └───EntryDescription.java
│ └───build.gradle
├───core-web
│ └───src
│ │ └───EntryAspect.java
│ └───build.gradle
├───server
│ └───src
│ │ └───LoginController.java
│ └───build.gradle
├───build.gradle
└───settings.gradle.kts
每当调用带有
@EntryDescription
的方法时,我都会尝试记录。但是当条件满足时,代码似乎甚至没有进入Aspect代码。
这是我的注释:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EntryDescription {
String value();
}
看点:
@Aspect
@Slf4j
public class EntryAspect {
@Around("execution(public * *(..)) && @annotation(com.elal.uc.annotation.EntryDescription)")
public Object logMethodName(ProceedingJoinPoint call) throws Throwable {
Method method = ((MethodSignature) call.getSignature()).getMethod();
EntryDescription entryDescription = method.getAnnotation(EntryDescription.class);
log.info(entryDescription.value());
return call.proceed();
}
}
我的控制器:
@RestController
public class LoginController {
@PostMapping(value = "/login")
@EntryDescription("Customer login process started")
public void login(@RequestBody LoginRequest request, BindingResult bindingResult, HttpServletRequest req, HttpServletResponse response) {
// Login logic
}
}
我想罪魁祸首是Gradle,因为我设法让它在单模块环境中工作。
server/build.gradle
:
dependencies {
...
implementation project(":core")
implementation project(":core-web")
...
}
myproject/build.gradle
:
buildscript {
repositories {
mavenCentral()
}
}
plugins {
id 'java'
id 'org.springframework.boot' version "3.0.0" apply false
id "io.freefair.aspectj.post-compile-weaving" version "8.0.1"
}
subprojects {
apply plugin: 'java-library'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'idea'
dependencyManagement {
imports {
mavenBom "org.springframework.boot:spring-boot-dependencies:3.0.0"
}
}
dependencies {
implementation("org.aspectj:aspectjrt:1.9.19")
implementation("org.aspectj:aspectjweaver:1.9.19")
}
}
settings.gradle.kts
:
rootProject.name = "myproject"
include("core")
include("core-web")
注意:我尝试在应用程序类上使用
@EnableAspectJAutoProxy
但没有帮助。