ofbiz實體引擎(二) delegator實例化具體方式

/**
     * @author 鄭小康
     * 採用spi創建對應實例DelegatorFactoryImpl
     * */
    public static <A, R> R getObjectFromFactory(Class<? extends Factory<R, A>> factoryInterface, A obj) throws ClassNotFoundException {
        Iterator<? extends Factory<R, A>> it = ServiceLoader.load(factoryInterface).iterator();
        while (it.hasNext()) {
            Factory<R, A> factory = it.next();
            R instance = factory.getInstance(obj);
            if (instance != null) {
                return instance;
            }
        }
        throw new ClassNotFoundException(factoryInterface.getClass().getName());
    }


      注:上下代碼不是在一個類

/**
 * @author 鄭小康
 * 根據delegatorName創建一個GenericDelegator
 * 所以實際delegator引用的是一個GenericDelegator實例
 * */
public class DelegatorFactoryImpl extends DelegatorFactory {

    public static final String module = DelegatorFactoryImpl.class.getName();

    public Delegator getInstance(String delegatorName) {
        if (Debug.infoOn()) Debug.logInfo("Creating new delegator [" + delegatorName + "] (" + Thread.currentThread().getName() + ")", module);
        //Debug.logInfo(new Exception(), "Showing stack where new delegator is being created...", module);
        try {
            return new GenericDelegator(delegatorName);
        } catch (GenericEntityException e) {
            Debug.logError(e, "Error creating delegator", module);
            return null;
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章