maven 项目(四) spring集成springMVC开发统一接入API(准备工作:第一部分)
2016-03-14 14:35
549 查看
第一部分:通过自定义Annotation,获取所有Controller的Url
1、自定义Annotation接口:
2、普通接口一般写法(知道为毛这么爱用接口吗:度娘一下):
2.1 在需要获取Url的Controller上加上自定义Annotation
2.2 利用Java反射原理读取Annotation
注:以上代买的意义是讲Annotation的类和方法加载map中。
好处自然,就是在获取的时候不用再次加载;map作为类似缓存,方便取值;
这行代码以后将会出现:
public static Object getBeanByFunctionCode(String functionCode) {
return functionCodeBeanMap.get(functionCode);
}
1、自定义Annotation接口:
@Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) public @interface FunctionCode { String value(); String descript(); }
2、普通接口一般写法(知道为毛这么爱用接口吗:度娘一下):
public interface JdpushService { ApiResponse<HashMap<String,Object>> doJdpush(ApiRequest apiRequest) throws Throwable; }
2.1 在需要获取Url的Controller上加上自定义Annotation
@FunctionCode(value = "jdpush", descript = "推送相关API") @Service public class JdpushServiceImpl implements JdpushService { @FunctionCode(value = "jdpush.doJdpush", descript = "推送处理接口") @Override public ApiResponse<HashMap<String,Object>> doJdpush(ApiRequest apiRequest) throws Exception { ApiResponse<HashMap<String,Object>> apiResponse =null; boolean flag=false; try { flag=Jdpush.SendPush("这是一条推送消息"); } if (flag==true) { apiResponse = new ApiResponse<HashMap<String, Object>>(RestResultEnum.SUCCESS, 1, hashMap); }else{ apiResponse = new ApiResponse<HashMap<String, Object>>(RestResultEnum.FAIL, 1, hashMap); } }catch (Exception ex){ ex.printStackTrace(); apiResponse=new ApiResponse<HashMap<String,Object>>(RestResultEnum.UNKNOW_ERROR); } return apiResponse; }
2.2 利用Java反射原理读取Annotation
public class SpringBeanProxy { private static ApplicationContext applicationContext; private static Map<String, Object> functionCodeBeanMap = new HashMap<String, Object>(); private static Map<String, Method> functionCodeMethodMap = new HashMap<String, Method>(); private static Map<String, String> functionCodeCatalogMap = new HashMap<String, String>(); private static Map<String, Map<String, String>> functionCodeListMap = new HashMap<String, Map<String, String>>(); //公共静态方法:项目启动。初始化加载Annotation的接口 public synchronized static void setApplicationContext(ApplicationContext arg0) { applicationContext = arg0; Map<String, Object> tempMap = applicationContext.getBeansWithAnnotation(FunctionCode.class);//读取注解bean if (tempMap != null && tempMap.size() > 0) { for (Map.Entry<String, Object> entry : tempMap.entrySet()) { Object bean = entry.getValue(); FunctionCode beanFc = bean.getClass().getAnnotation(FunctionCode.class);//反射bean if (beanFc != null) { String beanFunctionCode = beanFc.value();//读取定义的值 functionCodeBeanMap.put(beanFunctionCode, bean); functionCodeCatalogMap.put(beanFunctionCode, beanFc.descript());//构造目录结构 Method[] methodArr = bean.getClass().getDeclaredMethods();//获取类或接口声明的所有方法 if (methodArr != null && methodArr.length > 0) { Map<String, String> methodFunctionCodeMap = new HashMap<String, String>(); for (Method method : methodArr) { FunctionCode methodFc = method.getAnnotation(FunctionCode.class); if (methodFc != null) { String methodFunctionCode = methodFc.value(); functionCodeMethodMap.put(methodFunctionCode, method);//code对应方法 methodFunctionCodeMap.put(methodFunctionCode, methodFc.descript());//code对应描述 } } functionCodeListMap.put(beanFunctionCode, methodFunctionCodeMap);//code对应对象 } } } } } public static Object getBean(String beanName) { return applicationContext.getBean(beanName); } public static Object getBeanByFunctionCode(String functionCode) { return functionCodeBeanMap.get(functionCode); } public static Method getMethodByFunctionCode(String functionCode) { return functionCodeMethodMap.get(functionCode); } public static Map<String, String> getFunctionCodeCatalogMap() { return functionCodeCatalogMap; } public static Map<String, Map<String, String>> getFunctionCodeListMap() { return functionCodeListMap; } }
注:以上代买的意义是讲Annotation的类和方法加载map中。
好处自然,就是在获取的时候不用再次加载;map作为类似缓存,方便取值;
这行代码以后将会出现:
public static Object getBeanByFunctionCode(String functionCode) {
return functionCodeBeanMap.get(functionCode);
}
相关文章推荐
- 【转】myeclipse 上安装 Maven3
- Myeclipse去除空行
- 简单归并算法-有序数组合并算法实现
- 用JAVA打暴雪星际争霸(1)--搭建开发环境
- [疯狂Java]AWT剪切板:图像传递
- Java中BIO,NIO,AIO的理解
- 【Junit】JUnit-4.12使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
- Java反射及其在开发数据库上面的运用
- springmvc中使用aop实现登录验证
- spring常用注解
- java微信开发-配置SpringMVC
- struts2注解配置
- SpringMvc学习心得(三) spring实例化JavaBean的过程
- Java中存在着两种Random函数
- Java设计模式透析之 —— 模板方法(Template Method)
- java native关键词
- 自动化测试之路——EMMA在java代码覆盖率自动化测试中的应用(三)
- 如何写第一个RMI程序?
- eclipse:Some projects cannot be imported because they already exist in the works
- 开始Java之旅