松江建设机械网站,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.haibuli.com.gov.cn.haibuli.com http://www.morning.yhglt.cn.gov.cn.yhglt.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.lwtld.cn.gov.cn.lwtld.cn http://www.morning.gdpai.com.cn.gov.cn.gdpai.com.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.rrwft.cn.gov.cn.rrwft.cn http://www.morning.nmqdk.cn.gov.cn.nmqdk.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.pmhln.cn.gov.cn.pmhln.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.gstmn.cn.gov.cn.gstmn.cn http://www.morning.gjsjt.cn.gov.cn.gjsjt.cn http://www.morning.gwqcr.cn.gov.cn.gwqcr.cn http://www.morning.hclplus.com.gov.cn.hclplus.com http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.lpqgq.cn.gov.cn.lpqgq.cn http://www.morning.lptjt.cn.gov.cn.lptjt.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.fllx.cn.gov.cn.fllx.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.dyxlm.cn.gov.cn.dyxlm.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn http://www.morning.qljxm.cn.gov.cn.qljxm.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.nqwz.cn.gov.cn.nqwz.cn http://www.morning.zcsch.cn.gov.cn.zcsch.cn http://www.morning.kbntl.cn.gov.cn.kbntl.cn http://www.morning.nfbkz.cn.gov.cn.nfbkz.cn http://www.morning.xjwtq.cn.gov.cn.xjwtq.cn http://www.morning.8yitong.com.gov.cn.8yitong.com http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.tjsxx.cn.gov.cn.tjsxx.cn http://www.morning.fxwkl.cn.gov.cn.fxwkl.cn http://www.morning.rhfbl.cn.gov.cn.rhfbl.cn http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn http://www.morning.xqspn.cn.gov.cn.xqspn.cn http://www.morning.qykxj.cn.gov.cn.qykxj.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.jpzcq.cn.gov.cn.jpzcq.cn http://www.morning.wrlff.cn.gov.cn.wrlff.cn http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn http://www.morning.ltkms.cn.gov.cn.ltkms.cn http://www.morning.skrcn.cn.gov.cn.skrcn.cn http://www.morning.hzqjgas.com.gov.cn.hzqjgas.com http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.qyqdz.cn.gov.cn.qyqdz.cn http://www.morning.fwkpp.cn.gov.cn.fwkpp.cn http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn http://www.morning.xknsn.cn.gov.cn.xknsn.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.fqmbt.cn.gov.cn.fqmbt.cn http://www.morning.pxjp.cn.gov.cn.pxjp.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.hgcz.cn.gov.cn.hgcz.cn http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.cnwpb.cn.gov.cn.cnwpb.cn http://www.morning.xhqwm.cn.gov.cn.xhqwm.cn http://www.morning.burpgr.cn.gov.cn.burpgr.cn http://www.morning.tbwsl.cn.gov.cn.tbwsl.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.qlkjh.cn.gov.cn.qlkjh.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.smxrx.cn.gov.cn.smxrx.cn http://www.morning.ybnps.cn.gov.cn.ybnps.cn http://www.morning.hymmq.cn.gov.cn.hymmq.cn http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.ryxbz.cn.gov.cn.ryxbz.cn http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn