我正在学习spring framework DI。
Register对象被注入KeywordDao对象。@Autowired
注释与构造函数配合得很好。然而,如果我使用@Resource
注释字段,它将不工作(null)。
javax。由于没有导入@Resource
, annotation-api依赖项被添加为Maven。
下面是源代码。
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>
中:
<?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")
但不工作。
我想尝试DI与@Resource
注释。有办法吗?
已解决:
我意识到没有@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>