<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Spring Boot2 `@SpringBootApplication`自動配置 > 原文: [https://howtodoinjava.com/spring-boot2/springbootapplication-auto-configuration/](https://howtodoinjava.com/spring-boot2/springbootapplication-auto-configuration/) Spring Boot 非常易于使用,它在后臺執行了許多操作,您可能沒有意識到。 將來,一個好的開發人員將確切地了解 **spring boot 自動配置**背后的工作,如何以自己的喜好使用它,以及如何禁用某些不需要的部分到您的項目中。 為了了解 Spring Boot 背后的大多數基本知識,我們將創建一個具有單個依賴項和單個啟動類文件的最小啟動應用程序。 然后,我們將分析啟動日志以獲取見解。 ## 使用啟動類創建 Spring Boot 應用程序 1. 在 Eclipse 中創建一個新的 Maven 項目,原型為`maven-archetype-quickstart`。 2. 使用`spring-boot-starter-web`依賴和插件信息更新`pom.xml`文件。 ```java <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd; <modelVersion>4.0.0</modelVersion> <groupId>com.howtodoinjava</groupId> <artifactId>springbootdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springbootdemo</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>repository.spring.release</id> <name>Spring GA Repository</name> <url>http://repo.spring.io/release</url> </repository> </repositories> </project> ``` 3. 創建啟動應用。 ```java package com.howtodoinjava.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; @SpringBootApplication public class App { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(App.class, args); } } ``` #### 這個入門班是做什么的? 上面的類稱為 Spring Boot 應用程序啟動類。 它曾經通過 Java `main()`方法來運行和啟動 Spring 應用程序。 它通常會執行以下操作: * 創建 Spring 的`ApplicationContext`的實例。 * 啟用該功能以接受命令行參數并將其公開為 Spring 屬性。 * 根據配置加載所有 Spring bean。 您還可以根據項目需要進行其他操作。 ## `@SpringBootApplication`注解 此注解是在一條語句中應用 3 個注解的快捷方式: 1. #### `@SpringBootConfiguration` [@SpringBootConfiguration](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/SpringBootConfiguration.html) 是 Spring Boot 2 中的新注解。以前,我們一直在使用[`@Configuration`](https://docs.spring.io/spring-framework/docs/5.0.4.RELEASE/javadoc-api/org/springframework/context/annotation/Configuration.html)注解。 您可以代替使用`@Configuration`。 兩者都是同一回事。 它指示一個類提供 Spring Boot 應用程序`@Configuration`。 它僅表示帶注解的類是配置類,應對其進行掃描以獲取進一步的配置和 Bean 定義。 2. #### `@EnableAutoConfiguration` 該注解用于啟用 Spring 應用下上文的自動配置,嘗試猜測和配置您可能需要的 bean。 通常根據您的類路徑和定義的 bean 來應用自動配置類。 自動配置會嘗試盡可能智能化,并且在您定義更多自己的配置時會自動退出。 您始終可以使用兩種方法手動排除您永遠不想應用的任何配置: i)使用`excludeName()` ii)使用屬性文件中的`spring.autoconfigure.exclude`屬性。 例如 ```java @EnableAutoConfiguration(excludeName = {"multipartResolver","mbeanServer"}) ``` 自動配置始終在注冊用戶定義的 bean 之后應用。 3. #### `@ComponentScan` 該注解提供與 Spring XML 的`context:component-scan`元素并行的支持。 可以指定`basePackageClasses()`或`basePackages()`定義要掃描的特定程序包。 如果未定義特定的程序包,則將從聲明此注解的類的程序包中進行掃描。 ## 運行啟動應用程序并檢查日志 讓我們以最簡單的選項開始運行它 - 作為 Java 應用程序運行。 在您的 IDE 中,右鍵單擊應用程序類,然后將其作為 Java 應用程序運行。 為了了解已注冊的 bean,我添加了如下修改的啟動應用程序。 ```java @SpringBootApplication public class App { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(App.class, args); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } } } ``` 現在查看日志: ```java . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.0.RELEASE) 2018-04-02 13:09:41.100 INFO 11452 --- [ main] com.howtodoinjava.demo.App : Starting App on FFC15B4E9C5AA with PID 11452 (C:\Users\zkpkhua\IDPPaymentTransfers_Integrated\springbootdemo\target\classes started by zkpkhua in C:\Users\zkpkhua\IDPPaymentTransfers_Integrated\springbootdemo) 2018-04-02 13:09:41.108 INFO 11452 --- [ main] com.howtodoinjava.demo.App : No active profile set, falling back to default profiles: default 2018-04-02 13:09:41.222 INFO 11452 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4450d156: startup date [Mon Apr 02 13:09:41 IST 2018]; root of context hierarchy 2018-04-02 13:09:43.474 INFO 11452 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2018-04-02 13:09:43.526 INFO 11452 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-04-02 13:09:43.526 INFO 11452 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.28 2018-04-02 13:09:43.748 INFO 11452 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-04-02 13:09:43.748 INFO 11452 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2531 ms 2018-04-02 13:09:43.964 INFO 11452 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-04-02 13:09:43.969 INFO 11452 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-04-02 13:09:43.970 INFO 11452 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-04-02 13:09:43.970 INFO 11452 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-04-02 13:09:43.970 INFO 11452 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-04-02 13:09:44.480 INFO 11452 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4450d156: startup date [Mon Apr 02 13:09:41 IST 2018]; root of context hierarchy 2018-04-02 13:09:44.627 INFO 11452 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-04-02 13:09:44.630 INFO 11452 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-04-02 13:09:44.681 INFO 11452 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-04-02 13:09:44.682 INFO 11452 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-04-02 13:09:44.747 INFO 11452 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-04-02 13:09:45.002 INFO 11452 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-04-02 13:09:45.070 INFO 11452 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-04-02 13:09:45.076 INFO 11452 --- [ main] com.howtodoinjava.demo.App : Started App in 4.609 seconds (JVM running for 5.263) app basicErrorController beanNameHandlerMapping beanNameViewResolver characterEncodingFilter conventionErrorViewResolver defaultServletHandlerMapping defaultValidator defaultViewResolver dispatcherServlet dispatcherServletRegistration error errorAttributes errorPageCustomizer errorPageRegistrarBeanPostProcessor faviconHandlerMapping faviconRequestHandler handlerExceptionResolver hiddenHttpMethodFilter httpPutFormContentFilter httpRequestHandlerAdapter jacksonCodecCustomizer jacksonObjectMapper jacksonObjectMapperBuilder jsonComponentModule localeCharsetMappingsCustomizer mappingJackson2HttpMessageConverter mbeanExporter mbeanServer messageConverters methodValidationPostProcessor multipartConfigElement multipartResolver mvcContentNegotiationManager mvcConversionService mvcHandlerMappingIntrospector mvcPathMatcher mvcResourceUrlProvider mvcUriComponentsContributor mvcUrlPathHelper mvcValidator mvcViewResolver objectNamingStrategy org.springframework.boot.autoconfigure.AutoConfigurationPackages org.springframework.boot.autoconfigure.condition.BeanTypeRegistry org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration 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.http.codec.CodecsAutoConfiguration org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration$JacksonCodecConfiguration 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.jmx.JmxAutoConfiguration org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration 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.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter$FaviconConfiguration 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.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.annotation.internalRequiredAnnotationProcessor org.springframework.context.event.internalEventListenerFactory org.springframework.context.event.internalEventListenerProcessor parameterNamesModule preserveErrorControllerTargetClassPostProcessor propertySourcesPlaceholderConfigurer requestContextFilter requestMappingHandlerAdapter requestMappingHandlerMapping resourceHandlerMapping restTemplateBuilder server-org.springframework.boot.autoconfigure.web.ServerProperties servletWebServerFactoryCustomizer simpleControllerHandlerAdapter spring.http.encoding-org.springframework.boot.autoconfigure.http.HttpEncodingProperties spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties 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 standardJacksonObjectMapperBuilderCustomizer stringHttpMessageConverter tomcatServletWebServerFactory tomcatServletWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer viewControllerHandlerMapping viewResolver webServerFactoryCustomizerBeanPostProcessor websocketContainerCustomizer welcomePageHandlerMapping ``` 您會看到有多少個 bean 自動注冊。 那是 SpringBoot 子的美。 如果您想更深入地研究為什么要注冊任何特定的 bean? 通過在應用程序啟動時放置調試標志,您可以看到這一點。 **只需將`-Ddebug=true`作為 VM 參數傳遞。** 現在,當您運行該應用程序時,您將獲得許多調試日志,它們具有類似的信息: ```java CodecsAutoConfiguration.JacksonCodecConfiguration matched: - @ConditionalOnClass found required class 'com.fasterxml.jackson.databind.ObjectMapper'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition) CodecsAutoConfiguration.JacksonCodecConfiguration#jacksonCodecCustomizer matched: - @ConditionalOnBean (types: com.fasterxml.jackson.databind.ObjectMapper; SearchStrategy: all) found bean 'jacksonObjectMapper' (OnBeanCondition) DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched: - @ConditionalOnClass found required class 'javax.servlet.ServletRegistration'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition) - Default DispatcherServlet did not find dispatcher servlet beans (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition) DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration matched: - @ConditionalOnClass found required class 'javax.servlet.ServletRegistration'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition) - DispatcherServlet Registration did not find servlet registration bean (DispatcherServletAutoConfiguration.DispatcherServletRegistrationCondition) ... ... ... ``` 上面的日志說明了為什么將特定的 bean 注冊到 spring 上下文中。 當您調試自動配置問題時,此信息非常有用。 同樣,每次我們向 Spring Boot 項目添加新的依賴項時,Spring Boot 自動配置都會自動嘗試基于該依賴項配置 Bean。 希望以上討論的信息將來能在調試 Spring Boot 相關問題時為您提供幫助。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看