在日常开发中,存在着这种一种场景,框架对接口A提供了一个种默认的实现AImpl,随着需求的变更,现今AImpl不能满足了功能需要,这时,我们该怎么办? 当然是修改AImpl的实现代码了,但是,如果它是第三方依赖呢? 或者说,我们自己写了的某个功能模块,AImpl是我们为此模块提供的一种默认实现,用户可以使用它,也可以按需求进行覆盖(或者说是扩展),该怎么办?

  对此,spring为我们提供了@Conditional注解,使用它可以很好的解决此类问题。

  下面,举个例子。 有个RandomGenerator接口,接口里面有个generator()方法生成随机字符串,有两个实现类StringRandomGenerator和NumberRandomGenerator, 其中StringRandomGenerator是默认实现。也就是说,在我们不配置NumberRandomGenerator时,程序使用StringRandomGenerator生成随机字符串,在我们有配置NumberRandomGenerator时,程序使用NumberRandomGenerator生成随机字符串。

/**
* 生成随机字符串的顶层接口
*/
public interface RandomGenerator {
Object generator();
}
import org.apache.commons.lang.RandomStringUtils;

public class StringRandomGenerator implements RandomGenerator {
@Override
public Object generator() {
char[] chars = {'a','b','c','d','e','f','g'};
String random = RandomStringUtils.random(5, chars);
return "StringRandomGenerator:"+random;
}
}
/**
* 将NumberRandomGenerator 注册成spring bean时,StringRandomGenerator 失效
* 相当于是NumberRandomGenerator中的generator方法覆盖了 StringRandomGenerator 中的generator方法
*/
@Component
public class NumberRandomGenerator implements RandomGenerator {
@Override
public Object generator() {
String random = RandomStringUtils.random(5, true, true);
return "NumberRandomGenerator:" + random;
}
}
/**
* 配置类
*/
@SpringBootConfiguration
public class RandomGeneratorConfig { /**
* @ConditionalOnMissingBean(value = RandomGenerator.class)
* 该注解的意思是: 如果Ioc容器中没有 RandomGenerator 类型的 bean时
* 就将StringRandomGenerator注册到Ioc容器中
*/
@Bean
@ConditionalOnMissingBean(value = RandomGenerator.class)
public RandomGenerator stringRandomGenerator() {
return new StringRandomGenerator();
}
}

最后写个controller来测试一把

@RestController
public class RandomController {
@Autowired
private RandomGenerator randomGenerator; @GetMapping("/random")
public Object random() {
return randomGenerator.generator();
}
}

在将NumberRandomGenerator 注册到Ioc容器的情况下,测试结果如下:

接下来,我们将NumberRandomGenerator类上中@Component注解干掉,再测试

完美!

Spring框架之接口实现覆盖(接口功能扩展)的更多相关文章

  1. Spring 框架的架包分析、功能作用、优点,及jar架包简介

    Spring 框架的架包详解    Spring的作用     Spring的优势  由于刚搭建完一个MVC框架,决定分享一下我搭建过程中学习到的一些东西.我觉得不管你是个初级程序员还是高级程序员抑或 ...

  2. Spring框架4大原则和主要功能

    Spring框架4大原则: 使用POJO进行轻量级和最小侵入式开发 POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJ ...

  3. springMvc接口开发--对访问的restful api接口进行拦截实现功能扩展

    1.视频参加Spring Security开发安全的REST服务\PART1\PART1 3-7 使用切片拦截REST服务三通it学院-www.santongit.com-.mp4 讲的比较的经典,后 ...

  4. Spring框架之什么是IOC的功能?

    1. 什么是IOC的功能? * IoC -- Inverse of Control,控制反转,将对象的创建权反转给Spring!! * 使用IOC可以解决的程序耦合性高的问题!!  

  5. Spring框架学习 - 配置

    [资料] ★★☆ Spring 中提供一些Aware相关接口,像是BeanFactoryAware. ApplicationContextAware.ResourceLoaderAware.Servl ...

  6. Spring框架和MVC原理

    Spring框架和MVC原理 目录 Spring框架 SpringMVC工作原理 参考资料 回到顶部 Spring框架 Spring当前框架有20个jar包,大致可以分为6大模块: Core Cont ...

  7. Spring框架事务支持模型的优势

    全局事务 全局事务支持对多个事务性资源的操作,通常是关系型数据库和消息队列.应用服务器通过JTA管理全局性事务,API非常烦琐.UserTransaction通常需要从JNDI获取,意味着需要与JND ...

  8. 漫谈 GOF 设计模式在 Spring 框架中的实现

    原文地址:梁桂钊的博客 博客地址:http://blog.720ui.com 欢迎关注公众号:「服务端思维」.一群同频者,一起成长,一起精进,打破认知的局限性. 漫谈 GOF 设计模式在 Spring ...

  9. 设计模式在 Spring 框架中的良好应用

    在开始正文之前,请你先思考几个问题: 你项目中有使用哪些 GOF 设计模式 说一说 GOF 23 种设计模式的设计理念 说说 Spring 框架中如何实现设计模式 假设我是面试官问起了你这些面试题,你 ...

随机推荐

  1. CSU 1552 Friends(二分图 + 米勒测试)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1552 Description On an alien planet, every e ...

  2. P1582倒水

    推了一个多小时的式子,ac后一看题解,7行代码搞定 emmmm我还是太菜了 传送 蒟蒻解法: 不管怎么倒水,最终所有瓶子里面的水的数量一定可以用2k表示出来. n最终可以合并成几个瓶子呢? 我们可以把 ...

  3. 015-Spring Boot 定制和优化内嵌的Tomcat

    一.内嵌web容器 参看http://www.cnblogs.com/bjlhx/p/8372584.html 查看源码可知提供以下三种: 二.定制优化tomcat 2.1.配置文件配置 通过appl ...

  4. sticky用法

    可以用于滚动到一定距离以后让他实现定位效果. 比如滚动到50px的时候让导航栏固定定位. 用法:给最外层的div设置绝对定位 然后要固定的div设置position:sticky; top:0: 这样 ...

  5. 16/7/8_PHP-书写规范 PHP Coding Standard

    变量命名规范这里感觉 打算采用 匈牙利命名法+驼峰法命名,因为 PHP是弱类型语言,很多时间因为忽略了变量类型而导致犯一些低级错误.所以在前面加上类型名有助于更好的理解代码. 下载是转载 PHP书写规 ...

  6. HTML --JS 选择框

    <html> <head> <title>选择框</title> <script language="JavaScript"& ...

  7. 5期-Metasploitable3专题课程

    metasploitable2基于ubantu的渗透演练环境.Rapid7官方长时间未更新,导致跟不上当前的节奏.metasploitable3出世. metasploitable2配合metaspl ...

  8. Git006--管理修改

    Git--管理修改 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ...

  9. Git005--工作区和暂存区

    Git--工作区和暂存区 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b0 ...

  10. JavaScript Sort

    function ArrayList() { var array = []; this.swap = function(index1, index2) { var aux = array[index1 ...