JDK9~17+Springboot3 @资源常见问题及解决方案
一、常见问题描述
因为JDK版本升级的改动,在Jdk9~17环境下,搭建Springboot项目,会出现原有@Resource(javax.annotation.Resource)不存在的问题,导致项目从Jdk8迁移到高版本时遇到的问题
原因
你可能会问,为什么javax.annotation.Resource注解不存在呢?
从Jdk9开始,JavaEE从Jdk中分离,jdk就移除掉了javax.annotation.jar包的默认集成,从而导致版本不兼容。所以一旦spring项目从JDK8升到高版本,都会出现javax.annotation.Resource无法引用报红。
java EE 即 java Enterprise Edition,企业级应用,目标是制定一系列企业级应用的标准服务。常见的
javax.servlet
,javax.annotation
。Oracle 收购了创造 java 的 SUN 公司,Oracle 又不想发展 java EE 了,就把 java EE 交给 Eclipse 社区了,但是又因为不知名的原因,禁止社区使用
javax
这个名字。所以,javax.servlet
就变成了jakarta.servlet
,jakarta.annotation
。api无法向前兼容。java ee 的最后一个版本也是 8,以后就再也没有 java ee 的新版本
二、各版本出现的问题和解决方案
2.1 Jdk9以上、Springboot3/Spring6.0以前版本的问题
解决方案:
1、手动导入javax.annotation包
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
2、降到Jdk8,你发任你发,我用Java8
2.2 Springboot3/Spring6.0以上版本的问题
从Java EE APIs 到 Jakarta EE
Spring Boot 3开始,所有的Java EE Api都需要迁移到Jakarta EE上来。
大部分用户需要修改import相关API的时候,要用jakarta替换javax。比如:原来引入javax.servlet.Filter的地方,需要替换为jakarta.servlet.Filter
但是当Spring版本升级到6.0以上的版本,即使手动引入javax.annotation包,但是还是会有问题,你会发现类无法注入,导致引用的类is Null,报空指针。
因为Spring也放弃了javax.annotation.Resource注解的支持,而是对jakarta.annotation.Resource注解的支持
private InjectionMetadata buildResourceMetadata(Class<?> clazz) {
if (!AnnotationUtils.isCandidateClass(clazz, resourceAnnotationTypes)) {
return InjectionMetadata.EMPTY;
} else {
List<InjectedElement> elements = new ArrayList();
Class targetClass = clazz;
do {
List<InjectedElement> currElements = new ArrayList();
ReflectionUtils.doWithLocalFields(targetClass, (field) -> {
if (ejbClass != null && field.isAnnotationPresent(ejbClass)) {
if (Modifier.isStatic(field.getModifiers())) {
throw new IllegalStateException("@EJB annotation is not supported on static fields");
}
currElements.add(new CommonAnnotationBeanPostProcessor.EjbRefElement(field, field, (PropertyDescriptor)null));
} else if (field.isAnnotationPresent(Resource.class)) {
if (Modifier.isStatic(field.getModifiers())) {
throw new IllegalStateException("@Resource annotation is not supported on static fields");
}
if (!this.ignoredResourceTypes.contains(field.getType().getName())) {
currElements.add(new CommonAnnotationBeanPostProcessor.ResourceElement(field, field, (PropertyDescriptor)null));
}
}
});
ReflectionUtils.doWithLocalMethods(targetClass, (method) -> {
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
if (BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
if (ejbClass != null && bridgedMethod.isAnnotationPresent(ejbClass)) {
if (Modifier.isStatic(method.getModifiers())) {
throw new IllegalStateException("@EJB annotation is not supported on static methods");
}
if (method.getParameterCount() != 1) {
throw new IllegalStateException("@EJB annotation requires a single-arg method: " + method);
}
PropertyDescriptor pdx = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
currElements.add(new CommonAnnotationBeanPostProcessor.EjbRefElement(method, bridgedMethod, pdx));
} else if (bridgedMethod.isAnnotationPresent(Resource.class)) {
if (Modifier.isStatic(method.getModifiers())) {
throw new IllegalStateException("@Resource annotation is not supported on static methods");
}
Class<?>[] paramTypes = method.getParameterTypes();
if (paramTypes.length != 1) {
throw new IllegalStateException("@Resource annotation requires a single-arg method: " + method);
}
if (!this.ignoredResourceTypes.contains(paramTypes[0].getName())) {
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
currElements.add(new CommonAnnotationBeanPostProcessor.ResourceElement(method, bridgedMethod, pd));
}
}
}
}
});
elements.addAll(0, currElements);
targetClass = targetClass.getSuperclass();
} while(targetClass != null && targetClass != Object.class);
return InjectionMetadata.forElements(elements, clazz);
}
}
通过跟代码进去,发现@Resouce的源代码是 jakarta.annotation下的
package jakarta.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Resources.class)
public @interface Resource {
String name() default "";
String lookup() default "";
Class<?> type() default Object.class;
Resource.AuthenticationType authenticationType() default Resource.AuthenticationType.CONTAINER;
boolean shareable() default true;
String mappedName() default "";
String description() default "";
public static enum AuthenticationType {
CONTAINER,
APPLICATION;
private AuthenticationType() {
}
}
}
推荐阅读
-
Flutter 故障排除系列:键盘原理及常见问题解决方案
-
JDK9~17+Springboot3 @资源常见问题及解决方案
-
HC-05蓝牙模块常见问题、解决方案及实现在手机间的连接通透讲解
-
移动云加强全方位云网保护,守护数字中国发展 - 新增云安全中心涵盖终端安全,整合EDR的查杀、预警、应对及溯源功能,实现终端安全管理一体化。它能迅速定位并处理各类网络威胁,如病毒、入侵和新漏洞,减少人工应对负担。EDR在HVV行动中是关键防护,能在终端建立坚固防线,阻止威胁扩散,并协同其他产品追踪攻击链路。 态势感知全面覆盖监控、审计、运维、评估和预警等多个方面,针对混合云环境,提供统一业务安全管理、全面安全信息收集、智能安全事件关联分析以及系统性能与可用性的全面检测,满足等保标准、安全运营、数据保护和重要时期的保障需求。 云堡垒机推出全新混合云版本,支持混合云、私有云及客户自建平台部署,专为运维资源管理和审计提供安全保障。安全资源池行业版则针对于私有云和行业云,提供定制化的场景化安全合规整体解决方案,并可根据需要提供改造、统一管理、远程更新等一系列配套服务。 共同构建安全、便捷且高效的远程办公环境。
-
JS复制文字到剪贴板的常见问题及全面解决方案
-
共享开源流媒体解决方案、服务器、直播平台及更多优质资源!
-
HERO引擎的常见问题及解决方案
-
MaxCompute(原ODPS) MapReduce 常见问题及解决方案
-
Postfix常见问题深度解析及解决方案
-
Vue3和Vite的按需自动导入设置教程及常见问题解决方案