Spring @Resource annotation不工作

yfjy0ee7  于 2023-04-04  发布在  Spring
关注(0)|答案(1)|浏览(163)

我正在学习Spring框架的DI。
Register对象被注入了一个KeywordDao对象。@Autowired annotation在构造函数中工作正常。但是,如果我对Field使用@Resource annotation,它将不起作用(null)。
javax.annotation-api依赖项被添加为Maven,因为@Resource没有被导入。
下面是源代码。
pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

...

    <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"?>

...

    <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)

我试着使用@Resource(name="keywordDao"),但不工作。
我想用@Resource注解尝试DI,有什么办法吗?

ni65a41a

ni65a41a1#

通过以下方式解决:

我意识到@Resource注解没有工作代码。
所以,我认为我没有引入所有必要的依赖关系。
因此,我修改了pom.xml并修复了这个问题。

发件人:

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

收件人:

<dependency>
    <groupId>jakarta.platform</groupId>
    <artifactId>jakarta.jakartaee-api</artifactId>
    <version>9.1.0</version>
</dependency>

相关问题