1、SpringBoot特点
1.1、依赖管理
-
父项目做依赖管理
我们这个项目(参考基于maven,你的第一个springboot,helloworld)在pom文件里引入了一个springboot父项目
org.springframework.boot 2.5.6 spring-boot-starter-parent 然后我们导入了一个叫starter-web的依赖
org.springframework.boot spring-boot-starter-web org.springframework.boot 2.5.6 spring-boot-starter-parent 可能父项目里会声明很多依赖,子项目只要继承了父项目 , 子项目写依赖就不需要版本号了
即使我们在下面导入依赖了,都没写过版本号
**spring-boot-starter-parent.pom 的父项目 : **
org.springframework.boot spring-boot-dependencies 2.5.6 spring-boot-dependencies.pom 看到
里面声明了几乎我们在开发中常用的所有的jar包版本 spring-boot-dependencies.pom里搜索mysql看到:
mysql mysql-connector-java ${mysql.version} com.google.protobuf protobuf-java 在pom.xml里写:
mysql mysql-connector-java 如果说,某一天我们要用8.0.21这个mysql版本,我的mysql数据库就要是mysql8相关的数据库,如果我装的是mysql5的数据库怎么办呢?(若版本对应则忽略)
就要改mysql驱动
maven仓库
在springboot版本仲裁里面mysql的版本是用一个属性mysql.version,如果我们想修改:
来到我们的pom.xml里面
也写个
申明上一些属性,这个属性里面就来修改 版本 5.1.43 我们首先得看底层人家规定这个依赖的时候人家是以什么属性规定的
-
开发导入starter场景启动器
1、见到很多spring-boot-starter-* , *就某种场景 2、只要引入了starter,这个场景的所有常规依赖我们都会自动引入 3、springboot所有支持的场景: https://docs.spring.io/springboot/docs/current/reference/html/using.html#using 4、见到 *-spring-boot-starter:第三方为我们提供的简化开发的场景启动器 工具的分析依赖树(鼠标右键选择Diagrams -> show Dependencies)(听说IDEA社区版是没有这个图解的) 5、所有场景启动器最底层的依赖
org.springframework.boot spring-boot-starter 2.3.4.RELEASE compile 未来我们在springboot里面会见到以spring-boot-starter命名的依赖,包括官方文档Using Spring Boot(1.5 Starters)
starter是一组依赖的集合描述
我们一般引入一个starter,它的整个完整的开发环境就被全部引入了,这个场景所有的依赖全部都进来了。而且官方的命名方式是spring-boot-starter-*
如果不满意也可以自定义starter(但是这是高阶知识,现在不必要),springboot推荐我们自己创建的starter千万不要以spring-boot命名开始
-
无需关注版本号,自动版本仲裁
1、以后引入依赖默认都可以不写版本 2、但是引入的非仲裁(没有声明)的jar,要写版本号
-
可以修改版本号
1、查看spring-boot-dependencies里面规定当前依赖的版本。用的key 2、在当前项目里重写配置
5.1.43
1.2、自动配置
-
自动配置好Tomcat
不管自动配好了什么东西,肯定需要俩步:
-
引入tomcat依赖(默认配置在spring-boot-starter-web下)
org.springframework.boot spring-boot-starter-tomcat 2.5.6 compile 我们tomcat的jar包才能进来,进来后做的第二步事情:
-
配置tomcat , 后面自动配置要研究的东西
-
-
自动配置好SpringMVC
-
引入了springMVC全套组件
-
自动配好了SpringMVC常用组件(功能)
spring springMVC整合
以前要在web.xml写一大堆配置,比如在web.xml我们最先要配置的是springMVC的整个DispatcherServlet它帮我们来拦截所有请求:
springDisoatcherServlet org.springframework.web.servlet.DispatcherServlet 相当于springboot里的springMVC要工作DispatcherServlet一定要有;来看一下整个应用里面有没有帮我们配DispatcherServlet:
首先SpringApplication.run(MainApplication.class,args);会返回一个ConfigurableApplicationContext(这是返回我们的IOC容器)这个容器里面就包含了我们当前应用所有的组件。
//查看容器里面的组件 String[] names = run.getBeanDefinitionNames();//getBeanDefinitionNames()获取所有我们组件定义的这个名字 for (String name : names) {//根据名字来判断有哪些组件 System.out.println(name); }org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory mainApplication(主程序) org.springframework.boot.autoconfigure.internalCachingmetadataReaderFactory helloController(自己写的业务逻辑组件) org.springframework.boot.autoconfigure.AutoConfigurationPackages org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration propertySourcesPlaceholderConfigurer org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration$TomcatWebSocketConfiguration websocketServletWebServerCustomizer org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat tomcatServletWebServerFactory org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration servletWebServerFactoryCustomizer tomcatServletWebServerFactoryCustomizer org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor org.springframework.boot.context.internalConfigurationPropertiesBinderFactory org.springframework.boot.context.internalConfigurationPropertiesBinder org.springframework.boot.context.properties.BoundConfigurationProperties org.springframework.boot.context.properties.EnableConfigurationPropertiesRegistrar.methodValidationExcludeFilter server-org.springframework.boot.autoconfigure.web.ServerProperties webServerFactoryCustomizerBeanPostProcessor errorPageRegistrarBeanPostProcessor org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletConfiguration dispatcherServlet(有小写,就肯定有相关的组件) spring.mvc-org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration dispatcherServletRegistration org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration taskExecutorBuilder applicationTaskExecutor spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration error beanNameViewResolver org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration conventionErrorViewResolver spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties spring.web-org.springframework.boot.autoconfigure.web.WebProperties org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration errorAttributes basicErrorController errorPageCustomizer preserveErrorControllerTargetClassPostProcessor org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration requestMappingHandlerAdapter requestMappingHandlerMapping welcomePageHandlerMapping localeResolver themeResolver flashMapManager mvcConversionService mvcValidator mvcContentNegotiationManager mvcPatternParser mvcUrlPathHelper mvcPathMatcher viewControllerHandlerMapping beanNameHandlerMapping routerFunctionMapping resourceHandlerMapping mvcResourceUrlProvider defaultServletHandlerMapping handlerFunctionAdapter mvcUriComponentsContributor httpRequestHandlerAdapter simpleControllerHandlerAdapter handlerExceptionResolver mvcViewResolver mvcHandlerMappingIntrospector viewNameTranslator org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter defaultViewResolver viewResolver requestContextFilter org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration formContentFilter org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration mbeanExporter objectNamingStrategy mbeanServer org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration springApplicationAdminRegistrar org.springframework.boot.autoconfigure.aop.AopAutoConfiguration$ClassProxyingConfiguration forceAutoProxyCreatorToUseClassProxying org.springframework.boot.autoconfigure.aop.AopAutoConfiguration org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration applicationAvailability org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration standardJacksonObjectMapperBuilderCustomizer spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration jacksonObjectMapperBuilder org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$ParameterNamesModuleConfiguration parameterNamesModule org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration jacksonObjectMapper org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration jsonComponentModule org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration lifecycleProcessor spring.lifecycle-org.springframework.boot.autoconfigure.context.LifecycleProperties org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration stringHttpMessageConverter org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration mappingJackson2HttpMessageConverter org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration messageConverters org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration spring.sql.init-org.springframework.boot.autoconfigure.sql.init.SqlInitializationProperties org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer$DependsOnDatabaseInitializationPostProcessor org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration scheduledBeanLazyInitializationExcludeFilter taskSchedulerBuilder spring.task.scheduling- org.springframework.boot.autoconfigure.task.TaskSchedulingProperties org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration restTemplateBuilderConfigurer restTemplateBuilder org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration$TomcatWebServerFactoryCustomizerConfiguration tomcatWebServerFactoryCustomizer org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration characterEncodingFilter(能保证字符串返回中文的时候不乱码) localeCharsetMappingsCustomizer org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration multipartConfigElement multipartResolver(文件上传解析器) spring.servlet.multipart-org.springframework.boot.autoconfigure.web.servlet.MultipartProperties org.springframework.aop.config.internalAutoProxyCreator
spring springMVC以前要解决字符乱码问题要:
characterEncodingFilter /* characterEncodingFilter能保证字符串返回中文的时候不乱码
包括文件上传:springmvc要有视图解析器:
springboot有没有相关的视图解析器(viewResolver)
beanNameViewResolver org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration conventionErrorViewResolver
文件上传,springMVC要配置(文件上传解析器)
springboot也有
multipartResolver
等等
所以看自动配置好Web常见功能,如: 字符编码问题结论
-
-
自动配置好Web常见功能,如: 字符编码问题
-
SpringBoot帮我们配置好了所有web开发的常见场景
因为我们看到了组件在容器中有,在容器中有就代表会生效,至于怎么生效是后来研究的事情
-
-
默认的包结构
什么叫默认包结构?
以前要指定spring springMVC 我们得指定这些Controller在哪个包下,指定包扫描。
在springboot我们没有配任何包扫描只要我们写的Controller他就能发现。原因在哪?
2.1.Using the “default” Package(默认包扫描规则)
默认包扫描规则就是说我们这个主程序比如在com.example.myapplication这个包下,只要是主程序它下面的子包或者子包下面所有的组件它都能被默认扫描到
-
主程序所在的包以及下面所有的子包里的组件都会被默认扫描进来
-
无需配置以前的包扫描
-
*若是在主程序包外面还要发现服务,其实也行(想要改变扫描路径):
在@SpringBootApplication里面有一个scanbasePackages()(扫描的基础包)
@SpringBootApplication(scanbasePackages="包名")
或者还有一个注解:@ComponentScan注解(包扫描)
这个包扫描因为@SpringBootApplication里面已经写了包扫描,这是一个不能重复的注解,所以显示报错。之后再看这怎么写
-
ComponentScan指定扫描路径
@SpringBootApplication 等同于 @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan("com.chentianyu.boot")
-
-
-
各种配置拥有默认值
自己可以在application.properties里改配置,而且这些配置点进去(按住ctrl + 你鼠标你移到上面点击进入)你会发现它们其实是绑定一段java类的
- 默认配置最终都是映射到某一个类上的
- 配置文件的值最终会绑定到某个类上,这个类会在容器中创建对象
-
按需加载所有自动配置项
-
有非常多的starter
-
引入了哪些场景,这个场景的自动配置才会开启
其实springboot对于所有东西的自动配置全部集中于
spring-boot-starter-web -> web依赖于spring-boot-starter -> 这个包里面有一个包叫spring-boot-autoconfigure
-
SpringBoot所有自动配置都在spring-boot-autoconfigure包里
Externall Libraries -> Maven:org.sprngframework.boot:spring-boot-starter-autocofigure:你的版本号 -> spring-boot-autoconfigure-2.5.6.jar -> org -> sprngframework -> boot -> autoconfigure
****Autoconfiguration**就是自动配置,但是这个自动配置不一定全部都能生效,类点开有发红的就是不生效的
-



