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值的更多相关文章

  1. spring security 在controller层 方法级别使用注解 @PreAuthorize("hasRole('ROLE_xxx')")设置权限拦截 ,无权限则返回403

    1.前言 以前学习的时候使用权限的拦截,一般都是对路径进行拦截 ,要么用拦截器设置拦截信息,要么是在配置文件内设置拦截信息, spring security 支持使用注解的形式 ,写在方法和接口上拦截 ...

  2. 使用抽象工厂反射获取不到Dal层对象,未能加载文件或程序集......

    Put aside the fog and see the essence 解决问题之前,要明白问题为什么会出现 我相信能点开这篇帖子的人,都是具有探索精神的人,因为,只有心存疑问才会搜索 如果只想单 ...

  3. .NET MVC通过反射获取数据修改历史记录,并插入数据表中

    本文属于原创,转载时请标明出处! 折磨了我一个晚上的问题,奈何对物理的反射印象太深了,整天去想着物理的反射.折射怎么解.感谢少将哥哥给我的指点,经过一个晚上对反射的恶补,最终搞定了.纪念一下. 1.核 ...

  4. Java自定义注解和运行时靠反射获取注解

    转载:http://blog.csdn.net/bao19901210/article/details/17201173/ java自定义注解 Java注解是附加在代码中的一些元信息,用于一些工具在编 ...

  5. Spring MVC中,事务是否可以加在Controller层

    一般而言,事务都是加在Service层的,但是爱钻牛角尖的我时常想:事务加在Controller层可不可以.我一直试图证明事务不止可以加在Service层,还可以加在Controller层,但是没有找 ...

  6. SpringBoot测试Controller层

    一.准备工作 1.导入测试依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  7. spring Controller 层注解获取 properties 里面的值

    前言:最近在做一个项目,想要在 controller 层直接通过注解 @Value("")来获取 properties 里面配置的属性. 这个其实和 springmvc.sprin ...

  8. 深入理解--SSM框架中Dao层,Mapper层,controller层,service层,model层,entity层都有什么作用

    SSM是sping+springMVC+mybatis集成的框架. MVC即model view controller. model层=entity层.存放我们的实体类,与数据库中的属性值基本保持一致 ...

  9. token获取在controller层中

    集合判断是否为空 注意:token获取在controller层中,token中存的所有数据都要在controller中获取 在自己的接口里调用别的接口需要判断一下返回值是否为空

随机推荐

  1. Winform开发框架之字典管理模块的更新,附上最新2013年全国最新县及县以上行政区划代码sql脚本

    在很多项目里面,字典管理是必备的项目模块,而这个又是比较通用的功能,因此可以单独做成一个通用字典管理,例如这个模块,可以通过集成的方式,使用在我的<Winform开发框架>.<WCF ...

  2. 【原创】有关Silverlight控件DataGrid的绑定数据后单元格单独复制的功能实现分析

    前些日子,公司新需求需要对silverlight的datagrid进行局部任意单元格数据可复制,查阅了半天网络资料愣是没找到相关资料,开始还以为是silverlight的bug根部无法实现, 最后还是 ...

  3. sencha 报错问题汇总

    store的url必填 否则报错:Uncaught TypeError: Cannot read property 'indexOf' of undefined ext-all.js store必须在 ...

  4. 【JS复习笔记】07 复习感想

    好吧,其实<JavaScript语言精粹>后面还简单介绍了代码风格,优美特性,以及包含的毒瘤.糟粕. 但我很快就看完了,发现其实都在前面讲过了,所以就不写了. 至今为止已经算是把JavaS ...

  5. ati显卡驱动后,性能不咋地

    ubuntu装了ati的闭源驱动后,拖动窗口,会有明显的断裂感,不够平滑

  6. MyBatis中的特殊符号[20160713]

    今天中午回到工位已经是12:20多了,没有时间睡觉了,本想着还能提前开始,结果看了点新闻之后,又是12:40了,所以新闻坚决不能看,执行力. 今天主要记录一下MyBatis中的特殊符号的问题,这个问题 ...

  7. PHP学习笔记:删除与销毁session

    删除某个session值可以使用PHP的unset函数,删除后就会从全局变量$_SESSION中去除,无法访问. session_start(); $_SESSION['name'] = 'jobs' ...

  8. [.NET] 使用C#开发SQL Function来提供服务 - 简讯发送

    [.NET] 使用C#开发SQL Function来提供服务 - 简讯发送 范例下载 范例程序代码:点此下载 问题情景 在「使用C#开发SQL Function来提供数据 - 天气预报」这篇文章中,介 ...

  9. [CLK Framework] CLK.Settings - 跨平台的参数存取模块

    [CLK Framework] CLK.Settings - 跨平台的参数存取模块 问题情景 开发功能模块的时候,常常免不了有一些参数(例如ConnectionString),需要存放在Config檔 ...

  10. Error occurred in deployment step 'Add Solution': Operation is not valid due to the current state of the object.

    Sharepoint 部署的时候出现一个错误 Error occurred in deployment step 'Add Solution': Operation is not valid due ...