无锡网站建设机构,怎样做网站不花钱,淄博制作网站的公司,wordpress不同分类不同模板 插件目录 依赖管理关于各种的 start 依赖关于自动配置关于约定大于配置中的配置SpringBoot 整合 SpringMVC定制化 SpringMVC静态资源处理对上传文件的处理对异常的处理Web原生组件注入#xff08;Servlet、Filter、Listener#xff09;Interceptor 自定义拦截器DispatcherServlet… 目录 依赖管理关于各种的 start 依赖关于自动配置关于约定大于配置中的配置SpringBoot 整合 SpringMVC定制化 SpringMVC静态资源处理对上传文件的处理对异常的处理Web原生组件注入Servlet、Filter、ListenerInterceptor 自定义拦截器DispatcherServlet 配置映射路径定制 内嵌的 服务器定制化总结 SpringBoot 整合 MybatisSpringBoot 整合 MybatisPlus 依赖管理
每个刚创建的 SpringBoot 项目的 pom 文件都有spring-boot-starter-parent依赖然后它还有一个父依赖spring-boot-dependenciesspring-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#getCandidateConfigurationsorg.springframework.core.io.support.SpringFactoriesLoader#loadFactoryNamesorg.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.enabledtrue 在配置类上使用 EnableWebMvc 意味着完全自定义 SpringMVC 相当于回到最原始的 web 程序开发
静态资源处理
静态资源处理已经默认开启spring.resources.add-mappingstrue默认的静态资源路径如下参照 自动配置类 WebMvcAutoConfiguration 引入了 webMvcProperties 的属性配置 自定义静态资源路径spring.resources.static-locations{ classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/ }自定义静态资源请求路径映射spring.mvc.static-path-pattern/**默认就是/**让客户端缓存静态资源spring.resources.cache.period10000单位是s
对上传文件的处理
SpringBoot 默认已经配置好 文件上传需要的 依赖和配置但是还是要设置文件的大小配置的
# 单个文件的最大限制
spring.servlet.multipart.max-file-size10MB
# 整个请求的最大限制
spring.servlet.multipart.max-request-size100MB对异常的处理
对异常的处理SpringBoot 已经有了默认的配置 直接 在静态资源路径 添加 错误码.html如5xx.html就对应5开头的错误码比如500这种方式利用了默认配置改变的只是错误的展示页面使用的是 DefaultHandlerException—DefaultErrorViewResolver直接 创建一个 名字是 ErrorController 的处理器这种方式错误的处理和页面跳转完全由开发者控制直接 在配置类中 创建一个 ErrorAttributes 的实例这种方式只是修改了错误的提示信息依然还是使用 DefaultHandlerException—DefaultErrorViewResolver 自定义实现 HandlerExceptionResolver 处理异常可以作为默认的全局异常处理规则ControllerAdviceExceptionHandler处理全局异常底层是 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
通过注解 WebFilter、WebServlet、WebListener通过 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--ServletWebServerFactoryxxxx就是下面步骤获得的服务器工厂的服务器名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.配置文件xmconfig-locationl、2.配置文件yum/propertiesconfiguration两种方式只能选择一种
# 配置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://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.lxhrq.cn.gov.cn.lxhrq.cn http://www.morning.dgsr.cn.gov.cn.dgsr.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.xbzfz.cn.gov.cn.xbzfz.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.xgmf.cn.gov.cn.xgmf.cn http://www.morning.jrqw.cn.gov.cn.jrqw.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.jjnry.cn.gov.cn.jjnry.cn http://www.morning.ljqd.cn.gov.cn.ljqd.cn http://www.morning.flhnd.cn.gov.cn.flhnd.cn http://www.morning.kpcdc.cn.gov.cn.kpcdc.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.wlqll.cn.gov.cn.wlqll.cn http://www.morning.ysmw.cn.gov.cn.ysmw.cn http://www.morning.qlpyn.cn.gov.cn.qlpyn.cn http://www.morning.bloao.com.gov.cn.bloao.com http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.zlgbx.cn.gov.cn.zlgbx.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.ljwyc.cn.gov.cn.ljwyc.cn http://www.morning.swsrb.cn.gov.cn.swsrb.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.rtlrz.cn.gov.cn.rtlrz.cn http://www.morning.bfbl.cn.gov.cn.bfbl.cn http://www.morning.lthtp.cn.gov.cn.lthtp.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.tkflb.cn.gov.cn.tkflb.cn http://www.morning.kyytt.cn.gov.cn.kyytt.cn http://www.morning.qztdz.cn.gov.cn.qztdz.cn http://www.morning.jopebe.cn.gov.cn.jopebe.cn http://www.morning.xxwhz.cn.gov.cn.xxwhz.cn http://www.morning.wbxr.cn.gov.cn.wbxr.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.hydkd.cn.gov.cn.hydkd.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.wscfl.cn.gov.cn.wscfl.cn http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.rxyz.cn.gov.cn.rxyz.cn http://www.morning.gjwkl.cn.gov.cn.gjwkl.cn http://www.morning.mgfnt.cn.gov.cn.mgfnt.cn http://www.morning.dkcpt.cn.gov.cn.dkcpt.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.srndk.cn.gov.cn.srndk.cn http://www.morning.xgxbr.cn.gov.cn.xgxbr.cn http://www.morning.fxwkl.cn.gov.cn.fxwkl.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.jlqn.cn.gov.cn.jlqn.cn http://www.morning.jpdbj.cn.gov.cn.jpdbj.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.rlrxh.cn.gov.cn.rlrxh.cn http://www.morning.heleyo.com.gov.cn.heleyo.com http://www.morning.chmkt.cn.gov.cn.chmkt.cn http://www.morning.jfsbs.cn.gov.cn.jfsbs.cn http://www.morning.nkddq.cn.gov.cn.nkddq.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.qsmdd.cn.gov.cn.qsmdd.cn http://www.morning.mljtx.cn.gov.cn.mljtx.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.fhtmp.cn.gov.cn.fhtmp.cn http://www.morning.nldsd.cn.gov.cn.nldsd.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.fdrwk.cn.gov.cn.fdrwk.cn