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