gpt4 book ai didi

java - 运行时类路径中是否需要注解类文件?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:23:40 24 4
gpt4 key购买 nike

如果一个类被注解了,那么该注解的定义是否必须在运行时类路径中才能使用该类?例如,给定注释

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Retention;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Component {}

我可以执行程序吗

@Component
public class Test {
public static void main(String[] args) {
System.out.println("It worked!");
}
}

类路径中没有 Component.class? (在我的测试中我可以,但是规范定义了这种行为吗?)

我问是因为有conflicting claims使用库中的注释是否会创建对该库的依赖。

最佳答案

运行时注解是注解处理器在运行时处理的元信息。如果在运行时访问注解,你肯定会在类路径中添加注解。例如junit肯定需要类路径中的注解来确定测试方法。

如果没有完成注释的处理,就没有必要让它成为类路径。

关于java - 运行时类路径中是否需要注解类文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10032946/

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