Defaultbeanfactory, check_charset 备忘

java项目获取实例化对象
</pre><pre code_snippet_id="1937090" snippet_file_name="blog_20161019_3_8507335" name="code" class="html">public class DefaultBeanFactory {
	
	private final static String APP_CTX = "default";
	
	private static String seletor;
	
	public final static Object getBean(String beanName){
		return getBean(beanName,APP_CTX);
	}
	
	public final static Object getBean(String beanName,String ctxName){
		return getFactory(ctxName).getBean(beanName);
	}
	
	protected final static BeanFactory getFactory(String ctxName){
		BeanFactoryLocator beanFactoryLocator = seletor == null ? SingletonBeanFactoryLocator.getInstance():
			SingletonBeanFactoryLocator.getInstance(seletor);
		BeanFactoryReference ref = beanFactoryLocator.useBeanFactory(ctxName);
		return ref.getFactory();
	}
	
	public final static BeanFactory getFactory(){
		BeanFactoryLocator beanFactoryLocator = seletor == null ? SingletonBeanFactoryLocator.getInstance():
			SingletonBeanFactoryLocator.getInstance(seletor);
		BeanFactoryReference ref = beanFactoryLocator.useBeanFactory(APP_CTX);
		return ref.getFactory();
	}
	
	public final static boolean isSingleton(String beanName){
		return isSingleton(beanName,APP_CTX);
	}
	
	public final static boolean isSingleton(String beanName,String ctxName){
		return getFactory(ctxName).isSingleton(beanName);
	}

	public static void setSeletor(String seletor) {
		DefaultBeanFactory.seletor = seletor;
	}
	
}


检测文件编码---

public class LookupCharset {
	private Logger log = Logger.getLogger(getClass());
	/** 字符集对象 */
	private Charset charset = Charset.defaultCharset();
	/** 找到某个字符集的布尔变量 */
	private boolean found = false;

	/**
	 * 构造方法
	 * 
	 * @param file
	 */
	public LookupCharset(File file) {
		lookup(file);
	}

	/**
	 * 获取字符集
	 * 
	 * @return
	 */
	public Charset getCharset() {
		return charset;
	}

	/**
	 * 查找指定文件的字符集
	 * 
	 * @param file
	 */
	protected void lookup(File file) {
		// Initalize the nsDetector() ;
		nsDetector det = new nsDetector(nsPSMDetector.ALL);// 语言线索常量
		// Set an observer...
		// The Notify() will be called when a matching charset is found.
		det.Init(new nsICharsetDetectionObserver() {// nsICharsetDetectionObserver.Notify编码识别成功后掉用
					public void Notify(String charsetName) {
						charset = Charset.forName(charsetName);
						found = true;
					}
				});
		BufferedInputStream imp = null;
		try {
			// 读取文件
			imp = new BufferedInputStream(new FileInputStream(file));
			byte[] buf = new byte[1024];
			int len = imp.read(buf, 0, buf.length); // 读取一个缓存区的数据
			boolean isAscii = det.isAscii(buf, len);// 先测试Ascii
			if (!isAscii) {
				det.DoIt(buf, buf.length, false);// 测试
			}
			det.DataEnd();// 测试结束!
			if (isAscii) {
				charset = Charset.forName("ASCII");
				found = true;
			}
			if (!found) {// 没找到设定一个最有可能的编码!!
				String prob[] = det.getProbableCharsets();
				charset = Charset.forName(prob[0]);
			}
		} catch (IOException e) {
			log.error(e);
		} finally {
			try {
				if (imp != null) {
					imp.close();
				}
			} catch (IOException e) {
				log.error(e);
			}
		}
	}

}


实例:


DefaultBeanFactory.getBean("monitorService");

BundleConfig functionBundle = new BundleConfig("function");(附件jar)读取property文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值