Spring MVC 反射对象和依赖注入
今天,上班写接口的时候,和同事讨论,
是否所有接口能调用同一个地址,通过key作为参数,把模块名和方法名传入,把模块名称和方法名通过反射机制,获取到对应模块名下的方法,通过对应的参数返回想要的结果
讨论下来,发现貌似是可以的,说做就做,先写个Demo
传入json:
{ "action": "UserWebService-login", "timestamp": "1452223478", "gsonValue": "", "appkey": "123", "nonceStr": "06e2fe8b-1e33-4a80-bdc3-4055b1459388", "signature": "3886041eb6336b6f190cc298fb83ce63", "content": { "C_ACCOUNTNAME": "kate", "C_PASSWORD": "1" } }
action就是这个模块名和方法名的结合UserWebService-login
模块UserWebService,方法login
通过这个key,还有对应的参数,请求UserWebService下的login,返回对应的结果集
1 @Resource 2 HttpServletRequest request; 3 4 public Object UserWebService(ModleForm form,HttpServletRequest req) throws Exception{ 5 return returnMethodValue(form, req, UserWebService.class,userWebService); 6 } 7 8 /** 9 * @Title: returnMethodValue 10 * @Description: TODO(通过json,获取方法名,获取接口数据) 11 * @param: @param form 12 * @param: @param req 13 * @param: @param clazz 14 * @param: @return 15 * @param: @throws Exception 16 * @return: Object 17 * @throws 18 */ 19 public Object returnMethodValue(ModleForm form,HttpServletRequest req,Class<?> clazz) throws Exception{ 20 JSONObject jsonObject = new JSONObject(form.getJson()); 21 String action = (String) jsonObject.get("action"); 22 String Method = (String) StringUtils.returnObjectByLength(StringUtils.toSpilt(action, "-"),1); 23 Method actionMethod = clazz.getMethod(Method,ModleForm.class,HttpServletRequest.class); 24 Object object = actionMethod.invoke(clazz.getClass().newInstance(), form , req); 25 return object; 26 } 27 28 29 public Map<String,Object> getMapByObject(Object object){ 30 Map<String,Object> map = (Map<String, Object>) object; 31 return map; 32 } 33 34 35 @ResponseBody 36 @RequestMapping(value = "/mobile_user/entrance") 37 public String entrance() { 38 Object object = null; 39 String json = request.getParameter("json"); 40 JSONObject jsonObject = new JSONObject(json); 41 ModleForm modleForm = new ModleForm(); 42 modleForm.setJson(json); 43 Map<String,Object> map = new HashMap<String, Object>(); 44 //验证 45 boolean flag = Verification(json); 46 //返回错误码 47 map = getErrorCode(flag,new ModleForm()); 48 try { 49 String action = (String) jsonObject.get("action"); 50 List<Object> list = StringUtils.toSpilt(action, "-"); 51 String Modular = (String) StringUtils.returnObjectByLength(list,0); 52 53 Method MobileMethod = MobileWebService.class.getMethod(Modular,ModleForm.class,HttpServletRequest.class); 54 object = MobileMethod.invoke(MobileWebService.class.newInstance(), modleForm , request); 55 map = getMapByObject(object); 56 } catch (Exception e) { 57 // TODO Auto-generated catch block 58 e.printStackTrace(); 59 map = this.returnErrorCode(new ModleForm().getComponentId()+"-1101", getMesssage(new ModleForm().getComponentId()+"-1101")); 60 return gsonUtils.toJson(map); 61 } 62 return gsonUtils.toJson(map); 63 }
但是,重点来了,因为是用的SpringMVC
MobileMethod.invoke(MobileWebService.class.newInstance(), modleForm , request);
Object object = actionMethod.invoke(clazz.getClass().newInstance(), form , req);
这样写,手动new一个对象,SpringMVC是不会根据这个对象,依赖注入后面的service,dao之类的东西的,而我的UserWebService中就有对应的service需要依赖注入!
怎么办呢,尝试了各种办法,找资料,突然想到@Resource:
@Resource 注解被用来激活一个命名资源(named resource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI context中的一个对象。 Spring确实支持使用@Resource通过JNDI lookup来解析对象,默认地,拥有与@Resource注解所提供名字相匹配的“bean name(bean名字)”的Spring管理对象会被注入。 在下面的例子中,Spring会向加了注解的setter方法传递bean名为“dataSource”的Spring管理对象的引用。
他可以帮助我们来依赖注入,那是不是我可以不去手动new一个对象呢,说来就来
1 @Resource 2 MobileWebService mobileWebService; 3 4 5 @Resource 6 UserWebService userWebService; 7 8 9 10 @Resource 11 HttpServletRequest request; 12 13 public Object UserWebService(ModleForm form,HttpServletRequest req) throws Exception{ 14 return returnMethodValue(form, req, UserWebService.class,userWebService); 15 } 16 17 /** 18 * @Title: returnMethodValue 19 * @Description: TODO(通过json,获取方法名,获取接口数据) 20 * @param: @param form 21 * @param: @param req 22 * @param: @param clazz 23 * @param: @return 24 * @param: @throws Exception 25 * @return: Object 26 * @throws 27 */ 28 public Object returnMethodValue(ModleForm form,HttpServletRequest req,Class<?> clazz,Object classObject) throws Exception{ 29 JSONObject jsonObject = new JSONObject(form.getJson()); 30 String action = (String) jsonObject.get("action"); 31 String Method = (String) StringUtils.returnObjectByLength(StringUtils.toSpilt(action, "-"),1); 32 Method actionMethod = clazz.getMethod(Method,ModleForm.class,HttpServletRequest.class); 33 Object object = actionMethod.invoke(classObject, form , req); 34 return object; 35 } 36 37 38 public Map<String,Object> getMapByObject(Object object){ 39 Map<String,Object> map = (Map<String, Object>) object; 40 return map; 41 } 42 43 44 @ResponseBody 45 @RequestMapping(value = "/mobile_user/entrance") 46 public String entrance() { 47 Object object = null; 48 String json = request.getParameter("json"); 49 JSONObject jsonObject = new JSONObject(json); 50 ModleForm modleForm = new ModleForm(); 51 modleForm.setJson(json); 52 Map<String,Object> map = new HashMap<String, Object>(); 53 //验证 54 boolean flag = Verification(json); 55 //返回错误码 56 map = getErrorCode(flag,new ModleForm()); 57 try { 58 String action = (String) jsonObject.get("action"); 59 List<Object> list = StringUtils.toSpilt(action, "-"); 60 String Modular = (String) StringUtils.returnObjectByLength(list,0); 61 62 Method MobileMethod = MobileWebService.class.getMethod(Modular,ModleForm.class,HttpServletRequest.class); 63 object = MobileMethod.invoke(mobileWebService, modleForm , request); 64 map = getMapByObject(object);//转成map 65 } catch (Exception e) { 66 // TODO Auto-generated catch block 67 e.printStackTrace(); 68 map = this.returnErrorCode(new ModleForm().getComponentId()+"-1101", getMesssage(new ModleForm().getComponentId()+"-1101")); 69 return gsonUtils.toJson(map); 70 } 71 return gsonUtils.toJson(map); 72 }
这样的话,我们的反射机制和依赖注入就完成了,所有的接口都通过这个入口
http://localhost:8080/xxx/mobile_user/entrance.bk?json={"action":"UserWebService-login","timestamp":"1452223478","gsonValue":"","appkey":"123","nonceStr":"06e2fe8b-1e33-4a80-bdc3-4055b1459388","signature":"3886041eb6336b6f190cc298fb83ce63","content":{"C_ACCOUNTNAME":"kate","C_PASSWORD":"1"}}
返回我想要的结果
这样做的好处呢是:对应的,我的工作量就变小了,在入口处写 接口日志,获取session信息,返回错误码,接口验证我都能在一个地方完成,而不需要在每一个Controller对应的方法里面写很多类似的代码,我的方法中只需要写相关逻辑