一个接口多个实现类

摘要:
importorg.springframework.context.ApplicationContextAware;TgetBean(Stringname){assertContextInjected();TgetBean(Class<applicationContext=null;
一、以静态变量保存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");
    }

}

启动项目测试

免责声明:文章转载自《一个接口多个实现类》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇NodeJs实现下载Excel文件Delphi -- 创建 桌面、发送到...、快速启动栏、开始菜单、程序菜单、右键菜 单下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Bat脚本之延时变量cmd /v:on

延时环境变量作用即工作原理: ​ 当准备执行一条命令时,命令解释器会检查语句中是否有环境变量,如果有就会先将变量的值读取出来赋给变量,就像初始化,会将已知变量先赋值。这会导致语句中多次使用的某个环境变量的值都是一样的都是一个定值。 ​ 延时环境变量,开启方式如下,使用!xx!包裹,不是%xx%,会在变量使用的时候才进行读值赋值,不会在一开始就赋值。 举个例...

nodejs下function,new function和this的研究

转:http://www.html5china.com/html5-article-3023-1.html 重点我都高亮了! 由于在使用nodejs之前接触js也比较少,最近一直被js的function和new function所困惑,由于两者都可以呈现出面向对象的样子,不知道两者的差别在哪里,就此问题做了一些研究。在研究的过程中发现this指针是个...

iOS 16进制颜色的宏

iOS 16进制颜色的宏// 参数格式为:0xFFFFFF  16进制#define kColorWithRGB(rgbValue)     [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0                     green:((f...

Oracle之PL/SQL编程

PL/SQL(ProceduralLanguage/SQL,过程语言/SQL) 是结合了Oracel过程语言和结构化查询语言(SQL)的一种扩展语言。 优点: (1)PL/SQL具有编程语言的特点,它能把一组SQL语句放到一个模块中,使其更具模块化种序的特点。 (2)PL/SQL可以采用过程性语言控制程序的结构。 (3)PL/SQL有自动处理的异常处理机制...

R in action读书笔记(15)第十一章 中级绘图 之二 折线图 相关图 马赛克图

第十一章 中级绘图 本节用到的函数有: plot legend corrgram mosaic 11.2折线图 如果将散点图上的点从左往右连接起来,那么就会得到一个折线图。 创建散点图和折线图: > opar<-par(no.readonly=TRUE) > par(mfrow=c(1,2)) > t1<-su...

函数响应式编程(FRP)从入门到”放弃”——基础概念篇

前言 研究ReactiveCocoa一段时间了,是时候总结一下学到的一些知识了。 一.函数响应式编程 说道函数响应式编程,就不得不提到函数式编程,它们俩到底有什么关系呢?今天我们就详细的解析一下他们的关系。 现在有下面4个概念,需要我们理清一下它们之间的关系:面向对象编程 Object Oriented Programming响应式编程 Reactive...