【发布时间】:2013-03-17 15:57:17
【问题描述】:
问题:orderRepository = null。
据我了解 orderRepository 未注入,因为 user 不是 Spring 创建的 bean,而只是使用 new 创建的对象,然后在 dao 中设置了它的属性?
我将用户设置为活动对象,因此将存储库注入模型类。有什么解决办法,还是有更好的办法?
@Controller
class UserController {
...
// Get user from DB
User user = userService.findUser(userId);
Order order = user.findOrder(orderId);
...
}
@Component
User {
@Autowired
OrderRepository orderRepository;
Order findOrder(long orderId) {
Order order = orderRepository
.findOrderOrThrowException(this.getId(), orderId);
return order;
}
}
【问题讨论】:
-
您是否在 applicationContext.xml 中激活了组件扫描指令?
。 OrderRepository 类是什么样子的? -
是的,我得到了这一切,并且应用程序比上面的要大:) 并且在将 repo 注入模型时可以正常工作,除非我从数据库中检索模型类。这个 User bean 在 dao 中创建为 new User(),然后从结果集中填充属性。 Repo 类是常规类,已自动连接到它。
标签: spring-mvc javabeans autowired