Java Spring引导自定义注释中的方面
我正在为方法参数创建一个自定义注释NullCheck,以检查值是否为null(@NullCheck String text),但我无法调用注释周围的方面 主类Java Spring引导自定义注释中的方面,java,spring-boot,annotations,spring-aop,Java,Spring Boot,Annotations,Spring Aop,我正在为方法参数创建一个自定义注释NullCheck,以检查值是否为null(@NullCheck String text),但我无法调用注释周围的方面 主类 package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.spr
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);
}
}
控制器类,只调用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());
}
}
格雷德尔先生
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);
}
}
有关我的代码的更多详细信息,您可以在我的github存储库中查看,我已经创建了spring boot演示版本,并在
这还包括其他类型的方面,如跟踪特定方法的时间
希望这将帮助您在Spring boot中基本了解
@Aspect
我不知道为什么会这样,但我也有同样的问题。这可以通过使用切入点轻松解决,例如:
@Before("nulllCheckAnnotation()")
public void beforeAdvice(JoinPoint joinPoint) {
System.out.println("Before method:" + joinPoint.getSignature());
}
}
@Pointcut("@annotation(com.example.demo.NullCheck)")
private void nulllCheckAnnotation() { }
如果您感兴趣,请在此阅读有关切入点的更多信息:检查现有解决方案Hi@krishna,感谢您的回复,您的代码编译是否正常?我在这里也尝试了同样的@Around(“execution(*hello.HelloController.nullChecker(String))),它的意思是java.lang.IllegalArgumentException:警告与此类型名称不匹配:hello.HelloController[Xlint:invalidabsolutionpename],所以我尝试了@Around(“execution(*hello.HelloController.nullChecker(String)))它说java.lang.IllegalArgumentException:Pointcut的格式不正确:在字符位置43执行(*@com.example.demo.NullChecker())@shrikant.sharma时需要“name pattern”,因此我的代码中没有
com.example.demo.NullChecker
。在Intellj或eclipses中,请运行类应用程序的main
方法,我附上了github中编译的屏幕截图。还有一件事,你应该只从gs spring boot
目录编译。嗨@krishna,我的错,我忘了在代码段中添加包,每个类都在“com.example.demo”包中,我也更新了有问题的包。@shrikant.sharma我已经添加了运行我在github上提供给你的代码的步骤,请通过:@shrikant.sharma检查这些步骤。根据您的代码片段,我提供了带有包名的代码,希望您能够运行它并尽快验证。
@Before("nulllCheckAnnotation()")
public void beforeAdvice(JoinPoint joinPoint) {
System.out.println("Before method:" + joinPoint.getSignature());
}
}
@Pointcut("@annotation(com.example.demo.NullCheck)")
private void nulllCheckAnnotation() { }