首页 > 代码库 > Spring获取ApplicationContext对象工具类

Spring获取ApplicationContext对象工具类


(1)实现的工具类:

package com.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

final public class ApplicationContextUtil {

	private static ApplicationContext ac=null;
	
	private ApplicationContextUtil(){
		
	}
	
	static{
		ac=new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	public static ApplicationContext getApplicationContext(){
		//获得返回的容器对象
		return ac;
	}
	
}


(2)使用方法:

public static void getByUtil(){
		ApplicationContextUtil.getApplicationContext().getBean("userService");
	}  



Spring获取ApplicationContext对象工具类