springboot controller调用service,springboot自动生成controller
目录
手动动态注入控制器和服务在控制器中使用参数说明
手动动态注入controller和service
封装测试;导入org。spring框架。豆子。工厂。注释。自动连线;导入org。spring框架。豆子。工厂。配置。bean定义;导入org。spring框架。豆子。工厂。支持。bean定义生成器;导入org。spring框架。豆子。工厂。支持。defaultlistablebeanfactory导入org。spring框架。靴子。春季申请;导入org。spring框架。靴子。自动配置。弹簧启动应用程序;导入org。spring框架。语境。应用程序上下文;导入org。spring框架。语境。可配置的应用程序上下文;导入org。spring框架。核心。io。文件系统资源;导入org。spring框架。核心。io。文件系统资源加载器;导入org。spring框架。刻板印象。组件;导入org。spring框架。util。类实用工具;导入org。spring框架。util。反射工具;导入org。spring框架。util。字符串实用程序;导入org。spring框架。网络。servlet。MVC。方法。requestmappinginfo导入组织。spring框架。网络。servlet。MVC。方法。注释。requestmappingchandlermapping导入Java。io。文件;导入Java。io。文件输入流;导入Java。io。filenotfoundexception导入Java。io。io异常;导入Java。郎。反思。invocationtargetexception导入Java。郎。反思。方法;导入Java。网。malformedurexception导入Java。网。网址;导入Java。网。URL类加载器;@ spring boot应用程序@组件公共类应用引导{静态应用上下文应用上下文;静态字符串file= f : 下载 Apache-maven-3。3 .9-bin repository org example spring boot-test-common 1.0-SNAPSHOT spring boot-test-common-1.0-SNAPSHOT。jar ;公共静态void main(String[]args)抛出异常{应用程序上下文=spring应用程序。运行(应用程序引导程序。class,args);string clazzName= test1。控制器。mytest控制器1 ;字符串clazz name 1= test1。服务。mytestserviceimpl ;注册bean(clazz name 1);注册bean(clazz名称);寄存器控制器(字符串实用程序。un大写(别名。子串(别名。lastindexof( . ) 1)));}公共静态void注册bean(字符串clazz名称)抛出不包括
ception{ URL url = new File(file).toURI().toURL();// URLClassLoader loader = (URLClassLoader) ApplicationBootstrap.class.getClassLoader();// Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });// add.setAccessible(true);// add.invoke(loader, url); URLClassLoader loader = new URLClassLoader(new URL[]{url});// String clazzName = "test1.controller.MyTestController1"; Class<?> clazz = loader.loadClass(clazzName);// applicationContext.getClassLoader().loadClass("test.MyController");// DefaultListableBeanFactory factory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory(); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz); BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition(); //设置当前bean定义对象是单利的 beanDefinition.setScope("singleton"); //将变量首字母置小写 String beanName = StringUtils.uncapitalize(clazzName); beanName = beanName.substring(beanName.lastIndexOf(".")+1); beanName = StringUtils.uncapitalize(beanName); //将applicationContext转换为ConfigurableApplicationContext ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext; // 获取bean工厂并转换为DefaultListableBeanFactory DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory(); defaultListableBeanFactory.registerBeanDefinition(beanName, beanDefinition); } /** * 注册Controller * @param controllerBeanName * @throws Exception */ public static void registerController(String controllerBeanName) throws Exception{ final RequestMappingHandlerMapping requestMappingHandlerMapping= applicationContext.getBean(RequestMappingHandlerMapping.class); if(requestMappingHandlerMapping!=null){ String handler=controllerBeanName; Object controller= applicationContext.getBean(handler); if(controller==null){ return; }// unregisterController(controllerBeanName); //注册Controller Method method=requestMappingHandlerMapping.getClass().getSuperclass().getSuperclass(). getDeclaredMethod("detectHandlerMethods",Object.class); //将private改为可使用 method.setAccessible(true); method.invoke(requestMappingHandlerMapping,handler); } } /** * 去掉Controller的Mapping * @param controllerBeanName */ public static void unregisterController(String controllerBeanName){ final RequestMappingHandlerMapping requestMappingHandlerMapping=(RequestMappingHandlerMapping) applicationContext.getBean("requestMappingHandlerMapping"); if(requestMappingHandlerMapping!=null){ String handler=controllerBeanName; Object controller= applicationContext.getBean(handler); if(controller==null){ return; } final Class<?> targetClass=controller.getClass(); ReflectionUtils.doWithMethods(targetClass, new ReflectionUtils.MethodCallback() { public void doWith(Method method) { Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass); try { Method createMappingMethod = RequestMappingHandlerMapping.class. getDeclaredMethod("getMappingForMethod", Method.class, Class.class); createMappingMethod.setAccessible(true); RequestMappingInfo requestMappingInfo =(RequestMappingInfo) createMappingMethod.invoke(requestMappingHandlerMapping,specificMethod,targetClass); if(requestMappingInfo != null) { requestMappingHandlerMapping.unregisterMapping(requestMappingInfo); } }catch (Exception e){ e.printStackTrace(); } } }, ReflectionUtils.USER_DECLARED_METHODS); } }}
在controller中使用参数
说明
控制器中使用参数有很多种方式,直接上代码,在代码注释中进行说明
package com.mzj.springboot.actionpai.controller; import com.mzj.springboot.actionpai.common.User;import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest;import java.util.List; @RestControllerpublic class HelloWorldController3 { //----------------------方法1、获取路径中的值---------------------------- /** * URL:http://localhost:8081/addUser0/MAZHONGJIA * * @param username * @return */ @GetMapping("/addUser0/{username}") public String addUser0(@PathVariable String username) { System.out.println("username : " + username); return "success0"; } //----------------------方法2、获取路径中的参数---------------------------- /** * URL:http://localhost:8081/addUser1?username=mazhongjia * * @param username * @return */ @GetMapping("/addUser1") public String addUser1(String username) { System.out.println("username : " + username); return "success1"; } //----------------------方法3、通过bean接收HTTP提交的对象---------------------------- /** * URL:http://localhost:8081/addUser2?username=mazhongjia&age=33 * * @param user * @return */ @GetMapping("/addUser2") public String addUser2(User user) { System.out.println("user ... " + user); return "success2"; } //----------------------方法4、通过HttpServletRequest接收参数---------------------------- /** * URL:http://localhost:8081/addUser3?username=mazhongjia * * @param request * @return */ @GetMapping("/addUser3") public String addUser3(HttpServletRequest request) { System.out.println("username ... " + request.getParameter("username")); return "success3"; } //----------------------方法5、用@RequestParam绑定入参---------------------------- /** * 通过@RequestParam注解绑定入参 * <p> * URL:http://localhost:8081/addUser1?username=mazhongjia * * @param username * @return */ @GetMapping("/addUser4") public String addUser4(@RequestParam String username) { System.out.println("username : " + username); return "success4"; } //----------------------方法6、用@RequestBody接收JSON数据---------------------------- /** * URL:http://localhost:8081/addUser5 * BODY: * [ * { * "username": "mazhongjia", * "age": "35" * }, * { * "username": "huan", * "age": "31" * } * ] * <p> * 通过@RequestBody接收JSON入参,同时需要设置http的header中Content-Type属性值为【application/json;charset=UTF-8】 * * @param userList * @return */ @PostMapping("/addUser5") public String addUser5(@RequestBody List<User> userList) { System.out.println("userList : " + userList); return "success5"; } //----------------------方法7、用@ModelAttribute注解获取参数---------------------------- /** * URL:http://localhost:8081/addUser6?username=mazhong&age=35 * <p> * 通过@ModelAttribute注解,从Model、Form或者URL请求参数中获取属性值,上面的URL演示从URL参数中取值 * * @param user * @return */ @PostMapping("/addUser6") public String addUser6(@ModelAttribute("user") User user) { System.out.println("user ... " + user); return "success6"; } /** * URL:http://localhost:8081/addUser7 * <p> * 通过@ModelAttribute注解,从Model、Form或者URL请求参数中获取属性值,上面的URL演示从Model中取值 * * @param user * @return */ @PostMapping("/addUser7") public String addUser7(@ModelAttribute("user") User user) { System.out.println("user7 ... " + user); return "success7"; } @ModelAttribute("user") public User addAccount() { return new User("jz", 55); } }
除了上面的方式外,还有:上传文件MultipartFile、上传图片,这里省略。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。