1. 程式人生 > 其它 >根據不同業務型別動態切換實現類(applicationContext.getBeansOfType實現)

根據不同業務型別動態切換實現類(applicationContext.getBeansOfType實現)

  本文以獲取不同銀行賬戶餘額為例,具體實現如下:

  整體結構:

  

  IBankService.java

/**
 * 銀行介面
 */
public interface IBankService {

    /**
     * 查詢賬戶餘額
     */
    void getBalance();
}

  AbcService.java

import org.springframework.stereotype.Service;

/** 農業銀行實現類
 * @description:
 * @author: jack
 * @time: 2022/5/9 15:31
 */
@Service("abcService")
public class AbcService implements IBankService {

    @Override
    public void getBalance() {
        System.out.println("您的農行餘額為1000元");
    }
}

  CcbService.java

import org.springframework.stereotype.Service;

/** 建設銀行實現類
 * @description:
 * @author: jack
 * @time: 2022/5/9 15:34
 */
@Service("ccbService")
public class CcbService implements IBankService {
    @Override
    public void getBalance() {
        System.out.println("您的建行餘額為2000元");
    }
}

  BankFactory.java

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: jack
 * @time: 2022/5/9 15:24
 */
@Service("bankFactory")
public class BankFactory implements InitializingBean, ApplicationContextAware {

    // 實現介面的實現類集合
    private Map<String,IBankService> bankMap = new HashMap<>();
    // 宣告上下文
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        // 根據上下文獲取實現介面的實現類
        Map<String,IBankService> mapBeans = applicationContext.getBeansOfType(IBankService.class);
        // 遍歷集合並賦值
        for (String beanName : mapBeans.keySet()) {
            IBankService bean = mapBeans.get(beanName);
            bankMap.put(beanName, bean);
        }
    }

    public IBankService getBankService(String beanName) {
        return bankMap.get(beanName);
    }
}

  BankTestController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description:
 * @author: jack
 * @time: 2022/5/9 15:36
 */
@RestController
public class BankTestController {

    @Autowired
    private BankFactory bankFactory;

    @RequestMapping("/getBalance")
    public void getBalance(@RequestParam("bankName")String bankName) {
        IBankService bankService = bankFactory.getBankService(bankName+"Service");
        bankService.getBalance();
    }
}

  測試呼叫:

  http://localhost:8039/getBalance?bankName=abc

  控制檯列印:您的農行餘額為1000元

  http://localhost:8039/getBalance?bankName=ccb

  控制檯列印:您的建行餘額為2000元