以下列方式加载应用程序上下文是否意味着在由 ContextLoader 创建的上下文之外创建另一个上下文?
我需要为非 bean 类加载应用程序上下文。
private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"channel-integration-context.xml");
根据我的观察,它似乎创建了另一个bean context。如果是这样,什么是更好的解决方法。
请帮忙。
最佳答案
除非一些非常特殊的原因,通常没有必要创建多个 ApplicationContext
。
你可以创建一个单例:
public class ApplicationContextWrapper {
private static ApplicationContext INSTANCE = null;
private ApplicationContextWrapper() {
}
public static ApplicationContext getIntance() {
if (INSTANCE == null) {
//note you can add more Spring XML configuration filenames in this array
String[] contexts = new String[] {"channel-integration-context.xml"};
INSTANCE = new ClassPathApplicationContext(contexts);
}
return INSTANCE;
}
}
您可以将它用于:
private static ApplicationContext applicationContext = ApplicationContextWrapper.getInstance();
关于java - new ClassPathXmlApplicationContext() 创建新的 bean 集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27501580/