java - 存储库未使用 @Autowired 实例化
问题描述
我有一个 JPA 存储库的空指针异常,我认为存储库在具有@Autowired
.
@Component
public class UserService {
@Autowired
UserRepository repository;
private Logger logger = LoggerFactory.getLogger(this.getClass());
public void createUser(User user) {
System.out.println("user email = " + user.getEmail());
repository.save(user);
}
...
应用
@ComponentScan({"mypackage", "mypackage.service"})
@SpringBootApplication
public class Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
}
}
解决方案
@autowired 用于注入由 spring 容器基于 applicationcontext.xml 文件预先实例化的 bean,因此请确保在该文件中添加您想要注入的适当 bean。 来自春天的文档
推荐阅读
- java - 使用不带 -javaagent 参数的 ByteBuddy Java 代理
- java - 在 SearchView 中输入字符串之前,如何隐藏 ListView
- javascript - 如何修改此功能
- excel - 如何将我的 VBA 命名范围转换为循环代码
- scala - 新的 dotty 运行时完全死在我身上
- tags - 了解 ID3v2.3 标签
- javascript - while循环的意外输出
- linux - 当键/值是某些东西时,如何仅打印某些文本块?
- javascript - firebase 中的 onSnapshot() 给出错误:未定义不是函数?
- c# - 在 C# 中获取 Guest 内置帐户名称