范文健康探索娱乐情感热点
投稿投诉
热点动态
科技财经
情感日志
励志美文
娱乐时尚
游戏搞笑
探索旅游
历史星座
健康养生
美丽育儿
范文作文
教案论文
国学影视

SpringBoot学习

  Spring Boot是为了简化Spring应用的创建、运行、调试、部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置。
  简单来说,它提供了一堆依赖打包,并已经按照使用习惯解决了依赖问题---习惯大于约定。
  Spring Boot默认使用tomcat作为服务器,使用logback提供日志记录。
  无需多言,直接进入节奏:
  前提
  Spring Boot提供了一系列的依赖包,所以需要构建工具的支持:maven 或 gradle。个人仅熟悉maven,所以下面的内容都是maven相关的。
  如果不熟悉maven,请先了解一下。
  使用
  ① 新建一个maven项目。
  ② pom中parent设为  spring-boot-starter-parent  。建议使用最新的  RELEASE  版本。否则可能需要设置    和   。
  ③ 添加应用需要的starter模块,作为示例,我们仅添加web starter模块。
  这里需要解释下starter模块,简单的说,就是一系列的依赖包组合。例如web starter模块,就是包含了Spring Boot预定义的一些Web开发的常用依赖:   spring-web, spring-webmvc Spring WebMvc框架   tomcat-embed-* 内嵌Tomcat容器   jackson 处理json数据   spring-* Spring框架   spring-boot-autoconfigure Spring Boot提供的自动配置功能
  换句话说,当你添加了相应的starter模块,就相当于添加了相应的所有必须的依赖包。
  starter模块的列表及含义,见 Spring Boot的启动器Starter详解 。
  换句话说,当你添加了相应的starter模块,就相当于添加了相应的所有必须的依赖包。
  starter模块的列表及含义
  至此,pom内容如下:      4.0.0     cn.larry.spring     larry-spring-demo4     0.0.1-SNAPSHOT               org.springframework.boot         spring-boot-starter-parent         1.4.0.RELEASE                                 org.springframework.boot             spring-boot-starter-web               
  保存pom,刷新maven,以便刷新依赖导入。
  基本上,如果没有特别的需要,现在就可以直接写Controller了!!!--特别的需要 是指设置容器、访问端口、路径等。后面再解释。
  ④ 写一个简单的Controller。--直接拿了 package cn.larry.spring.controller;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;  @Controller @EnableAutoConfiguration public class SampleController {      @RequestMapping("/")     @ResponseBody     String home() {         return "Hello World!";     }      public static void main(String[] args) throws Exception {         SpringApplication.run(SampleController.class, args);     } }
  这里有两个新东西: @EnableAutoConfiguration  和  SpringApplication  。
  @EnableAutoConfiguration  用于自动配置。简单的说,它会根据你的pom配置(实际上应该是根据具体的依赖)来判断这是一个什么应用,并创建相应的环境。
  在上面这个例子中, @EnableAutoConfiguration  会判断出这是一个web应用,所以会创建相应的web环境。
  SpringApplication  则是用于从main方法启动Spring应用的类。默认,它会执行以下步骤: 创建一个合适的ApplicationContext实例 (取决于classpath)。 注册一个CommandLinePropertySource,以便将命令行参数作为Spring properties。 刷新application context,加载所有单例beans。 激活所有CommandLineRunner beans。
  默认,直接使用 SpringApplication  的静态方法run()即可。但也可以创建实例,并自行配置需要的设置。
  具体的描述见javadoc即可,如下: Open Declaration org.springframework.boot.SpringApplication   Classes that can be used to bootstrap and launch a Spring application from a Java main method. By default class will perform the following steps to bootstrap your application:   Create an appropriate ApplicationContext instance (depending on your classpath)  Register a CommandLinePropertySource to expose command line arguments as Spring properties  Refresh the application context, loading all singleton beans  Trigger any CommandLineRunner beans  In most circumstances the static run(Object, String []) method can be called directly from your main method to bootstrap your application:   @Configuration  @EnableAutoConfiguration  public class MyApplication  {   // ... Bean definitions   public static void main(String[] args) throws Exception {    SpringApplication.run(MyApplication.class, args);  }   For more advanced configuration a SpringApplication instance can be created and customized before being run:    public static void main(String[] args) throws Exception {    SpringApplication app = new SpringApplication(MyApplication.class);    // ... customize app settings here    app.run(args)  }   SpringApplications can read beans from a variety of different sources. It is generally recommended that a single @Configuration class is used to bootstrap your application, however, any of the following sources can also be used:  Class - A Java class to be loaded by AnnotatedBeanDefinitionReader  Resource - An XML resource to be loaded by XmlBeanDefinitionReader, or a groovy script to be loaded by GroovyBeanDefinitionReader  Package - A Java package to be scanned by ClassPathBeanDefinitionScanner  CharSequence - A class name, resource handle or package name to loaded as appropriate. If the CharSequence cannot be resolved to class and does not resolve to a Resource that exists it will be considered a Package.
  ⑤ 现在,直接右键启动main方法即可。启动信息(包括关闭信息)如下: .   ____          _            __ _ _  / / ___"_ __ _ _(_)_ __  __ _     ( ( )___ | "_ | "_| | "_ / _` |      /  ___)| |_)| | | | | || (_| |  ) ) ) )   "  |____| .__|_| |_|_| |___, | / / / /  =========|_|==============|___/=/_/_/_/  :: Spring Boot ::        (v1.4.0.RELEASE)  2016-08-15 14:30:16.565  INFO 10652 --- [           main] c.l.spring.controller.SampleController   : Starting SampleController on Larry with PID 10652 (D:WorkspaceWorkspace_stslarry-spring-demo4	argetclasses started by Administrator in D:WorkspaceWorkspace_stslarry-spring-demo4) 2016-08-15 14:30:16.567  INFO 10652 --- [           main] c.l.spring.controller.SampleController   : No active profile set, falling back to default profiles: default 2016-08-15 14:30:16.596  INFO 10652 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4a94ee4: startup date [Mon Aug 15 14:30:16 CST 2016]; root of context hierarchy 2016-08-15 14:30:17.676  INFO 10652 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2016-08-15 14:30:17.687  INFO 10652 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat 2016-08-15 14:30:17.688  INFO 10652 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.4 2016-08-15 14:30:17.767  INFO 10652 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext 2016-08-15 14:30:17.767  INFO 10652 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1173 ms 2016-08-15 14:30:17.928  INFO 10652 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: "dispatcherServlet" to [/] 2016-08-15 14:30:17.932  INFO 10652 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: "characterEncodingFilter" to: [/*] 2016-08-15 14:30:17.933  INFO 10652 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: "hiddenHttpMethodFilter" to: [/*] 2016-08-15 14:30:17.933  INFO 10652 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: "httpPutFormContentFilter" to: [/*] 2016-08-15 14:30:17.933  INFO 10652 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: "requestContextFilter" to: [/*] 2016-08-15 14:30:18.177  INFO 10652 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4a94ee4: startup date [Mon Aug 15 14:30:16 CST 2016]; root of context hierarchy 2016-08-15 14:30:18.230  INFO 10652 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String cn.larry.spring.controller.SampleController.home() 2016-08-15 14:30:18.234  INFO 10652 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2016-08-15 14:30:18.235  INFO 10652 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2016-08-15 14:30:18.262  INFO 10652 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-08-15 14:30:18.262  INFO 10652 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-08-15 14:30:18.295  INFO 10652 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-08-15 14:30:18.423  INFO 10652 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup 2016-08-15 14:30:18.480  INFO 10652 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2016-08-15 14:30:18.485  INFO 10652 --- [           main] c.l.spring.controller.SampleController   : Started SampleController in 2.209 seconds (JVM running for 2.642) 2016-08-15 14:30:23.564  INFO 10652 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet "dispatcherServlet" 2016-08-15 14:30:23.564  INFO 10652 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet "dispatcherServlet": initialization started 2016-08-15 14:30:23.574  INFO 10652 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet "dispatcherServlet": initialization completed in 10 ms 2016-08-15 14:30:32.002  INFO 10652 --- [2)-192.168.56.1] inMXBeanRegistrar$SpringApplicationAdmin : Application shutdown requested. 2016-08-15 14:30:32.003  INFO 10652 --- [2)-192.168.56.1] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4a94ee4: startup date [Mon Aug 15 14:30:16 CST 2016]; root of context hierarchy 2016-08-15 14:30:32.004  INFO 10652 --- [2)-192.168.56.1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
  如果使用sts (Spring Tools Suite--没意外的话,后面的博客我会介绍一下),还可以用Spring Application的形式启动,信息不变,但是彩色的,如下:
  ⑥ 根据这个信息,我们可以看出很多东西,不过现在先访问一下吧。
  默认访问地址:  http://localhost:8080/
  按照之前的web项目习惯,你可能会问,怎么没有项目路径?
  这就是Spring Boot的默认设置了,将项目路径直接设为根路径。
  当然,我们也可以设置自己的项目路径 -- 在classpath下的 application.properties 或者 application.yaml 文件中设置即可。
  内容如下: # application.yaml # Server settings (ServerProperties) server:   port: 8080   address: 127.0.0.1   sessionTimeout: 30   contextPath: /aaa    # Tomcat specifics   tomcat:     accessLogEnabled: false     protocolHeader: x-forwarded-proto     remoteIpHeader: x-forwarded-for     basedir:     backgroundProcessorDelay: 30 # secs# application.properties # Server settings (ServerProperties) server.port=8080 server.address=127.0.0.1 #server.sessionTimeout=30 server.contextPath=/aaa  # Tomcat specifics #server.tomcat.accessLogEnabled=false server.tomcat.protocolHeader=x-forwarded-proto server.tomcat.remoteIpHeader=x-forwarded-for server.tomcat.basedir= server.tomcat.backgroundProcessorDelay=30
  上面,  server.contextPath=/aaa  就是设置了项目路径。所以现在需要访问 http://localhost:8080/aaa/ 才行。
  分析
  OK,当目前为止,已经成功运行并访问了一个 SpringMVC 应用。简单的不能再简单了!
  再来看一下启动时的信息: 第 9 行,启动SampleController。 第10行,查找active profile,无,设为default。 第11行,刷新上下文。 第12行,初始化tomcat,设置端口8080,设置访问方式为http。 第13行,启动tomcat服务。 第14行,启动Servlet引擎。 第15行,Spring内嵌的WebApplicationContext 初始化开始。 第16行,Spring内嵌的WebApplicationContext 初始化完成。 第17行,映射servlet,将 dispatcherServlet 映射到 [/] 。  第18行,映射filter,将 characterEncodingFilter 映射到 [/*] 。  第19行,映射filter,将 hiddenHttpMethodFilter 映射到 [/*] 。  第20行,映射filter,将 httpPutFormContentFilter 映射到 [/*] 。  第21行,映射filter,将 requestContextFilter 映射到 [/*] 。  第22行,查找 @ControllerAdvice。  第23行,映射路径 "{[/]}" 到 cn.larry.spring.controller.SampleController.home()。  第24行,映射路径 "{[/error]}" 到 org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)。  第25行,映射路径 "{[/error],produces=[text/html]}" 到 org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)。  第26行,略。 第27行,略。 第28行,略。 第29行,略。  第30行,tomcat启动完毕。  第31行,SampleController启动耗费的时间。  第32行,初始化 dispatcherServlet 。  第33行,dispatcherServlet 的初始化已启动。  第34行,dispatcherServlet 的初始化已完成。  第35行,收到shutdown关闭请求。  第36行,关闭AnnotationConfigEmbeddedWebApplicationContext。  第37行,略。
  从上面的启动信息中可以明显看到SpringMVC的加载过程,特别需要注意的是这种默认方式下加载的几个 filter 。
  所有的面试题目都不是一成不变的,特别是像一线大厂,上面的面试题只是给大家一个借鉴作用,最主要的是给自己增加知识的储备,有备无患。最后给大家分享Spring系列的学习笔记和面试题,包含spring面试题、spring cloud面试题、spring boot面试题、spring教程笔记、spring boot教程笔记、最新阿里巴巴开发手册(63页PDF总结)、2022年Java面试手册。一共整理了1184页PDF文档。私信博主(777)领取,祝大家更上一层楼!!!

5G赋能扬帆未来5G量子筑牢智慧监狱安全屏障日前,工信部主办的第四届绽放杯5G应用征集大赛总决赛在深圳落下帷幕。由中国电信安徽公司安徽省监狱管理局联合打造的量子监狱5G应急指挥调度解决方案,从12281个参赛项目中脱颖而出,一文带你了解NFT的发展之路NFT是区块链技术上的代币方式,意味着了数据藏品工艺品纪念物,乃至是本人数据信息,他们是独一无二的,不可替代的,并且总数比较有限。因为区块链的清晰度追朔性和区块链技术的构架,NFT宁夏拟5年内全部市政车辆改用新能源来源中国新闻网中新社银川1月5日电(记者杨迪)记者5日从宁夏生态环境厅获悉,近日,该部门印发宁夏回族自治区应对气候变化十四五规划(以下简称规划),规划中提到,到2025年,宁夏将实我国首个互联网货运安全运营标准发布近日,中国交通运输协会发布了互联网货运平台安全运营规范(下称规范)团体标准,这是我国首个互联网货运安全团体标准,将于2022年3月1日起正式实施。近年来,随着互联网货运新业态的迅速招联金融渤海证券等App被通报!涉嫌超范围采集个人隐私1月6日,南都记者获悉,近日,国家计算机病毒应急处理中心通过互联网监测发现20款移动应用存在隐私不合规行为,违反网络安全法个人信息保护法相关规定,涉嫌超范围采集个人隐私信息,包括招2022年旗舰机别乱买,目前这三款最为合适,价格良心配置强悍如今智能手机可以说人人都有,甚至不只是一两台而已,大家平常应该会非常关注,厂商们为了迎合用户的需求,也是纷纷开启机海战术,不管是入门市场还是高端市场,市面上都拥有很多的选择,但来到iQOO9Pro实际上手体验如何,是否值得购买?说句实话,近几年来iQOO这个手机品牌发展还算是非常迅速的。因为iQOO有着独特的外观造型以及出色的游戏性能,深受许多年轻小伙伴的喜爱。就在昨天iQOO给广大消费者带来了iQOO数雪崩时每片雪花都觉得是无辜的,电商正在摧毁商业社会的价格体系有人说所有弱化电商的说法,都是基于一个错误的认知,那就是这个世界除了商业没有其它行业了。基于这个错误认知,就必须得保实体店,否则人们就挣不来钱,就无法消费,恶性循环。实际上,除了商中国工程院院士李培根数据是智能制造的基础,企业需要数据驱动每经记者郭荣村每经实习记者安宇飞每经编辑文多1月5日,钉钉召开主题为数字新生的2022制造业钉峰会。会上,中国工程院院士李培根表示,在企业数字化和智能制造中,数据是基础,是血液。数快递共配2022年末端网点想活下来,资源整合是关键在各方的努力下,价格战虽然得以缓解,但是快递行业通过以价换量扩大市场占有率的境况仍在持续,网点不赚钱显然已成为常态,此外,房租人力车辆等运营成本不断飙涨,快递网点的生存压力进一步加国芯科技科创板上市市值112亿国家集成电路基金是股东雷递网雷建平1月6日报道苏州国芯科技股份有限公司(简称国芯科技,股票代码为688262)今日在科创板上市。国芯科技此次发行6000万股,发行41。98元,募资总额为25。19亿元。
坦途皮卡房车翻新第五十天车主很喜欢坦途皮卡,更喜欢用坦途皮卡做成的房车,收了台两手的,拿来拆了内装重做,最终仅仅利用了原车的车厢,电池,逆变器,行车充电器,因为是48v系统的。空调原车是家用变频,但是对这房车使用1231。电池与工频逆变器断开后需要重新连接时,必须遵守以下流程切断电池空开到OFF把1456行车充电器的输出端,连接(悬挂)到逆变器正负极启动汽车引擎引擎运行三十秒到一分钟后,测量逆变不到400元组装移动固态硬盘,性价比吊打市面所有PSSD最近有很多小伙伴给笔者留言,问笔者有哪些移动硬盘速度快,性价比高,想要入手一款。发烧友们先别急,剁手前我们先来好好弄清楚,什么样的移动硬盘性价比才高呢?经过观察,发现目前大多数用户所托瑞安与恒邦财险达成战略合作共拓商用车险市场近日,继与太平洋财险渤海财险宣布合作之后,商用车智能驾驶领军企业所托瑞安再与恒邦财险签订战略合作协议,双方将在商用车险风控建设业务模式创新数据价值挖掘数字化运营等领域展开长期深入全这才是户外运动手表该有的模样军拓铁腕5X评测笔者已经接触军拓系列手表三年了,从刚一开始的铁腕3S,到现在手中的铁腕5X,一共经历了4代产品,刚一开始接触铁腕3S的时候,我默默地给这款产品打了一个圈钱的标签,但是每当体验完这个领势LINKSYSMR9000XAC3000M测评智能组网新体验今年要数最火的路由应该是WiFi6路由,但是今年之前最火的Mesh路由就日落西山了吗,答案是否定的。虽然在WiFi6即将普及的时代,Mesh路由还是有它存在的意义的。Mesh路由是刀片电池安全性被夸大,汉EV自燃打了谁的脸?引言王传福一开始高调宣扬和给刀片电池代言,本身就涉嫌过度夸大宣传,毕竟电芯单体的安全,只是整车安全的九牛一毛。比亚迪电动车从此告别自燃起火,本身就是个伪命题。遇到危机,身为大队长的王传福太自负爱吹牛,还是尹同跃太谦卑和懦弱?引言遥想当年,奇瑞也曾是老牌自主一哥,为何在近些年来不断被友军反超且差距越拉越大?我想,一个性格偏保守也有些过于的谨小慎微的掌门人,难辞其咎。面对合资车企,自主品牌究竟修炼到了第几iPhone13手机官壳曝光,iPhone13min700美元起,折合人民币4530元一名员工爆iPhone13手机壳已经出来了,苹果12没什么区别iPhone13系列售价曝光其中,最便宜的iPhone13mini以700美元起售,约合人民币4530元。iPhone刘宇辞任董事长,代康伟能否带领北汽蓝谷走出低谷?引言于立国事件后,代康伟被推向了北汽新能源总经理的高位,刘宇辞任董事长,北汽蓝谷正式进入刘宇代康伟时代。一个精于营销一个擅长技术,留给外界较大想象空间。古人有训,祸从口出。3月底因北京现代加速没落,李南鸿们为何失职不作为?传闻正在加速变为现实。继五月底,造车新势力理想汽车或将接盘,北京现代顺义废弃一工厂的消息不胫而走后,日前源自理想汽车官网的招聘信息,进一步坐实了相关传闻的可信性。理想汽车收购北京现