這是Day2,嘗試用中文的教材來理解
尚硅谷IT培训学校: https://www.youtube.com/watch?v=TAgd4Xbqudc&list=PLmOn9nNkQxJEeIH75s5pdTUnCo9-xOc7c
前置知識: Spring、SpringMVC、MyBatis
Spring和Spring MVC就是官網的Spring Framework那頁: https://spring.io/projects/spring-framework
導包、寫配置、啟動運行
Servlet容器: Tomcat、 Jetty、Undertow
不用裝Linux
默認配置
外部化配置
RestController: https://github.com/fenturechance/spring-boot-practice-240128/commit/5b231daf2b3a4d119dca33248440b45da789f23f
pom.xml的build的spring-boot-maven-plugin
是為了要讓打包簡單化用的
筆記: IntelliJ的JDK位置: C:\Users\user\.jdks\openjdk-21.0.2\bin
把這串放到Windows的系統環境變數的Path即可
外部化配置application.properties
項目parent: spring-boot-starter-parent
場景: spring-boot-starter-web官方spring-boot-starter-*
第三方 *-spring-boot-starter
application.properties: 集中式管理
new module可以加一個新的application
resources裡面的static(靜態資源)、template(頁面模板)
依賴管理
maven依賴傳遞: https://youtu.be/eX-EiudPK9c?si=q7fz06Lq3HtQHKzc
maven就近原則
按右邊的M符號,可以刷新Maven
自動配置機制: https://youtu.be/ut_1gnVdcjQ?si=bbWkNBXnvvcRZyBx
IoC容器
SpringApplication.run(DemoApplication.class, args); //把run點進去後,結果如下
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
return run(new Class[]{primarySource}, args);
}
java 10局部變量自動推斷
var ioc = SpringApplication.run(DemoApplication.class, args);
String[] names = ioc.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
demoApplication
org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
appConfig
helloController
testClass1
org.springframework.boot.autoconfigure.AutoConfigurationPackages
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
propertySourcesPlaceholderConfigurer
org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration
fileWatcher
sslPropertiesSslBundleRegistrar
sslBundleRegistry
org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor
org.springframework.boot.context.internalConfigurationPropertiesBinder
org.springframework.boot.context.properties.BoundConfigurationProperties
org.springframework.boot.context.properties.EnableConfigurationPropertiesRegistrar.methodValidationExcludeFilter
spring.ssl-org.springframework.boot.autoconfigure.ssl.SslProperties
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
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.TaskExecutorConfigurations$ThreadPoolTaskExecutorBuilderConfiguration
threadPoolTaskExecutorBuilder
org.springframework.boot.autoconfigure.task.TaskExecutorConfigurations$TaskExecutorBuilderConfiguration
taskExecutorBuilder
org.springframework.boot.autoconfigure.task.TaskExecutorConfigurations$SimpleAsyncTaskExecutorBuilderConfiguration
simpleAsyncTaskExecutorBuilder
org.springframework.boot.autoconfigure.task.TaskExecutorConfigurations$TaskExecutorConfiguration
applicationTaskExecutor
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration
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.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
welcomePageHandlerMapping
welcomePageNotAcceptableHandlerMapping
localeResolver
themeResolver
flashMapManager
mvcConversionService
mvcValidator
mvcContentNegotiationManager
requestMappingHandlerMapping
mvcPatternParser
mvcUrlPathHelper
mvcPathMatcher
viewControllerHandlerMapping
beanNameHandlerMapping
routerFunctionMapping
resourceHandlerMapping
mvcResourceUrlProvider
defaultServletHandlerMapping
requestMappingHandlerAdapter
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
spring.jmx-org.springframework.boot.autoconfigure.jmx.JmxProperties
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$JacksonMixinConfiguration
jsonMixinModuleEntries
jsonMixinModule
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.TaskSchedulingConfigurations$ThreadPoolTaskSchedulerBuilderConfiguration
threadPoolTaskSchedulerBuilder
org.springframework.boot.autoconfigure.task.TaskSchedulingConfigurations$TaskSchedulerBuilderConfiguration
taskSchedulerBuilder
org.springframework.boot.autoconfigure.task.TaskSchedulingConfigurations$SimpleAsyncTaskSchedulerBuilderConfiguration
simpleAsyncTaskSchedulerBuilder
org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration
spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties
org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration
httpMessageConvertersRestClientCustomizer
restClientSsl
restClientBuilderConfigurer
restClientBuilder
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
SpingApplication只掃描自己和子層的package,除非加上屬性scanBasePackages
自定義掃描路徑
scanBasePackages: https://github.com/fenturechance/spring-boot-practice-240128/commit/34a4c7ef98205222920be0ba4b1fe352268e6c2f
ComponentScan也可以做到一樣的效果
由此文件可知,默認port是8080
spring-boot-starter
是所有starter的starter
spring-boot-autoconfigure,config各種場景: cache、kafka....
目前是導入Web,所以Web會生效
產生Getter Setter
On the Code menu, click Generate Alt+Insert.
CTRL + D duplicates the selected line
常用Annotation(包含@import): https://youtu.be/WW_R1veZcr8?si=FbXaubidiN5UeXw7
Scope: prototype: https://github.com/fenturechance/spring-boot-practice-240128/commit/c853fe8de91a8b6d83ad7ae21d60c8d91c437d05
多型
常用Annotation2: https://youtu.be/EraYx170VQs?si=cSzqii0fPwLaaqDb
ConditionalOnClass / ConditionalOnMissingClass
如果有(沒有)這個組件,就放入指定組件: https://github.com/fenturechance/spring-boot-practice-240128/commit/abd21d821c400c13709fccad8d53f10eff1b00d6
IntelliJ: Delete line: Cmd + Y
設定文件為UTF-8,就可以在properties裡面寫中文
抓properties裡面的屬性來創建組件: https://youtu.be/5YmwINwKQ0U?si=BiMv1XQhhlN8gDsd
ConfigurationProperties: https://github.com/fenturechance/spring-boot-practice-240128/commit/ced21c8251b05ef87f5d0acbc2b915ecd27f532f
在容器中註冊組件: @Component / @Bean
IntelliJ
Successively press Alt +J to find and select the next occurrence of case-sensitively matching word or text range.
在AppConfig讓組件自動放入容器
EnableConfigurationProperties: https://github.com/fenturechance/spring-boot-practice-240128/commit/48963609df7c214f94fa6660eccecd3f514edcb8
【尚硅谷】Spring注解驱动教程(雷丰阳源码级讲解): https://www.youtube.com/playlist?list=PLjwE8m3kyOlc4UWESNUjzdyMRvOXrf4Q2
導入流程: https://youtu.be/HtX6pt0hCNs?si=p4yJsav68nDPGR4s
核心場景
spring-boot-autoconfigure
@SpringBootConfiguration //聲明我是個配置類
@EnableAutoConfiguration //開啟自動配置的核心
@Import給容器中導入組件
@Import(AutoConfigurationImportSelector.class) 批次給容器導入組件
@ComponentScan //掃描指定包
142個AutoConfigure
C:\Users\user\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\3.2.2\spring-boot-autoconfigure-3.2.2.jar!\META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports
自動配置類
按需生效
看Web場景的自動配置做舉例
C:\Users\user\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\3.2.2\spring-boot-autoconfigure-3.2.2.jar!\org\springframework\boot\autoconfigure\web\servlet\DispatcherServletAutoConfiguration.class
給容器中使用@Bean放一堆組件
每個自動配置類都可能有這個註解: @EnableConfigurationProperties(XXXX.class)
配置文件中的屬性封裝到屬性類中
沒有留言:
張貼留言