首先,p-namespace
被定義爲
的p-namespace
使您可以使用bean元素的屬性, 代替嵌套<property/>
元素,描述你的財產 值和/或合作豆。
換句話說,它只是定義bean屬性的一種選擇。
例如,
<bean id="ldapDao" class="org.mycompany.LdapDAOImpl" p:ldapUrl-ref="ldapHost"/>
相當於
<bean id="ldapDao" class="org.mycompany.LdapDAOImpl">
<property name="ldapUrl" ref="ldapHost" />
</bean>
其中-ref
後綴在p:
屬性與示例文檔
中說明正如你可以看到,此示例不僅包含使用的屬性值p-名稱空間,還使用特殊格式聲明屬性 引用。儘管第一個bean定義使用<property name="spouse" ref="jane"/>
來創建bean約翰到 bean的引用,但第二個bean定義使用p:spouse-ref="jane"
作爲 屬性來完成同樣的事情。在這種情況下,配偶是 屬性名稱,而-ref
部分表明這不是一個 直接值,而是對另一個bean的引用。
每個property
元素出現在您的<bean>
定義中需要在bean類中有相應的setter。
鑑於所有上述情況,對應@Bean
定義將是一個初始化org.mycompany.LdapDAOImpl
類型的對象,並調用其setLdapUrl
設定器與對應於名爲ldapHost
作爲參數豆的對象。
例如,假設你有這樣一個bean
@Bean
public LdapHost ldapHost() {
return new LdapHost();
}
,那麼你會用它來初始化ldapDao
@Bean
public LdapDaoImpl ldapDao() {
LdapDaoImpl ldapDao = new LdapDaoImpl();
ldapDao.setLdapUrl(ldapHost());
return ldapDao;
}
或者,也可以有春天注入給你。
@Bean
public LdapDaoImpl ldapDao(LdapHost ldapHost) {
LdapDaoImpl ldapDao = new LdapDaoImpl();
ldapDao.setLdapUrl(ldapHost);
return ldapDao;
}
你有什麼麻煩?你對'p:ldapUrl-ref'是什麼感到困惑嗎? –
我已更新該問題。如果還不清楚,請告訴我。謝謝。 – user6123723