现在的位置: 首页 > 综合 > 正文

spring 获取context的web main两种方式

2013年12月03日 ⁄ 综合 ⁄ 共 747字 ⁄ 字号 评论关闭
package com.epdc.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

/**
 * @author lsh
 * 
 */
public class SpringContextUtil {

	private static String configName = "classpath:applicationContext*.xml";
	private static ApplicationContext context;

	public static Object getWebBean(String name) {
		WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
		return wac.getBean(name);
	}

	public static Object getMainBean(String name) {
		if (context == null) {
			context = new ClassPathXmlApplicationContext(configName);
		}
		return context.getBean(name);
	}

	public static void main(String args[]) throws Exception {

	}
}

抱歉!评论已关闭.