Holder模式

程序经常需要用到配置文件,但我们又并不想关心何时读取配置文件,于是写了如下代码,备忘:
/**
 * 2007-4-28 下午03:24:37
 */
package kindsoft.auth.internal;

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

import kindsoft.auth.Configuration;

/**
 * the top ContextHolder
 * @author <a href="mailto:[email protected]">桂健雄</a>
 * @since 2007-4-28
 */
public class ContextHolder {
	private static final Configuration configuration;
	static{
		ApplicationContext context = new ClassPathXmlApplicationContext("security.xml");
		configuration = (Configuration) context.getBean("configuration");
	}
	/**
	 * get the configuration
	 * @return the configuration
	 */
	public static Configuration getConfiguration() {
		return configuration;
	}
}


有时我们希望针对每个线程设置一个参数,比如在web程序中,我们使用session来保存信息,但每次必须获得session对象,才可以进行操作,于是有了如下代码,备忘:
/**
 * 2007-4-28 下午05:20:05
 */
package kindsoft.auth.internal;

import java.util.HashMap;
import java.util.Map;

/**
 * @author <a href="mailto:[email protected]">桂健雄</a>
 * @since 2007-4-28
 */
public class ThreadHolder {
	private static ThreadLocal<Map> localMap=new ThreadLocal<Map>(){
		@Override
		protected Map initialValue() {
			return new HashMap();
		}
	};
	
	@SuppressWarnings("unchecked")
	public static void put(String key,Object value){
		localMap.get().put(key, value);
	}
	
	public static Object get(String key){
		return localMap.get().get(key);
	}
	
}

你可能感兴趣的:(java,Web,xml,Security)