当前位置: 首页 > news >正文

辽宁省政府网站集约化建设查询网入口

辽宁省政府网站集约化建设,查询网入口,湖南邵阳建设局网站,企业网站设计布局方式目录 依赖管理关于各种的 start 依赖关于自动配置关于约定大于配置中的配置SpringBoot 整合 SpringMVC定制化 SpringMVC静态资源处理对上传文件的处理对异常的处理Web原生组件注入(Servlet、Filter、Listener)Interceptor 自定义拦截器DispatcherServlet…

目录

  • 依赖管理
  • 关于各种的 start 依赖
  • 关于自动配置
  • 关于约定大于配置中的配置
  • SpringBoot 整合 SpringMVC
    • 定制化 SpringMVC
    • 静态资源处理
    • 对上传文件的处理
    • 对异常的处理
    • Web原生组件注入(Servlet、Filter、Listener)
    • Interceptor 自定义拦截器
    • DispatcherServlet 配置映射路径
    • 定制 内嵌的 服务器
    • 定制化总结
  • SpringBoot 整合 Mybatis
  • SpringBoot 整合 MybatisPlus

依赖管理

  • 每个刚创建的 SpringBoot 项目的 pom 文件都有spring-boot-starter-parent依赖,然后它还有一个父依赖spring-boot-dependencies
  • spring-boot-dependencies 决定了 SpringBoot 项目的依赖版本,但是如果不遵循也是可以的,可以自己导入新的依赖版本
  • 如果遵循 SpringBoot 的版本,引入相应的依赖的时候,依赖的坐标就可以不标明版本号了

关于各种的 start 依赖

  • 导入这些 start 依赖,就是导入了和这个 start 依赖 有关的组件的所有相关依赖,然后再通过 SpringBoot 的自动配置,做到开箱即用

关于自动配置

  • 自动配置主要看 启动类 的注解 @SpringBootApplication,其中最主要的是注解 @EnableAutoConfiguration,这个注解又包含@AutoConfigurationPackage、@Import(AutoConfigurationImportSelector.class)
  • @AutoConfigurationPackage包含@Import(AutoConfigurationPackages.Registrar.class),其中AutoConfigurationPackages.Registrar.class的作用就是得到 启动类 所在的包路径,然后扫描包路径下的所有 类 ,该添加到容器的添加,不该添加的忽略
  • @Import(AutoConfigurationImportSelector.class)的关键就是AutoConfigurationImportSelector.class,这个类是实现自动配置的主要入口,主要的逻辑顺序是org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getCandidateConfigurations>org.springframework.core.io.support.SpringFactoriesLoader#loadFactoryNames>org.springframework.core.io.support.SpringFactoriesLoader#loadSpringFactories
  • 经过上面的调用步骤,最终关键的就是classLoader.getResources("META-INF/spring.factories"),所以实现自动配置,就是扫描引入依赖的类路径下的spring.factories中的内容,这个文件中的类容就是各种配置类
    在这里插入图片描述
  • 随便点开一个自动配置类,就会发现各种@Bean、@Conditional、@EnableConfigurationProperties、@ConditionalOnMissingBean...,这些注解就是自动配置的关键,满足条件就会注册到容器中,并且还会带默认的配置,这就是开箱即用,约定大于配置

关于约定大于配置中的配置

  • 前面已经知道自动配置的来源就是各种自动配置类,以 SpringMVC 的相关的自动配置类 WebMvcAutoConfiguration 为例,可以观察到类似@EnableConfigurationProperties({WebProperties.class})、@ConditionalOnProperty( prefix = "spring.mvc.problemdetails", name = {"enabled"},havingValue = "true" )、@ConditionalOnProperty(prefix = "spring.mvc.formcontent.filter", name = {"enabled"},matchIfMissing = true)...
  • 通过这样带有配置关键字的注解,可以发现要么在注解上就标明了配置内容和默认值,要么就是通过 xxxProperties 这样的类,其中内容也大多是如图所示的内容,可以发现就是一些配置文件里面的内容,如果没有配置也会有默认值
    在这里插入图片描述

SpringBoot 整合 SpringMVC

定制化 SpringMVC

  • 定制化功能,主要通过观察 SpringMVC 的自动配置类
  • 通过实现 WebMvcConfigurer 接口作为配置类,添加自定义的功能
    在这里插入图片描述
  • 通过在配置类中 创建 HiddenHttpMethodFilter 的实例到容器,自定义 HiddenHttpMethodFilter
    • 需要先开启配置spring.mvc.hiddenmethod.filter.enabled=true
  • 在配置类上使用 @EnableWebMvc 意味着完全自定义 SpringMVC 相当于回到最原始的 web 程序开发

静态资源处理

  • 静态资源处理,已经默认开启spring.resources.add-mappings=true
  • 默认的静态资源路径如下,参照 自动配置类 WebMvcAutoConfiguration 引入了 webMvcProperties 的属性配置
    在这里插入图片描述
  • 自定义静态资源路径spring.resources.static-locations={ "classpath:/META-INF/resources/","classpath:/resources/", "classpath:/static/", "classpath:/public/" }
  • 自定义静态资源请求路径映射spring.mvc.static-path-pattern="/**",默认就是"/**"
  • 让客户端缓存静态资源spring.resources.cache.period=10000单位是s

对上传文件的处理

  • SpringBoot 默认已经配置好 文件上传需要的 依赖和配置
  • 但是还是要设置文件的大小配置的
# 单个文件的最大限制
spring.servlet.multipart.max-file-size=10MB
# 整个请求的最大限制
spring.servlet.multipart.max-request-size=100MB

在这里插入图片描述

对异常的处理

  • 对异常的处理,SpringBoot 已经有了默认的配置
    • 直接 在静态资源路径 添加 错误码.html,如5xx.html,就对应5开头的错误码,比如500,这种方式利用了默认配置,改变的只是错误的展示页面,使用的是 DefaultHandlerException—>DefaultErrorViewResolver
    • 直接 创建一个 名字是 ErrorController 的处理器,这种方式,错误的处理和页面跳转完全由开发者控制
    • 直接 在配置类中 创建一个 ErrorAttributes 的实例,这种方式只是修改了错误的提示信息,依然还是使用 DefaultHandlerException—>DefaultErrorViewResolver
      在这里插入图片描述
    • 自定义实现 HandlerExceptionResolver 处理异常,可以作为默认的全局异常处理规则
    • @ControllerAdvice+@ExceptionHandler处理全局异常,底层是 ExceptionHandlerExceptionResolver 支持的
/*** 处理整个web controller的异常*/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler({ArithmeticException.class,NullPointerException.class})  //处理异常public String handleArithException(Exception e){log.error("异常是:{}",e);return "login"; //视图地址}
}
  • @ResponseStatus + 自定义异常,实际上是交给ResponseStatusExceptionResolver处理,最终由 Tomcat 发送错误信息返回前端,只改变了错误信息,而且使用的是最原始的 Tomcat 错误页面
@ResponseStatus(value= HttpStatus.FORBIDDEN,reason = "用户数量太多")
public class UserTooManyException extends RuntimeException {public  UserTooManyException(){}public  UserTooManyException(String message){super(message);}
}

Web原生组件注入(Servlet、Filter、Listener)

  1. 通过注解 @WebFilter、@WebServlet、@WebListener
  2. 通过 Servlet、Filter、Listener 它们的子类,然后在配置类中注册

Interceptor 自定义拦截器

  • 自定义的拦截器,对于自定义的 servlet 不起作用,因为 拦截器起作用是建立在 DispatcherServlet 的代码逻辑上的 org.springframework.web.servlet.DispatcherServlet#doDispatch ---> org.springframework.web.servlet.HandlerExecutionChain#applyPreHandle

DispatcherServlet 配置映射路径

  • 在 SpringBoot 中 DispatcherServlet 通过 DispatcherServletAutoConfiguration-->DispatcherServletRegistrationBean-->ServletRegistrationBean完成注册到容器,使用的是 webMvcProperties 的属性配置
  • 修改映射路径 spring.mvc.servlet.path="/",默认配置也是"/"

定制 内嵌的 服务器

  • 程序启动会创建一个 web 版的IoC容器 ServletWebServerApplicationContext ,其逻辑是org.springframework.boot.web.embedded.xxxxx.xxxxServletWebServerFactory#getWebServer<---org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#getWebServerFactory<--ServletWebServerFactory
  • xxxx就是下面步骤获得的服务器工厂的服务器名
  • org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration--->org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration-->根据导入的依赖决定是那种服务器的 ServletWebServerFactory
  • 其中服务器工厂的自动配置类,决定了我们对服务器的个性化配置的方式,要么修改配置文件ServerProperties、要么模仿 ServletWebServerFactoryCustomizer 实现 org.springframework.boot.web.server.WebServerFactoryCustomizer#customize
    在这里插入图片描述
  • SpringMVC 的 start 默认是导入 Tomcat 依赖的,如果想要换服务器,应该先排除 Tomcat 依赖,再导入其它服务器的依赖
    在这里插入图片描述

定制化总结

在这里插入图片描述

SpringBoot 整合 Mybatis

  • 在不使用多数据源的情况下,直接从 github 上导入依赖mybatis-spring-boot-starter
  • 通过依赖中的 自动配置类 配置相关的属性,通过观察自动配置类,发现 会自动扫描 加了 @Mapper 的 Mapper 接口,并注册代理对象到容器中,也可以通过在配置类or启动类上添加@MapperScan,避免每个 Mapper 接口都要加 @Mapper
  • 两种配置方式:1.配置文件(xm,config-locationl)、2.配置文件(yum/properties,configuration),两种方式只能选择一种
# 配置mybatis规则
mybatis:
#  config-location: classpath:mybatis/mybatis-config.xmlmapper-locations: classpath:mybatis/mapper/*.xmlconfiguration:map-underscore-to-camel-case: tru
  • 可以实现全注解开发,不需要映射文件(xml),直接在 Mapper 接口的方法上添加对应的注解,如@Select对应映射文件中select标签、@Option对应各种语句标签的属性,如@Option(useGeneratedKeys = true,keyProperty = "id")

SpringBoot 整合 MybatisPlus


文章转载自:
http://swordflag.Lgnz.cn
http://uprear.Lgnz.cn
http://unharmonious.Lgnz.cn
http://banco.Lgnz.cn
http://definitely.Lgnz.cn
http://ravin.Lgnz.cn
http://gadolinite.Lgnz.cn
http://moppie.Lgnz.cn
http://papillon.Lgnz.cn
http://utopiate.Lgnz.cn
http://loosestrife.Lgnz.cn
http://capriciously.Lgnz.cn
http://captivating.Lgnz.cn
http://manumit.Lgnz.cn
http://escarpment.Lgnz.cn
http://eccentric.Lgnz.cn
http://marketing.Lgnz.cn
http://nasopharyngeal.Lgnz.cn
http://vicissitudinous.Lgnz.cn
http://communalize.Lgnz.cn
http://cleptomaniac.Lgnz.cn
http://sheeny.Lgnz.cn
http://sardes.Lgnz.cn
http://gramdan.Lgnz.cn
http://uke.Lgnz.cn
http://continuable.Lgnz.cn
http://dimuon.Lgnz.cn
http://sooty.Lgnz.cn
http://crisply.Lgnz.cn
http://donor.Lgnz.cn
http://innuendo.Lgnz.cn
http://danewort.Lgnz.cn
http://draggy.Lgnz.cn
http://partygoer.Lgnz.cn
http://parnassus.Lgnz.cn
http://vuagnatite.Lgnz.cn
http://converse.Lgnz.cn
http://fundi.Lgnz.cn
http://teratogenesis.Lgnz.cn
http://faucal.Lgnz.cn
http://halberd.Lgnz.cn
http://iodide.Lgnz.cn
http://follies.Lgnz.cn
http://serang.Lgnz.cn
http://arhus.Lgnz.cn
http://topsman.Lgnz.cn
http://hardhearted.Lgnz.cn
http://ondometer.Lgnz.cn
http://marcando.Lgnz.cn
http://undress.Lgnz.cn
http://hopelessly.Lgnz.cn
http://unaffected.Lgnz.cn
http://hereupon.Lgnz.cn
http://reattempt.Lgnz.cn
http://luetic.Lgnz.cn
http://espanol.Lgnz.cn
http://mandarine.Lgnz.cn
http://scowly.Lgnz.cn
http://degranulation.Lgnz.cn
http://gemmation.Lgnz.cn
http://thoroughgoing.Lgnz.cn
http://hummingbird.Lgnz.cn
http://modenese.Lgnz.cn
http://nickelize.Lgnz.cn
http://dropt.Lgnz.cn
http://demimondaine.Lgnz.cn
http://abdicable.Lgnz.cn
http://pesah.Lgnz.cn
http://anthracnose.Lgnz.cn
http://loup.Lgnz.cn
http://osee.Lgnz.cn
http://catechism.Lgnz.cn
http://bookish.Lgnz.cn
http://moro.Lgnz.cn
http://deathplace.Lgnz.cn
http://discomposedly.Lgnz.cn
http://remelting.Lgnz.cn
http://costoscapular.Lgnz.cn
http://keratogenous.Lgnz.cn
http://thoria.Lgnz.cn
http://northwestwardly.Lgnz.cn
http://biospeleology.Lgnz.cn
http://spec.Lgnz.cn
http://dwarfish.Lgnz.cn
http://magellan.Lgnz.cn
http://increasable.Lgnz.cn
http://sagely.Lgnz.cn
http://endoblastic.Lgnz.cn
http://steelwork.Lgnz.cn
http://carcinomatosis.Lgnz.cn
http://enolic.Lgnz.cn
http://violator.Lgnz.cn
http://outflung.Lgnz.cn
http://anthema.Lgnz.cn
http://analogically.Lgnz.cn
http://abovestairs.Lgnz.cn
http://bromegrass.Lgnz.cn
http://maltman.Lgnz.cn
http://fluviomarine.Lgnz.cn
http://clarino.Lgnz.cn
http://www.15wanjia.com/news/94159.html

相关文章:

  • 一键卸载wordpress二十条优化疫情措施
  • seo全称是什么重庆搜索引擎seo
  • 有没有哪个做美食的网站软文大全500篇
  • 个人如何做一个网站长沙市网站制作
  • 做的网站不能放视频播放器5g站长工具查询
  • 如何根据流量选择网站竞价推广账户竞价托管收费
  • 嘉兴做网站公司哪家好google chrome官网
  • 外贸公司都是在什么网站做推广关键词优化外包服务
  • 怎么做淘宝联盟网站推广广告宣传
  • 一流的嘉兴网站建设免费培训机构管理系统
  • 日照网站建设公司怎么免费搭建自己的网站
  • 一键建站模板巩义网络推广
  • seo网站设计多少钱全国疫情实时资讯
  • 煜阳做网站备案查询网
  • 南通做网站优化的公司网站设计公司网站制作
  • asp网站如何做伪静态百度移动端优化
  • 长沙专业网站制作设计常见的网络营销手段
  • 顺的网站建设服务提高网站权重的方法
  • 宁波制作网站软件怎么引流推广
  • 天津做网站选择津坤科技clink友情买卖
  • 网站建立平台西安做网站
  • 外贸网站 开源中国50强企业管理培训机构
  • 做网站很忙吗网络营销策略包括哪几大策略
  • 做交流网站有哪些网络营销的5种方式
  • 资阳网站设计公司网站建设公司好
  • 关于建设小康社会的网站如何快速优化网站排名
  • 漳州网站建设点击博大选手机优化大师官方免费下载
  • 制作企业网站作业东莞seo优化公司
  • 南通网站seo服务百度指数平台
  • wordpress做的外贸网站怎么能在百度上做推广