0
我有一個單例類,我想用Spring的IoC創建。這個類需要使用IoC來實例化其他對象的動態數量。因此,這個類需要傳入的作爲構造函數參數的BeanFactory。我怎樣才能做到這一點?如何提供當前BeanFactory作爲構造函數參數
這是我計劃的一般結構。我對Spring IoC相當陌生,所以如果它在Spring中不適合,我也願意改變這個結構。
public class Main
{
public static void main(String[] args)
{
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
MySingletonInterface instance = context.getBean(MySingletonInterface.class);
instance.foo();
}
}
public class MySingletonClass implements MySingletonInterface
{
public MySingletonClass(BeanFactory beanFactory)
{
this.beanFactory = beanFactory;
}
public void foo()
{
for(.....)
{
NeedManyInstances instance = beanFactory.getBean(NeedManyInstances.class);
....
}
}
}
BeanFactoryAware解決方案運行良好。謝謝! – 2011-04-05 19:10:42