Pet*_*zov 2 java reflection spring
我想使用核心 java 读取类级别的注释。我试过这个:
注解:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Fix {
public String[] author() default "";
}
Run Code Online (Sandbox Code Playgroud)
班级:
@Fix(author="John Doe")
public class TestClass {
public void test(){
}
}
Run Code Online (Sandbox Code Playgroud)
读取类的测试方法:
ResourcePatternResolver resolversec = new PathMatchingResourcePatternResolver();
Resource[] resour = resolversec.getResources("classpath*:/com/validation/*.class");
Class<?> cl = resolversec.getClassLoader().loadClass("com.validation.ValidateCharacteristicsProcessor");
if(cl != null){
out.println("Class is not null " + cl.getSimpleName());
}
Fix fix = cl.getAnnotation(Fix.class);
if (fix != null) {
out.println("!!!! " + fix.author());
}
Run Code Online (Sandbox Code Playgroud)
但是注释@Fix是空的。您知道阅读此注释的正确方法是什么吗?
填写示例:https : //pastebin.com/KbBAZVfB
您呼叫getAnnotation(Fix.class)的com.validation.ValidateCharacteristicsProcessor,而不是TestClass。注释按预期出现在TestClass:
jshell> TestClass.class.getAnnotations()
$4 ==> Annotation[1] { @Fix(author={"John Doe"}) }
jshell> TestClass.class.getAnnotation(Fix.class)
$5 ==> @Fix(author={"John Doe"})
jshell> TestClass.class.getAnnotation(Fix.class).author()[0]
$6 ==> "John Doe"
Run Code Online (Sandbox Code Playgroud)
看看你的完整例子,我猜要么predictedName不是正确的字符串,要么你正在检查的类实际上没有正确注释。
创建一个具有所需行为的小型MCVE,然后在您掌握了基本方法后将其重新合并到更大的项目中可能会有所帮助。
首先,我认为com.validation.ValidateCharacteristicsProcessor在你的项目中不存在。尽管如此,如果您正在尝试加载该类(如果存在),请检查是否使用该类进行了注释@Fix。
我正在展示您提供的 TestClass。
我尝试了一个简单的方法。
首先,我创建了一个简单的 maven 项目来演示两种方法。
使用Java 反射。
像在 Test 方法中一样使用Spring Core 。
项目结构:
修复程序
package com.example;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(TYPE)
public @interface Fix {
public String[] author() default "";
}
Run Code Online (Sandbox Code Playgroud)
测试类.java
package com.example;
@Fix(author = "John Doe")
public class TestClass {
public void test() {
}
}
Run Code Online (Sandbox Code Playgroud)
主程序
package com.example;
public class Main {
public static void main(String[] args) throws ClassNotFoundException {
// create an instance of TestClass
TestClass t = new TestClass();
// use reflection to extract annotation from TestClass.class
Fix fix = t.getClass().getAnnotation(Fix.class); // or TestClass.class.getAnnotation(Fix.class);
// print the value
System.out.println(fix.author()[0]);
}
}
Run Code Online (Sandbox Code Playgroud)
输出 :
如果您想继续使用您提供的代码,那么我在下面提供了一个修复程序。
我已经研究过ResourcePatternResolver并且我认为您正在使用spring-core阅读课程。
所以,我已经更新了你的代码。
package com.example;
import java.io.IOException;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
public class Main {
public static void main(String[] args) throws ClassNotFoundException {
ResourcePatternResolver resolversec = new PathMatchingResourcePatternResolver();
Class<?> cl = resolversec.getClassLoader().loadClass("com.example.TestClass");
if (cl != null) {
System.out.println("Class is not null " + cl.getSimpleName());
}
Fix fix = cl.getAnnotation(Fix.class);
if (fix != null) {
System.out.println("!!!! " + fix.author()[0]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出 :
pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>read-annotations</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
528 次 |
| 最近记录: |