java Spring BeanFactory作为Swing应用程序中的单例
我目前正在重构一个大型的Swing应用程序,以便从XmlBeanFactory获取一些对象
很多不同的班级都可以使用它,我想知道分享这个豆工厂的最佳方式是什么
我应该创建一个Singleton来共享这个XmlBeanFactory吗
单身阶级 { 公共静态XmlBeanFactory getBeanFactory() { (...) } }
或者我应该给我的对象添加一些setter(丑陋:它添加了一些依赖项…)
- 另一个解决方案李>
谢谢
# 1 楼答案
使用静态单例是一个不错的选择。我还没有找到更好的解决办法。这有点不令人满意,因为在使用单例之前,必须使用一个连接文件对其进行初始化,否则会导致bean创建异常,这只是需要记住的另一件事
}
在springframework的较新版本中,您可以使用泛型使getBean()返回比Object更具体的类,这样您就不必在获取bean后强制转换它