SpringBoot java 一個接口,多個實現,客戶定製化

產品定製化時,在不同的客戶中會有不同的需求,這時候會產生,一個接口,多個實現

SpringBoot ,如果發現有多實現時,會報如下錯誤
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

解決文案

在類上加 @Primary

具體代碼如下
接口
IVipSoftService.java

public interface IVipSoftService {
    /**
     * 用來演示產品中的實現方法 - 基礎實現
     */
    void defaultImpl();

    /**
     * 用來演示產品中的定製方法 - 客戶定製
     */
    void overrideImpl();
}

Default 默認實現

@Service
public class DefaultServiceImpl implements IVipSoftService {

    @Override
    public void defaultImpl() {
        System.out.println("DefaultServiceImpl.defaultImpl()");
    }

    @Override
    public  void overrideImpl() {
        System.out.println("DefaultServiceImpl.overrideImpl()");
    }

}

定製化實現
在類上加 @Primary


@Primary
@Service
public class VipSoftServiceImpl extends DefaultServiceImpl { 

    @Override
    public void overrideImpl() {
        //super.overrideImpl();
        System.out.println("VipSoftServiceImpl.overrideImpl()");
    }
}

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