功能需求

  1. 提供一个公共的jar包给其他业务模块依赖,需要在这个公共的jar中暴露一个restful API

  2. 采用spring auto config机制,在公共jar包中定义spring.factories文件,将jar包需要注入到spring容器中的bean定义好,业务模块依赖后直接使用,不需要额外定义bean,也不需要指定ComponentScan

之前做法:根据spring文档给的方案调用RequestMappingHandlerMapping的registerMapping方法手动注册一个mapping,可以不使用@Controller注解就可以追加一个rest 接口,可是spring 5之后,spring推出了spring web flux,而RequestMappingHandlerMapping也分成了是spring webmvc版和spring webflux两个,我们给定的jar又不能限定业务模块使用spring web还是spring web flux开发,所以这种方式就不适用了。

解决方式

我们知道,无论是webmvc还是webflux中的RequestMappingHandlerMapping类,都是在afterPropertiesSet方法中查找所有带有Controller或者RequestMapping注解的类,再把对应类中的带有RequestMapping注解的方法解析后注册到对应的RequestMappingHandlerMapping中的,其中判断一个类是否带有@Controller或@RequestMapping的方法如下

@Override
protected boolean isHandler(Class<?> beanType) {
return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}

对应的AnnotatedElementUtils.hasAnnotation方法,最终会调用到AnnotatedElementUtils.searchWithFindSemantics方法,代码片段如下

  else if (element instanceof Class) {
Class<?> clazz = (Class<?>) element;
if (!Annotation.class.isAssignableFrom(clazz)) {
// Search on interfaces 在实现接口中查找
for (Class<?> ifc : clazz.getInterfaces()) {
T result = searchWithFindSemantics(ifc, annotationTypes, annotationName,
containerType, processor, visited, metaDepth);
if (result != null) {
return result;
}
}
// Search on superclass 在父类中查找
Class<?> superclass = clazz.getSuperclass();
if (superclass != null && superclass != Object.class) {
T result = searchWithFindSemantics(superclass, annotationTypes, annotationName,
containerType, processor, visited, metaDepth);
if (result != null) {
return result;
}
}
}
}

发现这个地方查找是否有指定注解时,如果继承的类或实现的接口有相应的注解也是可以的,利用这个特性,我们可以采用如下思路来实现。

  1. 定义一个标记Controller,里面什么方法也没有,仅仅追加了一个注解

    @RestController
    public class MarkController { }
  2. 定义具体的Controller继承这个标记类,注意这个类不需要用RestController注解

public class HelloController extends MarkController {

	@RequestMapping("/hello")
public String hello() {
return "hello";
}

}


3. 在一个Configuration类中用@Bean注解声明这个类 ```
@Configuration
public class BeanConfig { @Bean
public HelloController helloController() {
return new HelloController();
} } ``` 这样我们就可以通过@Bean的形式声明Controller,之后把这个BeanConfig直接追加到spring.factories中,其他模块依赖这个jar之后,自动就会有一个/hello的接口了。

spring boot通过@Bean注解定义一个Controller的更多相关文章

  1. Spring Boot 二十个注解

    Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...

  2. spring boot 中@Autowired注解无法自动注入的错误

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...

  3. Spring Boot中@Scheduled注解的使用方法

    Spring Boot中@Scheduled注解的使用方法 一.定时任务注解为@Scheduled,使用方式举例如下 //定义一个按时间执行的定时任务,在每天16:00执行一次. @Scheduled ...

  4. spring boot @ConditionalOnxxx相关注解总结

    Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ...

  5. Spring boot 使用的注解有哪些?

    Spring boot 使用的注解有哪些? 注解 作用 @SpringBootApplication 等价于 @Configuration + @EnableAutoConfiguration + @ ...

  6. (32)Spring Boot使用@SpringBootApplication注解,从零开始学Spring Boot

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论] 如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章. 很多Spring Boot开发者总是使用 ...

  7. Spring Boot 计划任务中的一个“坑”

    计划任务功能在应用程序及其常见,使用Spring Boot的@Scheduled 注解可以很方便的定义一个计划任务.然而在实际开发过程当中还应该注意它的计划任务默认是放在容量为1个线程的线程池中执行, ...

  8. spring boot开发,jar包一个一个来启动太麻烦了,写一个bat文件一键启动

    spring boot开发,jar包一个一个来启动太麻烦了,写一个bat文件一键启动 @echo offcd D:\workProject\bushustart cmd /c "title ...

  9. Spring Boot 的核心注解是哪个?它主要由哪几个注解组成的?

    启动类上面的注解是@SpringBootApplication,它也是 Spring Boot 的核心注解,主要组合包含了以下 3 个注解: @SpringBootConfiguration:组合了 ...

随机推荐

  1. PHP两个变量值互换(不用第三变量)

    <?php /**  * 双方变量为数字或者字符串时  * 使用list()和array()方法可以达到交换变量值得目的  */ $a = "This is A"; // a ...

  2. Laravel修改配置后一定要清理缓存 "php artisan config:clear"!

    用laravel踩到一个大坑... 需要使用laravel的队列(queue)功能, 设置 ".env"配置文件 QUEUE_DRIVER=database 按照文档,建立jobs ...

  3. [转]关于SSH与SSM的组成及其区别

    前言 当下SpringBoot盛行,咱再聊聊SpringBoot盛行之前的框架组合,当做复习巩固哈. 在聊之前,得先说说MVC,MVC全名是Model View Controller,是模型(mode ...

  4. React 简书

    create-react-app   jianshu yarn add styled-components -D       利用js写css样式  样式会更高效 https://github.com ...

  5. Jmeter 发送json

    阅读更多 使用jmeter发送json数据.方法有三种 原创,转载请注明出处 1.利用CSV Data set Config. 参考: http://demi-panda.com/2013/01/08 ...

  6. JS的防抖与节流学习笔记

    防抖(debounce):当持续触发事件时,在一定的时间段内,只有最后一次触发的事件才会执行. 例: function debounce(fn, wait) { var timer = null; r ...

  7. Linux 标准 C 类型的使用

    尽管大部分程序员习惯自由使用标准类型, 如 int 和 long, 编写设备驱动需要一些小心 来避免类型冲突和模糊的 bug. 这个问题是你不能使用标准类型, 当你需要"一个 2-字节 填充 ...

  8. vue-learning:39 - router - vue-router的基本使用

    vue-router路由的基本使用 一张图阐述vue-router的基本使用步骤 // 0. 如果全局使用CDN引入:vue 引入在前,vue-router引入在后 // <script src ...

  9. python数据分析经常使用的库

    这个列表包含数据分析经常使用的Python库,供大家使用.1. 网络通用urllib -网络库(stdlib).requests -网络库.grab – 网络库(基于pycurl).pycurl – ...

  10. Windows Server Core Remote Manage Hyper-V

    原帖:https://serverfault.com/questions/852144/how-do-i-remotely-manage-hyper-v-2016-standalone-via-win ...