Java SpringBeanFactory作为Swing应用程序中的单例
我目前正在重构一个大型的Swing应用程序,以便从XmlBeanFactory获取一些对象 很多不同的班级都可以使用它,我想知道分享这个豆工厂的最佳方式是什么Java SpringBeanFactory作为Swing应用程序中的单例,java,spring,swing,singleton,Java,Spring,Swing,Singleton,我目前正在重构一个大型的Swing应用程序,以便从XmlBeanFactory获取一些对象 很多不同的班级都可以使用它,我想知道分享这个豆工厂的最佳方式是什么 我应该创建一个单例来共享这个XmlBeanFactory吗 单件阶级 { 公共静态XmlBeanFactory getBeanFactory() { (...) } } 或者我应该给我的对象添加一些setter(丑陋:它添加了一些依赖项…) 另一个解决方案 谢谢使用静态单例是个不错的选择。我还没有找到更好的解决办法。这有点不令人满意,
- 我应该创建一个单例来共享这个XmlBeanFactory吗 单件阶级 { 公共静态XmlBeanFactory getBeanFactory() { (...) } }
- 或者我应该给我的对象添加一些setter(丑陋:它添加了一些依赖项…)
- 另一个解决方案
谢谢使用静态单例是个不错的选择。我还没有找到更好的解决办法。这有点不令人满意,因为在使用singleton之前必须用一个连接文件对其进行初始化,否则会导致bean创建异常,这只是需要记住的另一件事
public final class SpringUtil {
private static ApplicationContext context = null;
private static Set<String> alreadyLoaded = new HashSet<String>();
/**
* Sets the spring context based off multiple wiring files. The files must exist on the classpath to be found.
* Consider using "import resource="wiring.xml" in a single wiring file to reference other wiring files instead.
* Note that this will destroy all previous existing wiring contexts.
*
* @param wiringFiles an array of spring wiring files
*/
public static void setContext(String... wiringFiles) {
alreadyLoaded.clear();
alreadyLoaded.addAll(Arrays.asList(wiringFiles));
context = new ClassPathXmlApplicationContext(wiringFiles);
}
/**
* Adds more beans to the spring context givin an array of wiring files. The files must exist on the classpath to be
* found.
*
* @param addFiles an array of spring wiring files
*/
public static void addContext(String... addFiles) {
if (context == null) {
setContext(addFiles);
return;
}
Set<String> notAlreadyLoaded = new HashSet<String>();
for (String target : addFiles) {
if (!alreadyLoaded.contains(target)) {
notAlreadyLoaded.add(target);
}
}
if (notAlreadyLoaded.size() > 0) {
alreadyLoaded.addAll(notAlreadyLoaded);
context = new ClassPathXmlApplicationContext(notAlreadyLoaded.toArray(new String[] {}), context);
}
}
/**
* Gets the current spring context for direct access.
*
* @return the current spring context
*/
public static ApplicationContext getContext() {
return context;
}
/**
* Gets a bean from the current spring context.
*
* @param beanName the name of the bean to be returned
* @return the bean, or throws a RuntimeException if not found.
*/
public static Object getBean(final String beanName) {
if (context == null) {
throw new RuntimeException("Context has not been loaded.");
}
return getContext().getBean(beanName);
}
/**
* Sets this singleton back to an uninitialized state, meaning it does not have any spring context and
* {@link #getContext()} will return null. Note: this is for unit testing only and may be removed at any time.
*/
public static void reset() {
alreadyLoaded.clear();
context = null;
}
public最终类SpringUtil{
私有静态ApplicationContext上下文=null;
私有静态集alreadyLoaded=newhashset();
/**
*基于多个连接文件设置spring上下文。这些文件必须存在于类路径上才能找到。
*考虑在单个配线文件中使用“导入资源=”Cudio.xml来引用其他的配线文件。
*请注意,这将破坏所有以前的现有布线上下文。
*
*@param wiringFiles一组spring布线文件
*/
公共静态void setContext(字符串…wiringFiles){
已删除。清除();
addAll(Arrays.asList(wiringFiles));
上下文=新的ClassPathXmlApplicationContext(wiringFiles);
}
/**
*向spring上下文中添加更多bean,并为其提供一个连接文件数组。这些文件必须存在于类路径中才能被删除
*找到了。
*
*@param addFiles一组spring布线文件
*/
公共静态void addContext(字符串…addFiles){
if(上下文==null){
setContext(addFiles);
返回;
}
Set notAlreadyLoaded=new HashSet();
for(字符串目标:addFiles){
如果(!alreadyLoaded.contains(目标)){
notAlreadyLoaded.add(目标);
}
}
如果(notAlreadyLoaded.size()>0){
已添加。添加所有(未添加);
context=newclasspathXmlApplicationContext(notAlreadyLoaded.toArray(新字符串[]{}),context);
}
}
/**
*获取直接访问的当前spring上下文。
*
*@返回当前的spring上下文
*/
公共静态应用程序上下文getContext(){
返回上下文;
}
/**
*从当前spring上下文获取bean。
*
*@param beanName要返回的bean的名称
*@返回bean,如果找不到,则抛出RuntimeException。
*/
公共静态对象getBean(最终字符串beanName){
if(上下文==null){
抛出新的RuntimeException(“尚未加载上下文”);
}
返回getContext().getBean(beanName);
}
/**
*将此单例设置回未初始化状态,这意味着它没有任何spring上下文和
*{@link#getContext()}将返回null。注意:这仅用于单元测试,可以随时删除。
*/
公共静态无效重置(){
已删除。清除();
上下文=空;
}
}
对于较新版本的springframework,您可以使用泛型使getBean()返回比Object更具体的类,这样您就不必在获取bean后强制转换它