springboot提供了一种快速构建应用程序的方法。它会查看您的类路径和已配置的bean,对您缺少的内容做出合理的假设,并添加这些项。使用springboot,您可以更多地关注业务特性,而不是基础设施。
springmvc在类路径上吗?您几乎总是需要几个特定的bean,springboot会自动添加它们。springmvc应用程序还需要一个servlet容器,因此springboot会自动配置嵌入式Tomcat。
这些只是springboot提供的自动配置的几个例子。例如,如果Thymeleaf在您的路径上,springboot会自动向您的应用程序上下文添加SpringTemplateEngine。但是,如果使用自己的设置定义自己的SpringTemplateEngine,springboot不会添加一个。这样你就可以控制局面了,而你却不费吹灰之力。
从Spring initializer开始
对于所有的Spring应用程序,您应该从Spring初始化器开始。initializer提供了一种快速的方法来获取应用程序所需的所有依赖项,并为您做了大量的设置工作。这个例子只需要springweb依赖项。下图显示了为此示例项目设置的初始值设定项:
@SpringBootApplication是一个方便的注释,它添加了以下所有内容:
@配置:将类标记为应用程序上下文的bean定义源。
@EnableAutoConfiguration:告诉springboot根据类路径设置、其他bean和各种属性设置开始添加bean。例如,如果springwebmvc在类路径上,这个注释将应用程序标记为web应用程序并激活关键行为,例如设置一个DispatcherServlet。
@ComponentScan:告诉Spring在com/example包中查找其他组件、配置和服务,让它找到控制器。
main()方法使用springboot的SpringApplication.run()启动应用程序的方法。你注意到没有一行XML?没有web.xml文件文件,或者。这个web应用程序是100%纯Java的,您不需要配置任何管道或基础设施。
还有一个标记为@Bean的CommandLineRunner方法,它在启动时运行。它检索由应用程序创建的或由springboot自动添加的所有bean。它把它们分类并打印出来。
创建应用程序类
Spring初始化器为您创建了一个简单的应用程序类。然而,在这种情况下,这太简单了。您需要修改application类以匹配以下列表(来自src/main/java/com/example/springboot/应用程序.java):
让我们检查一下springboot提供的bean
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.2.RELEASE)
2020-07-30 16:06:58.004 INFO 39956 --- [ main] com.example.springboot.Application : Starting Application on L-20191127 with PID 39956 (E:\eclipse-workspace\springsecuritytest-3\target\classes started by Administrator in E:\eclipse-workspace\springsecuritytest-3)
2020-07-30 16:06:58.007 INFO 39956 --- [ main] com.example.springboot.Application : No active profile set, falling back to default profiles: default
2020-07-30 16:07:01.327 INFO 39956 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-07-30 16:07:01.344 INFO 39956 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-07-30 16:07:01.345 INFO 39956 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-07-30 16:07:01.346 INFO 39956 --- [ main] o.a.catalina.core.AprLifecycleListener : An older version [1.2.14] of the Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.23]
2020-07-30 16:07:01.346 INFO 39956 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded Apache Tomcat Native library [1.2.14] using APR version [1.6.2].
2020-07-30 16:07:01.346 INFO 39956 --- [ main] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2020-07-30 16:07:01.347 INFO 39956 --- [ main] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2020-07-30 16:07:02.440 INFO 39956 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.0.2l 25 May 2017]
2020-07-30 16:07:02.574 INFO 39956 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-07-30 16:07:02.574 INFO 39956 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 4521 ms
2020-07-30 16:07:02.780 INFO 39956 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-30 16:07:02.926 WARN 39956 --- [ main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2020-07-30 16:07:03.045 INFO 39956 --- [ main] .s.s.UserDetailsServiceAutoConfiguration :
Using generated security password: 94220d7f-9568-4653-b175-4952d30bbb06
2020-07-30 16:07:03.143 INFO 39956 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@3b7b05a8, org.springframework.security.web.context.SecurityContextPersistenceFilter@77d18d0b, org.springframework.security.web.header.HeaderWriterFilter@1dbb650b, org.springframework.security.web.csrf.CsrfFilter@6bd16207, org.springframework.security.web.authentication.logout.LogoutFilter@769d513, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@4349754, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@2788d0fe, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@3d36dff4, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@796065aa, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@7b7b3edb, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@5b2f8ab6, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@7abe27bf, org.springframework.security.web.session.SessionManagementFilter@63ec445c, org.springframework.security.web.access.ExceptionTranslationFilter@7e7f0f0a, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@ae372b9]
2020-07-30 16:07:03.213 INFO 39956 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-07-30 16:07:03.221 INFO 39956 --- [ main] com.example.springboot.Application : Started Application in 5.645 seconds (JVM running for 6.193)
Let's inspect the beans provided by Spring Boot:
application
applicationAvailability
applicationTaskExecutor
authenticationEventPublisher
authenticationManagerBuilder
autowiredWebSecurityConfigurersIgnoreParents
basicErrorController
beanNameHandlerMapping
beanNameViewResolver
characterEncodingFilter
commandLineRunner
conventionErrorViewResolver
conversionServicePostProcessor
defaultServletHandlerMapping
defaultTemplateResolver
defaultViewResolver
delegatingApplicationListener
dispatcherServlet
dispatcherServletRegistration
enableGlobalAuthenticationAutowiredConfigurer
error
errorAttributes
errorPageCustomizer
errorPageRegistrarBeanPostProcessor
formContentFilter
handlerExceptionResolver
handlerFunctionAdapter
helloController
httpRequestHandlerAdapter
inMemoryUserDetailsManager
initializeAuthenticationProviderBeanManagerConfigurer
initializeUserDetailsBeanManagerConfigurer
jacksonObjectMapper
jacksonObjectMapperBuilder
java8TimeDialect
jsonComponentModule
lifecycleProcessor
localeCharsetMappingsCustomizer
mappingJackson2HttpMessageConverter
messageConverters
multipartConfigElement
multipartResolver
mvcContentNegotiationManager
mvcConversionService
mvcHandlerMappingIntrospector
mvcPathMatcher
mvcResourceUrlProvider
mvcUriComponentsContributor
mvcUrlPathHelper
mvcValidator
mvcViewResolver
objectPostProcessor
org.springframework.aop.config.internalAutoProxyCreator
org.springframework.boot.autoconfigure.AutoConfigurationPackages
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration$ClassProxyingConfiguration
org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration
org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration
org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration
org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperBuilderConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$JacksonObjectMapperConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration$ParameterNamesModuleConfiguration
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration
org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration
org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration$DefaultConfigurerAdapter
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration
org.springframework.boot.autoconfigure.security.servlet.WebSecurityEnablerConfiguration
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration
org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$DefaultTemplateResolverConfiguration
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$ThymeleafDefaultConfiguration
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$ThymeleafJava8TimeDialect
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$ThymeleafWebMvcConfiguration
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$ThymeleafWebMvcConfiguration$ThymeleafViewResolverConfiguration
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration$TomcatWebServerFactoryCustomizerConfiguration
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletConfiguration
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration$TomcatWebSocketConfiguration
org.springframework.boot.context.internalConfigurationPropertiesBinder
org.springframework.boot.context.internalConfigurationPropertiesBinderFactory
org.springframework.boot.context.properties.BoundConfigurationProperties
org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.event.internalEventListenerFactory
org.springframework.context.event.internalEventListenerProcessor
org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration
org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration
org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration
org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration
parameterNamesModule
preserveErrorControllerTargetClassPostProcessor
privilegeEvaluator
propertySourcesPlaceholderConfigurer
requestContextFilter
requestDataValueProcessor
requestMappingHandlerAdapter
requestMappingHandlerMapping
resourceHandlerMapping
restTemplateBuilder
routerFunctionMapping
securityFilterChainRegistration
server-org.springframework.boot.autoconfigure.web.ServerProperties
servletWebServerFactoryCustomizer
simpleControllerHandlerAdapter
spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties
spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties
spring.lifecycle-org.springframework.boot.autoconfigure.context.LifecycleProperties
spring.mvc-org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties
spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties
spring.security-org.springframework.boot.autoconfigure.security.SecurityProperties
spring.servlet.multipart-org.springframework.boot.autoconfigure.web.servlet.MultipartProperties
spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties
spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties
spring.thymeleaf-org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
springSecurityFilterChain
standardJacksonObjectMapperBuilderCustomizer
stringHttpMessageConverter
taskExecutorBuilder
taskSchedulerBuilder
templateEngine
thymeleafViewResolver
tomcatServletWebServerFactory
tomcatServletWebServerFactoryCustomizer
tomcatWebServerFactoryCustomizer
viewControllerHandlerMapping
viewResolver
webSecurityExpressionHandler
webServerFactoryCustomizerBeanPostProcessor
websocketServletWebServerCustomizer
welcomePageHandlerMapping
package com.example.springboot;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}