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文件