1. 程式人生 > >一個接口多個實現類

一個接口多個實現類

.get framework 定時 ngs ati println pin rest 存儲

一、以靜態變量保存Spring ApplicationContext, 可在任何代碼任何地方任何時候取出ApplicaitonContext。適用於根據數據庫的配置取出對應定時任務的實現類
package com.wy.SpringBean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import org.springframework.util.Assert; /** * @description:以靜態變量保存Spring ApplicationContext, 可在任何代碼任何地方任何時候取出ApplicaitonContext, * 適用於根據數據庫的配置取出對應定時任務的實現類 **/ @Component public class
SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class); /** * 取得存儲在靜態變量中的ApplicationContext. */ public
static ApplicationContext getApplicationContext() { assertContextInjected(); return applicationContext; } /** * 從靜態變量applicationContext中取得Bean, 自動轉型為所賦值對象的類型. */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { assertContextInjected(); return (T) applicationContext.getBean(name); } /** * 從靜態變量applicationContext中取得Bean, 自動轉型為所賦值對象的類型. */ public static <T> T getBean(Class<T> requiredType) { assertContextInjected(); return applicationContext.getBean(requiredType); } /** * 清除SpringContextHolder中的ApplicationContext為Null. */ public static void clearHolder() { logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext); applicationContext = null; } /** * 實現ApplicationContextAware接口, 註入Context到靜態變量中. */ @Override public void setApplicationContext(ApplicationContext applicationContext) { logger.debug("註入ApplicationContext到SpringContextHolder:" + applicationContext); if (SpringContextHolder.applicationContext != null) { logger.warn("SpringContextHolder中的ApplicationContext被覆蓋, 原有ApplicationContext為:" + SpringContextHolder.applicationContext); } SpringContextHolder.applicationContext = applicationContext; } /** * 實現DisposableBean接口, 在Context關閉時清理靜態變量. */ @Override public void destroy() throws Exception { SpringContextHolder.clearHolder(); } /** * 檢查ApplicationContext不為空. */ private static void assertContextInjected() { Assert.state(applicationContext != null, "applicaitonContext屬性未註入, 請在applicationContext.xml中定義SpringContextHolder."); } }

二、定義接口

public interface TestInterface {

    void testInter(String str);
}

三、定義兩個個實現類

@Service("testImp01")
public class testImp01 implements TestInterface{
    @Override
    public void testInter(String str) {
        System.out.println("testImp01:"+str);
    }
}
@Service("testImp02")
public class testImp02 implements TestInterface{
    @Override
    public void testInter(String str) {
        System.out.println("testImp02:"+str);
    }
}

四、定義controller

@RestController
public class TestController {



    @RequestMapping("test01")
    public void test01() {
        TestInterface testInterface = SpringContextHolder.getBean("testImp01");
        testInterface.testInter("01");
    }
    @RequestMapping("test02")
    public void test02() {
        TestInterface testInterface = SpringContextHolder.getBean("testImp02");
        testInterface.testInter("02");
    }

}

啟動項目測試

一個接口多個實現類