如何設置bean的基本屬性的值?使用@Component和@Autowired時的Spring bean原始屬性?
由於我們有@Component
註釋,而且@Autowired
註解也是用於綁定實例依賴關係,那麼基元屬性呢?
@Component
class Person{
@Autowired
Address address;
int age /// what about this one?
}
如何設置bean的基本屬性的值?使用@Component和@Autowired時的Spring bean原始屬性?
由於我們有@Component
註釋,而且@Autowired
註解也是用於綁定實例依賴關係,那麼基元屬性呢?
@Component
class Person{
@Autowired
Address address;
int age /// what about this one?
}
對於原語,您可以使用@Value
註釋。通常的情況是將其從屬性文件加載的值PropertyPlaceholderConfigurer
,然後有@Value("${property.key}")
您也可以定義你的價值觀如豆類,這是比較老派:
<bean id="foo" class="java.lang.Integer" factory-method="valueOf">
<constructor-arg value="20" />
</bean>
和然後
@Autowired
@Qualifier("foo")
private int foo;
我嘗試了Bozho建議的第二種方法。它似乎不工作。
下面的一個工作。作爲 定義的bean:
<bean id="foo" class="java.lang.Integer" factory-method="valueOf">
<constructor-arg value="20" />
</bean>
然後
@Autowired
@Qualifier("foo")
private java.lang.Integer foo;
OR
@Autowired
private java.lang.Integer foo;
第二條本辦法似乎不起作用,你寫到這裏 - http://stackoverflow.com/questions/6291462 /注 - 原始的屬性 - 對彈簧豆的時候,使用,自動裝配Autowired#6292220 – Betlista 2013-08-29 11:37:22