private static ApplicationContext context;
public static void initContext(){
context = new ClassPathXmlApplicationContext( "classpath*:config/spring/*.xml");
}
public static ApplicationContext getContext(){
if(context==null)
initContext();
return context;
}
本文介绍了一个简单的Spring应用上下文初始化方法。通过一个静态方法initContext()来创建ClassPathXmlApplicationContext实例加载配置文件,并提供getContext()方法确保上下文单例且按需加载。这种方式适用于需要按需初始化Spring上下文的应用场景。

被折叠的 条评论
为什么被折叠?



