开发者

Spring BeanFactory as a singleton in a Swing application

I'm currently refactoring a large Swing Applicat开发者_StackOverflow社区ion in order get some objects from a XmlBeanFactory.

Many different classes could use it and I wonder what would be the best way to share this beanFactory.

  • should I create a Singleton to share this XmlBeanFactory ?

    class Singleton { public static XmlBeanFactory getBeanFactory() { (...) } }

  • or should I add some setters to my object (ugly: it adds some depedencies... )

  • another solution ?

Thanks


Using a static singleton is an OK way to go. I haven't found a better solution, yet. Its a little unsatisfactory because the singleton must be initialized with a wiring file before it is used and failure to do this results in bean creation exceptions, just one more thing to remember to do.

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;
}

}

With newer versions of the springframework you could use generics to make getBean() return a more specific class than Object so you don't have to cast your bean after getting it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜