maven 项目(四) spring集成springMVC开发统一接入API(准备工作:第一部分)

第一部分:通过自定义Annotation,获取所有Controller的Url

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);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值