SpringMVC 类内部的RequestMapping注解能否被继承?
首先注意标题,说的是类内部的注解
结论是:
不能,但是子类却可以享有父类中该注解带来的效果。
看了一下这个:http://elf8848.iteye.com/blog/1621392
自己也试了一下,发现子类如果覆盖父类的方法,确实不能继承被覆盖方法的注解。
但是试了一下spring的注解,即便该注解没有被继承到子类上,子类同样能享有这个注解带来的效果,这可能和spring的注解扫描和bean加载机制有关,有时间看看源码吧,这里先记一下。
以下是实验时写的代码:
父类:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.PostConstruct;
import java.util.UUID; @RestController
@ConditionalOnMissingBean(name = "extDemoController")
public class DemoController {
@PostConstruct
public void init(){
System.out.println("---------------DemoController init---------------");
}
@Autowired
protected OneBean oneBean;
@RequestMapping("/random")
public String random(){
return UUID.randomUUID().toString();
}
}
子类:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.UUID; @RestController
public class ExtDemoController extends DemoController { public void init(){
System.out.println("---------------ExtDemoController init---------------");
} public String random(){
System.out.println(oneBean.hello());
return "random from ExtDemoController";
} }
测试代码1:
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import java.lang.reflect.Method;
public class CustomTest {
public static void main(String[] args) throws NoSuchMethodException {
Class<ExtDemoController> clazz = ExtDemoController.class;
Method method = clazz.getMethod("random",new Class[]{});
if(method.isAnnotationPresent(RequestMapping.class)){
RequestMapping oneAnnotation = method.getAnnotation(RequestMapping.class);
System.out.println(oneAnnotation.name());
}
}
}
测试代码2:
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; @SpringBootApplication
//@EnableConfigurationProperties(FirstConfig.class)
public class DemoApplication { public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
for(String name:ctx.getBeanDefinitionNames()){
System.out.println(name);
}
}
}
测试说明:
这是一个普通的springboot项目,maven里只引入了
spring-boot-starter-web
项目可以用 http://start.spring.io/ 生成。
测试步骤:
执行测试代码1,发现注解不能被子类继承。
执行测试代码2,发现子类虽未继承父类内部的三种注解,但是却完全可以享有它们的功能。即访问 /random 执行的是子类内的逻辑。
SpringMVC 类内部的RequestMapping注解能否被继承?的更多相关文章
- SpringMVC源码解读 - RequestMapping注解实现解读 - RequestMappingInfo
使用@RequestMapping注解时,配置的信息最后都设置到了RequestMappingInfo中. RequestMappingInfo封装了PatternsRequestCondition, ...
- SpringMVC源码解读 - RequestMapping注解实现解读 - RequestCondition体系
一般我们开发时,使用最多的还是@RequestMapping注解方式. @RequestMapping(value = "/", param = "role=guest& ...
- SpringMVC源码解读 - RequestMapping注解实现解读
SpringMVC源码解读 - RequestMapping注解实现解读 - RequestCondition体系 https://www.cnblogs.com/leftthen/p/520840 ...
- SpringMVC源码解读 - RequestMapping注解实现解读 - ConsumesRequestCondition
consumes 指定处理请求的提交内容类型(media-Type),例如application/json, text/html. 所以这边的ConsumesRequestCondition就是通过 ...
- SpringMVC 和SpringBoot中的注解是如何起作用的,如何实现的
SpringMVC源码解读 - HandlerMapping - RequestMappingHandlerMapping初始化 https://www.cnblogs.com/leftthen/ ...
- SpringMVC中@Controller和@RequestMapping用法和其他常用注解
一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...
- SpringMVC 学习笔记(二) @RequestMapping、@PathVariable等注解
版权声明:本文为博主原创文章,博客地址:http://blog.csdn.net/a67474506?viewmode=contents 1.1. @RequestMapping映射请求 Spring ...
- (转)SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解
http://blog.csdn.net/yerenyuan_pku/article/details/72511749 高级参数绑定 现在进入SpringMVC高级参数绑定的学习,本文所有案例代码的编 ...
- SpringMVC中@Controller和@RequestMapping用法和其他常用注解(转)
一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...
随机推荐
- 手机网站和PC网站兼容的响应式网页设计
今天跟大家介绍的这个网站叫 媒体查询 官网域名:http://mediaqueri.es/ 该酷站收集了很多响应式设计的案例.全部都是收集的一些励志精美而时尚的网站,使用媒体查询和响应的网页设计. ...
- Android图片加载框架最全解析(七),实现带进度的Glide图片加载功能
我们的Glide系列文章终于要进入收尾篇了.从我开始写这个系列的第一篇文章时,我就知道这会是一个很长的系列,只是没有想到竟然会写这么久. 在前面的六篇文章中,我们对Glide的方方面面都进行了学习,包 ...
- Windows Server 2003 下实现网络负载均衡(2) (转)
四.测试 在第一台机器上,关闭网络负载平衡管理器后,用鼠标右键单击“网络负载平衡群集”,从出现的菜单中选择“连接到现存的”,将会弹出“连接”界面.输入第一台计算机的名称或IP地址,点击“连接”按钮,在 ...
- [Linux] Systemd 入门教程:实战篇
reference : http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html 上一篇文章,我介绍了 Systemd ...
- 理解Python命名机制
理解Python命名机制 本文最初发表于恋花蝶的博客(http://blog.csdn.net/lanphaday),欢迎转载,但必须保留此声明且不得用于商业目的.谢谢. 引子 我热情地邀请大家猜测下 ...
- 机器学习算法之旅A Tour of Machine Learning Algorithms
In this post we take a tour of the most popular machine learning algorithms. It is useful to tour th ...
- 【BZOJ】【3503】【CQOI2014】和谐矩阵
高斯消元解Xor方程组 Orz ZYF o(︶︿︶)o 唉我的数学太烂了…… 错误思路:对每个格点进行标号,然后根据某5个异或和为0列方程组,高斯消元找自由元……(目测N^3会TLE) ZYF的正确思 ...
- python 数据处理中的 LabelEncoder 和 OneHotEncoder
One-Hot 编码即独热编码,又称一位有效编码,其方法是使用N位状态寄存器来对N个状态进行编码,每个状态都由他独立的寄存器位,并且在任意时候,其中只有一位有效.这样做的好处主要有:1. 解决了分类器 ...
- 剑指offer-序列化二叉树
请实现两个函数,分别用来序列化和反序列化二叉树 以前提交的内存超出了,可能现在要用非递归实现了 #include<iostream> #include<math.h> #inc ...
- 是否应该将SAN上的SQL Server中的user database的data文件, log文件和TempDB文件放在不同的LUN上?
请看下面的两个精彩解答: 解答1: If your SAN has performance and availability algorithms built into the management ...