2014-09-06 76 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中实例化。所以春天不能自动自动装配这些字段。

如果让你拥有这些选项

请提供更多信息。

相关问题