所以,在过去的 4 个小时里,我一直用头撞显示器,但无法弄清楚。
我正在使用 Dozer 进行绘图,效果很好。但是,我需要目标类中的 DAO 之一,并且 Autowiring 返回 null
。这是该类的片段:
@Component
public class Address
{
@XmlElement(name = "street", required = true)
protected String street;
@XmlElement(name = "city", required = true)
protected String city;
@XmlElement(name = "zip", required = true)
protected zip;
@Autowired
private CityDao cityDao;
// Getters/setters
}
cityDao
始终为 null
。我对 Spring 和 Dozer 都很陌生,但是 Dozer 文档说目标类是使用默认构造函数创建的,据我了解 Spring 应该不会有任何问题。不过,cityDao
是null
。请帮忙!
最佳答案
正如评论中所提到的,您不应该不将 DAO 注入(inject)到 DTO 中!
但是,如果您出于某种原因确实需要这样做,请查看 Spring 的 @Configurable
支持。这是Javadoc还有更多信息 here和 here .
如果设置正确,它允许未由 Spring 显式管理的对象受益于 Autowiring 依赖项等功能。
在示例代码中,Address
尽管使用 @Component
进行注释,但并不由 Spring 管理,因为它是使用 Dozer 创建的。这就是为什么您需要 @Configurable
将 CityDao
注入(inject) Address
关于java - 推土机目标类中的 Spring Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24226759/