You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring中@Resource字段注入失效求助(Autowired构造注入正常)

Spring 6中@Resource字段注入失效(NullPointerException)的解决方案

问题描述

在Spring 6.0.7中尝试依赖注入时,出现以下问题:

  • 使用@Autowired构造注入为Register对象注入KeywordDao可正常工作
  • 使用@Resource字段注入时,keywordDao始终为null,触发NullPointerException
  • 已添加javax.annotation-api依赖,尝试指定@Resource(name="keywordDao")仍无效

相关代码

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.jeongyongs</groupId>
    <artifactId>dependency-injection</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.7</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
</project>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean id="keywordDao" class="com.jeongyongs.keyword.KeywordDao"/>
    <bean id="register" class="com.jeongyongs.keyword.Register"/>

</beans>

KeywordDao.java

package com.jeongyongs.keyword;

import java.util.HashMap;
import java.util.Map;

public class KeywordDao {
    private Map<String, String> db = new HashMap<>();

    public void insert(String key, String value) {
        db.put(key, value);
    }

    public String select(String key) {
        return db.get(key);
    }

    public void update(String key, String value) {
        db.replace(key, value);
    }

    public void delete(String key) {
        db.remove(key);
    }
}

Register.java

package com.jeongyongs.keyword;

import javax.annotation.Resource;

public class Register {

    @Resource
    private KeywordDao keywordDao;

    public void newKeyword(String key, String value) {
        if (isAvailable(key)) {
            keywordDao.insert(key, value);
            return;
        }
        System.out.println("REG: fail! invalid key value.");
    }

    private boolean isAvailable(String key) {
        return keywordDao.select(key) == null;
    }
}

Main.java

package com.jeongyongs.keyword;

import org.springframework.context.support.GenericXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        String context = "classpath:applicationContext.xml";
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(context);
        Register register = ctx.getBean("register", Register.class);

        register.newKeyword("New key", "New value");
        register.newKeyword("New key", "New value");

        ctx.close();
    }
}

报错信息

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.jeongyongs.keyword.KeywordDao.select(String)" because "this.keywordDao" is null
    at com.jeongyongs.keyword.Register.isAvailable(Register.java:21)
    at com.jeongyongs.keyword.Register.newKeyword(Register.java:11)
    at com.jeongyongs.keyword.Main.main(Main.java:12)

原因分析

Spring 6.x 基于Jakarta EE 9+规范,该规范已将Java EE的包名从javax.*迁移至jakarta.*。旧的javax.annotation.Resource注解属于Java EE规范,Spring 6不再提供对其的支持,因此你添加的javax.annotation-api依赖无法被Spring识别,导致注入失效。

解决步骤

1. 替换为Jakarta版本的依赖

修改pom.xml中的javax.annotation-api依赖为Jakarta版本:

<dependency>
    <groupId>jakarta.annotation</groupId>
    <artifactId>jakarta.annotation-api</artifactId>
    <version>2.1.1</version>
</dependency>

2. 更新@Resource的导入语句

修改Register.java中的导入,使用Jakarta包下的@Resource:

import jakarta.annotation.Resource;

3. 重新运行项目

完成上述修改后,重新编译并运行项目,@Resource字段注入即可正常生效,keywordDao将被正确注入,不再触发空指针异常。

内容的提问来源于stack exchange,提问作者Jeongyong Lee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 23:02:32