通过反射获取SSM的controller层的注解以及注解中的value值
package com.reflection.test; import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.interview.Controller.ITVCController; public class Demo {
public static void main(String[] args) throws Exception, InvocationTargetException { //通过反射获取类的结构
Class<?> itvClass = ITVCController.class; //以下是类的注解信息 以及value值
System.out.println("以下是类的注解信息 以及value值"); //获取类的注解信息
Annotation[] annotations = itvClass.getAnnotations(); //遍历注解
for (Annotation annotation : annotations) { System.out.println(annotation.annotationType().getName()); //通过反射获取注解的类的结构信息
Class<? extends Annotation> annClass = annotation.getClass(); //获取注解的类的所有方法
Method[] methods = annClass.getDeclaredMethods(); //遍历注解的方法
for (Method annMethod : methods) { if("value".equals(annMethod.getName())){ //获取value值
Object invoke = annMethod.invoke(annotation); Class<? extends Object> class1 = invoke.getClass(); // 由于我测试的类是SSM的controller层 而controller层的类注解有 @Controller(value="测试")
// @RequestMapping(value="calendar")
// Controller 的value是String类型 RequestMapping 的value是String[]类型 所以我在猜出进行了一次判断 if("class java.lang.String".equals(class1.toString())){
System.out.println(annMethod.getName()+"====>"+invoke);
}else{
String[] Strings = (String[])invoke;
for (String string : Strings) {
System.out.println(annMethod.getName()+"====>"+string);
}
}
}else{ //输出该注解的方法名
System.out.println(annMethod.getName()); }
}
} //以下是类的方法的注解信息 以及value值
System.out.println("以下是类的方法的注解信息 以及value值"); //获取类的所有方法
Method[] methods = itvClass.getDeclaredMethods(); for (Method method : methods) { //以下注释同上
Annotation[] metannotation = method.getAnnotations(); for (Annotation annotation : metannotation) {
System.out.println(annotation.annotationType().getName());
Class<? extends Annotation> annClass = annotation.getClass();
Method[] annMethods = annClass.getDeclaredMethods();
for (Method annMethod : annMethods) {
if("value".equals(annMethod.getName())){
String[] invoke = (String[]) annMethod.invoke(annotation);
for (String string : invoke) {
System.out.println(annMethod.getName()+"====>"+string);
System.out.println();
}
}
}
}
} }
}
通过反射获取SSM的controller层的注解以及注解中的value值的更多相关文章
- spring security 在controller层 方法级别使用注解 @PreAuthorize("hasRole('ROLE_xxx')")设置权限拦截 ,无权限则返回403
1.前言 以前学习的时候使用权限的拦截,一般都是对路径进行拦截 ,要么用拦截器设置拦截信息,要么是在配置文件内设置拦截信息, spring security 支持使用注解的形式 ,写在方法和接口上拦截 ...
- 使用抽象工厂反射获取不到Dal层对象,未能加载文件或程序集......
Put aside the fog and see the essence 解决问题之前,要明白问题为什么会出现 我相信能点开这篇帖子的人,都是具有探索精神的人,因为,只有心存疑问才会搜索 如果只想单 ...
- .NET MVC通过反射获取数据修改历史记录,并插入数据表中
本文属于原创,转载时请标明出处! 折磨了我一个晚上的问题,奈何对物理的反射印象太深了,整天去想着物理的反射.折射怎么解.感谢少将哥哥给我的指点,经过一个晚上对反射的恶补,最终搞定了.纪念一下. 1.核 ...
- Java自定义注解和运行时靠反射获取注解
转载:http://blog.csdn.net/bao19901210/article/details/17201173/ java自定义注解 Java注解是附加在代码中的一些元信息,用于一些工具在编 ...
- Spring MVC中,事务是否可以加在Controller层
一般而言,事务都是加在Service层的,但是爱钻牛角尖的我时常想:事务加在Controller层可不可以.我一直试图证明事务不止可以加在Service层,还可以加在Controller层,但是没有找 ...
- SpringBoot测试Controller层
一.准备工作 1.导入测试依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...
- spring Controller 层注解获取 properties 里面的值
前言:最近在做一个项目,想要在 controller 层直接通过注解 @Value("")来获取 properties 里面配置的属性. 这个其实和 springmvc.sprin ...
- 深入理解--SSM框架中Dao层,Mapper层,controller层,service层,model层,entity层都有什么作用
SSM是sping+springMVC+mybatis集成的框架. MVC即model view controller. model层=entity层.存放我们的实体类,与数据库中的属性值基本保持一致 ...
- token获取在controller层中
集合判断是否为空 注意:token获取在controller层中,token中存的所有数据都要在controller中获取 在自己的接口里调用别的接口需要判断一下返回值是否为空
随机推荐
- Codeforces Round #313 (Div. 1) A. Gerald's Hexagon
Gerald's Hexagon Problem's Link: http://codeforces.com/contest/559/problem/A Mean: 按顺时针顺序给出一个六边形的各边长 ...
- Ext.NET 4.1.0 搭建页面布局
Ext.NET目前的最新版本为4.1.0,可以从官网:ext.net上下载,具体下载网址为:http://ext.net/download/. 文件下载下来后,在\lib\目录下存在3个文件夹,分别对 ...
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest, B. Layer Cake
Description Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping ...
- 一、MyBatis简介与配置MyBatis+Spring+MySql
//备注:该博客引自:http://limingnihao.iteye.com/blog/106076 1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架 ...
- 深入.NET框架
.NET是微软公司在2000年推出的一个战略(平台). 其目的就是想 任何人使用任何终端设备在任何地方都可以访问微软提供的服务. .NET Framework两大组件: CLR(Common Lang ...
- 批处理清除VisualStudio解决方案文件夹
有很多次我们需要手工来删除bin, obj这样的文件夹.这些文件夹是由Visual Studio编译项目时生成的,其中包括了当前项目的程序集.一个解决方案会包含好多个项目了,那么就有很多这样 ...
- 「C语言」int main还是void main?
从大一入学刚接触C到现在已满7个月了,虽然刚开始就知道```int main```才是标准的写法,但一直没有深刻理解为什么不能用```void main```而必须使用```int main```. ...
- 【GOF23设计模式】组合模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_组合模式.树状结构.杀毒软件架构.JUnite底层架构.常见开发场景 package com.test.composite ...
- Selenium&EmguCV实现爬虫图片识别
概述 爬虫需要抓取网站价格,与一般抓取网页区别的是抓取内容是通过AJAX加载,并且价格是通过CSS背景图片显示的. 每一个数字对应一个样式,如'p_h57_5' .p_h57_5 { backgrou ...
- 新技能,利用Reflector来修改dll引用
继上次<ArcObject10.1降级至10.0>又遇到版本降级问题.通常的方式有: 方案一:重新编译 将源代码加载到解决方案中,修改相应dll的版本,比较快捷的方式是多选后,设置属性中特 ...