0
我有一个私人字段countryCode
的实体。我想在我的实体类中添加一个方便的方法来设置国家代码:可以使用CountryCode对象或字符串来设置国家代码。为什么这个自动布线的字段总是空的?
如果国家/地区代码由字符串设置,则CountryCode存储库是必需的。但是,我无法让Spring初始化存储库字段。即使我把@Component
和@Scope("prototype")
放在我的实体上...
我在想什么?
private CountryCode countryCode;
public void setCountryCode(String code) {
this.countryCode = getByCode(code);
}
@Autowired
@Transient
private CountryCodeRepository countryCodeRepository;
private CountryCode getByCode(String code) {
if (code == null) {
throw new NullPointerException("The country code cannot be null.");
}
// countryCodeRepository is NULL below...
CountryCode finalCC = countryCodeRepository.findByAlpha2OrAlpha3(code);
// ...
}
Not enough info:Please add details of your configuration + any other associated classes – Reimeus 2014-09-06 10:59:40
根据提供的信息,我认为您的存储库和/或实体未获取组件扫描,因此无法用于自动装配。必须是您的配置问题。 – 2014-09-06 11:08:48