2014-09-06 39 views
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); 

    // ... 
} 
+2

Not enough info:Please add details of your configuration + any other associated classes – Reimeus 2014-09-06 10:59:40

+0

根據提供的信息,我認爲您的存儲庫和/或實體未獲取組件掃描,因此無法用於自動裝配。必須是您的配置問題。 – 2014-09-06 11:08:48

回答

1

我猜你的實體類是由一些ORM框架實例化的,因此沒有在spring中實例化。所以春天不能自動自動裝配這些字段。

如果讓你擁有這些選項

請提供更多信息。

相關問題