2012-04-03 1 views
0

1つのSpring Beanのプロパティを使用して別のBeanの親属性を設定できますか?1つのSpring Beanの親属性を別のBeanのプロパティに設定するにはどうすればよいですか?

バックグラウンド情報として、Springの設定を大きく変更することなく、コンテナ提供のデータソースを使用するようにプロジェクトを変更しようとしています。

試験したとき、私は春の豆の設定

<!-- new --> 
<bean id="springPreloads" class="sample.SpringPreloads" /> 

<!-- How do I set the parent attribute to a property of the above bean? --> 
<bean id="abstractDataSource" class="oracle.jdbc.pool.OracleDataSource" 
abstract="true" destroy-method="close" parent="#{springPreloads.dataSource}"> 
    <property name="connectionCachingEnabled" value="true"/> 
    <property name="connectionCacheProperties"> 
     <props> 
      <prop key="MinLimit">${ds.maxpoolsize}</prop> 
      <prop key="MaxLimit">${ds.minpoolsize}</prop> 
      <prop key="InactivityTimeout">5</prop> 
      <prop key="ConnectionWaitTimeout">3</prop> 
     </props> 
    </property> 
</bean> 

例外の

package sample; 

import javax.sql.DataSource; 

public class SpringPreloads { 

    public static DataSource dataSource; 

    public DataSource getDataSource() { 
     return dataSource; 
    } 

    //This is being set before the Spring application context is created 
    public void setDataSource(DataSource dataSource) { 
     SpringPreloads.dataSource = dataSource; 
    } 

} 

関連ビットを使用するプロパティを持つ

単純なクラス

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '#{springPreloads.dataSource}' is defined 
私は上から春ELを削除する場合

は、または私はこれを取得:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springPreloads.dataSource' is defined 
+0

ここで何をしようとしているのかわかりません。親は、テンプレートの設定です。これは、abstractDataSource Beanとは異なるクラスです。あるBeanから別のBeanに属性を挿入するための素晴らしい "トリック"がありますが、それはあなたの後ろのものとは思われません。 – Adam

答えて

1

私は、これはあなたが後にしている何だと思います。 springPreloads Beanは「ファクトリ」として使用されますが、そのプロパティはさまざまなプロパティでプラグインされています。

私はspringPreloads.dataSourceがoracle.jdbcのインスタンスであると推測しています。プール.OracleDataSource?

<bean id="springPreloads" class="sample.SpringPreloads" /> 

<bean id="abstractDataSource" factory-bean="springPreloads" factory-method="getDataSource"> 
    <property name="connectionCachingEnabled" value="true" /> 
    <property name="connectionCacheProperties"> 
     <props> 
      <prop key="MinLimit">${ds.maxpoolsize}</prop> 
      <prop key="MaxLimit">${ds.minpoolsize}</prop> 
      <prop key="InactivityTimeout">5</prop> 
      <prop key="ConnectionWaitTimeout">3</prop> 
     </props> 
    </property> 
</bean> 
関連する問題