您的位置:首页 > 编程语言 > Java开发

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

2016-03-14 14:35 549 查看
第一部分:通过自定义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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: