package CoreFS.SA01;

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

import CoreFS.SA04.CoreDBProxy;
import CoreFS.SA06.CoreICommunicationService;

/**
 * @desc :对bean的通用操作 bean工厂
 * @author sunjiawang
 * @create :091128
 * @modify :091128
 */
public class CoreBeanFacotry implements CoreIBeanFacotry {

	/**
	 * @desc :获取bean实例
	 * @param srcpath
	 *            Spring配置文件的源路径
	 * @param beanid
	 *            bean 的名称
	 * @create :091128
	 * @modify :091128
	 */
	public Object getBean(String srcpath, String beanid) {
		Object bean = null;

		try {
			ApplicationContext acontext = new ClassPathXmlApplicationContext(
					srcpath);
			bean = acontext.getBean(beanid);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return bean;
	}

	/**
	 * @desc :获取bean实例
	 * @param beanid
	 *            bean 的名称
	 * @create :091128
	 * @modify :091128
	 */
	public Object getBean(String beanid) {
		return this.getBean("applicationContext.xml", beanid);
	}

	/**
	 * @desc :获取HessionService实例
	 * @param srcpath
	 *            Spring配置文件的源路径
	 * @param beanid
	 *            bean 的名称
	 * @create :091128
	 * @modify :091128
	 */
	public CoreICommunicationService getHessionServiceBean(String srcpath,
			String beanid) {
		// TODO Auto-generated method stub
		CoreICommunicationService hessionServiceBean = null;
		if (this.getBean(srcpath, beanid) instanceof CoreICommunicationService)
			hessionServiceBean = (CoreICommunicationService) this.getBean(
					srcpath, beanid);
		return hessionServiceBean;
	}

	/**
	 * @desc :获取dao实例
	 * @param srcpath
	 *            Spring配置文件的源路径
	 * @param beanid
	 *            bean 的名称
	 * @create :091128
	 * @modify :091128
	 */
	public CoreDBProxy getDaoBean(String srcpath, String beanid) {
		CoreDBProxy dao = null;
		if (dao == null) {
			Object bean = this.getBean(srcpath, beanid);
			if (bean instanceof CoreDBProxy)
				dao = (CoreDBProxy)bean;
		}
		return dao;
	}

}
