<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
package test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResourceLoader; import org.springframework.stereotype.Component; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; @SpringBootApplication @Component public class ApplicationBootstrap { static ApplicationContext applicationContext; static String file = "F:\download\apache-maven-3.3.9-bin\repository\org\example\springboot-test-common\1.0-SNAPSHOT\" + "springboot-test-common-1.0-SNAPSHOT.jar"; public static void main(String[] args) throws Exception { applicationContext = SpringApplication.run(ApplicationBootstrap.class, args); String clazzName = "test1.controller.MyTestController1"; String clazzName1 = "test1.service.MyTestServiceImpl"; registerBean(clazzName1); registerBean(clazzName); registerController(StringUtils.uncapitalize(clazzName.substring(clazzName.lastIndexOf(".") + 1))); } public static void registerBean(String clazzName) throws Exception{ 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); } } }
控制器中使用引數有很多種方式,直接上程式碼,在程式碼註釋中進行說明
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; @RestController public 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、上傳圖片,這裡省略。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45