gpt4 book ai didi

java - Spring boot 自定义注解中的切面

转载 作者:行者123 更新时间:2023-11-30 12:06:54 25 4
gpt4 key购买 nike

我正在为方法参数创建一个自定义注释 NullCheck 以检查值是否为 null hello(@NullCheck String text),但我无法围绕注释调用方面。

主类

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication
@EnableAutoConfiguration
@EnableAspectJAutoProxy
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

Controller类,只是调用POC的一个方面,不返回任何东西

package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
class HelloController {
@GetMapping
public void hello() {
hello("hi");
}
private void hello(@NullCheck String text) {
System.out.println(text);
}
}

注释

package com.example.demo;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Documented
@Retention(RUNTIME)
@Target(ElementType.PARAMETER)
public @interface NullCheck { }

看点

package com.example.demo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class NullCheckAspect {

这是有效的

@Before("@annotation(org.springframework.web.bind.annotation.GetMapping)")

但这不是

@Before("@annotation(com.example.demo.NullCheck)")
public void beforeAdvice(JoinPoint joinPoint) {
System.out.println("Before method:" + joinPoint.getSignature());
}
}

构建.gradle

buildscript {
ext {
springBootVersion = '2.1.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
idea {
module {
// if you hate browsing Javadoc
downloadJavadoc = true
// and love reading sources :)
downloadSources = true
}
}
bootJar {
launchScript()
}
repositories {
mavenCentral()
jcenter()
}
bootJar {
launchScript()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
}

我错过了什么?

最佳答案

根据我的理解,在谷歌上做了一些搜索后,您可以使用以下代码获取带有特定注释的方法参数及其值:

package com.example.demo;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class NullCheckAspect {

@Around("execution(* com.example.demo.HelloController.nullChecker(String))")
public Object around(ProceedingJoinPoint pJoinPoint) throws Throwable {

Object[] args = pJoinPoint.getArgs();

Method method = MethodSignature.class.cast(pJoinPoint.getSignature()).getMethod();

Annotation[][] parametersAnnotations = method.getParameterAnnotations();

Map<String, Object> annotatedParameters = new HashMap<>();

int i = 0;
for(Annotation[] parameters : parametersAnnotations) {
Object arg = args[i];
String name = method.getParameters()[i++].getDeclaringExecutable().getName();
for(Annotation parameter: parameters) {
if ((parameter instanceof NullCheck)) {
System.out.println("Found the null checker annotation with name: " + name);
System.out.println("Found the null checker annotation with arg: " + arg);
annotatedParameters.put(name, arg);
}
}
}
System.out.println(annotatedParameters);

return pJoinPoint.proceed(args);
}
}

还有接口(interface)注解:

package com.example.demo;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import org.springframework.stereotype.Component;

@Component
@Retention(RUNTIME)
@Target(ElementType.PARAMETER)
public @interface NullCheck {
}

有关我的代码的更多详细信息,您可以在我的 github 存储库中查看,我已经创建了 spring boot 演示版本并在 https://github.com/krishnaiitd/learningJava/blob/master/springBoot/gs-spring-boot/src/main/java/com/example/demo/HelloController.java 为您推送了它。

这还包括其他类型的方面,例如跟踪特定方法的时间。

希望这能帮助您对 Spring boot 中的 @Aspect 有一个基本的了解。

关于java - Spring boot 自定义注解中的切面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55253013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com