前面分析了pom文件的父依赖,以及starter场景启动器,下面说一下helloworld主程序,这个主程序我们只要运行main方法就行了,但是运行main方法要注意,我们spring应用run的时候,要传入一个类,一定是SpringBootApplication来标注的类,如果我把这个注解注掉,然后我来运行一下,这个就报错了,这个注解是非常重要的,SpringBootApplication,我们来说一下这个主程序类,主程序类,也是我们的主入口类,他的这段代码呢,里面有一个核心注解,叫SpringBootApplication,我们翻译过来就是SpringBoot应用,那么这个类我们标注在哪一个类上,这个注解我们标注在哪个类上,说明这个类,是SpringBoot的主配置类,Springboot就应该运行,这个类的main方法,来启动Springboot应用,我们有一个非常重要的注解,SpringBootApplication,这个SpringBootApplication,到底是什么呢,打开可以看一下,其实它是一个组合注解/*** Indicates a {@link Configuration configuration} class that declares one or more* {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration* auto-configuration} and {@link ComponentScan component scanning}. This is a convenience* annotation that is equivalent to declaring {@code @Configuration},* {@code @EnableAutoConfiguration} and {@code @ComponentScan}.** @author Phillip Webb* @author Stephane Nicoll* @since 1.2.0*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {他有这么多的注解来组成的,我们来看一下,复合注解里面呢,第一个注解叫做SpringBootConfiguration,按照我们注解的名字,翻译过来,就叫SpringBoot配置,我们也叫配置类,那么这个注解,他标注在某个类上,表示这是Springboot的一个配置类,这个注解点进来看一下,我们会非常熟悉/*** Indicates that a class provides Spring Boot application* {@link Configuration @Configuration}. Can be used as an alternative to the Spring's* standard {@code @Configuration} annotation so that configuration can be found* automatically (for example in tests).* <p>* Application should only ever include <em>one</em> {@code @SpringBootConfiguration} and* most idiomatic Spring Boot applications will inherit it from* {@code @SpringBootApplication}.** @author Phillip Webb* @since 1.4.0*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {}它上面有一个注解叫Configuration,Spring里面定义的一个注解,他标在某一个类上,配置类上来标注这个注解,我们一直在说这个配置类,所谓的配置类呢,就是和我们的配置文件是一样的,我们以前开发Spring应用,需要编写非常多的配置文件,这文件一多太麻烦了,那接下来怎么办呢,把一个个配置文件替换成一个个的配置类,当然Springboot怎么知道这个类是做配置的,比如可以给容器中,注入组件等等,以前配置文件的功能她都能够做,那么我们要让Springboot知道,这是一个配置类,标注@Configuration注解,Spring就知道了,包括你标注@SpringBootConfiguration,效果都是一样的,只不过这个注解是Spring定义的注解,这个是Springboot定义的注解,而我们这个配置类呢,其实他就是一个组件@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {/*** Explicitly specify the name of the Spring bean definition associated* with this Configuration class. If left unspecified (the common case),* a bean name will be automatically generated.* <p>The custom name applies only if the Configuration class is picked up via* component scanning or supplied directly to a {@link AnnotationConfigApplicationContext}.* If the Configuration class is registered as a traditional XML bean definition,* the name/id of the bean element will take precedence.* @return the suggested component name, if any (or empty String otherwise)* @see org.springframework.beans.factory.support.DefaultBeanNameGenerator*/String value() default "";}我们记住配置类也是容器中的一个组件,这是我们说的第一个注解,叫做SpringBootApplication
然后他的第二个注解,叫做@EnableAutoConfiguration,开启启用的意思,开启自动配置功能,我们整个SpringBoot里面没有做任何配置,我们SpringMVC也启动起来了,我们整个应用也能用了,包扫描也扫进去了,这些功能都是怎么做的呢,就是我们这个注解,叫做@EnableAutoConfiguration,开启自动配置,以前我们需要配置的东西,我们现在都不需要配置了,然后是SpringBoot帮我们配置,要帮我们自动配置,就得需要加上这个注解,这个注解告诉SpringBoot,开启自动配置功能,这样我们整个自动配置,这样自动配置功能才能够生效,而自动配置是一个什么样的原理,我们还是打开来看一下,EnableAutoConfiguration点进来@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";/*** Exclude specific auto-configuration classes such that they will never be applied.* @return the classes to exclude*/Class<?>[] exclude() default {};/*** Exclude specific auto-configuration class names such that they will never be* applied.* @return the class names to exclude* @since 1.3.0*/String[] excludeName() default {};}它里面首先有一个@AutoConfigurationPackage,他也是一个组合注解,EnableAutoConfiguration它里面首先有一个注解,叫AutoConfigurationPackage,那么按照这个注解呢,翻译过来叫做自动配置包,自动配置包是什么意思呢,我们点击这个注解,/*** Indicates that the package containing the annotated class should be registered with* {@link AutoConfigurationPackages}.** @author Phillip Webb* @since 1.3.0* @see AutoConfigurationPackages*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {}它是用@Import(AutoConfigurationPackages.Registrar.class)注解完成的功能,而Import就是Spring的底层注解,他的作用就是给容器中,导入一个组件,导入那个组件呢,后面的这个类,在后面写一些逻辑判断,然后给他返回,由导入的组件这个类来指定,导入哪些组件,由import里面的class类,或者Spring注解的底层原理,我们就说一下AutoConfigurationPackages,他利用import,指定这个类给容器中导入组件,导入了什么组件呢,他这里有一个方法/*** {@link ImportBeanDefinitionRegistrar} to store the base package from the importing* configuration.*/
@Order(Ordered.HIGHEST_PRECEDENCE)
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {@Overridepublic void registerBeanDefinitions(AnnotationMetadata metadata,BeanDefinitionRegistry registry) {register(registry, new PackageImport(metadata).getPackageName());}@Overridepublic Set<Object> determineImports(AnnotationMetadata metadata) {return Collections.<Object>singleton(new PackageImport(metadata));}}叫做registerBeanDefinitions,注册一些bean定义信息,怎么导呢,new PackageImport(metadata).getPackageName(),metadata就是我们注解的原信息,所有的源数据,getPackageName拿到一个包名,首先metadata是注解的元信息,是Springboot注解,这是Springboot注解里面的东西,然后它是标注在HelloWorldMainApplication上的,元信息都能够拿得到,主要有一个new PackageImport(metadata).getPackageName(),可以来看一下包名,来计算一下,这个包名就叫做com.learn所以这个注解,AutoConfigurationPackages它本身的含义,将我们主配置类,主配置类就在这,所在的包下边,所有的组件,都扫描进去,这包名是主配置类的,将主配置类,也就是,就是我们SpringBootApplication注解,标注的这个类,所在包及下边所有子包,里面的所有组件,扫描到Spring容器中,这是非常重要的一句话,所以我们能够扫描我们的Controller,因为它是在我们住配置类的子包下,那么按照这种想法,我一旦说换包了,放到com下的一个HelloController,这里面才是我们的主类,看能不能扫描进来
我再来启动看行不行,我们看能不能把外面的Controller扫进来呢,这个应用启动起来了,它是把error路径映射到了,但是hello没有,如果我们访问hello请求那肯定就是404了localhost:8080/hello报的是404的错误,就是扫不进来,它是将主配置类下面的,所有配置扫进来,这个就是@AutoConfigurationPackage这个注解的作用,@EnableAutoConfiguration注解里面还有一个注解,还标了一个注解是@Import(EnableAutoConfigurationImportSelector.class),我们说了import的左右就是,给容器中导入一些组件,导入什么组件呢,就是后面这个类告诉你,后面这个类翻译过来,开启自动配置导包的选择器,导入哪些组件的选择器,点进来看要导入哪些组件,@Deprecated
public class EnableAutoConfigurationImportSelectorextends AutoConfigurationImportSelector {@Overrideprotected boolean isEnabled(AnnotationMetadata metadata) {if (getClass().equals(EnableAutoConfigurationImportSelector.class)) {return getEnvironment().getProperty(EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY, Boolean.class,true);}return true;}}这里面只有一个方法叫做isEnabled,我们看到他的父类点进来AutoConfigurationImportSelector@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {if (!isEnabled(annotationMetadata)) {return NO_IMPORTS;}try {AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);AnnotationAttributes attributes = getAttributes(annotationMetadata);List<String> configurations = getCandidateConfigurations(annotationMetadata,attributes);configurations = removeDuplicates(configurations);configurations = sort(configurations, autoConfigurationMetadata);Set<String> exclusions = getExclusions(annotationMetadata, attributes);checkExcludedClasses(configurations, exclusions);configurations.removeAll(exclusions);configurations = filter(configurations, autoConfigurationMetadata);fireAutoConfigurationImportEvents(configurations, exclusions);return configurations.toArray(new String[configurations.size()]);}catch (IOException ex) {throw new IllegalStateException(ex);}
}里面有一个方法叫做selectImports,这个方法就是用来告诉Spring容器,到底要导哪些组件,将所有需要导入的组件,以全类名的方式返回,这些组件就会被添加到容器中,我们到底Selector给容器添加了哪些组件呢,我们分析一下刚才的代码,我以debug的方式来进行运行,运行来到这一步,这是我们获取注解的原信息,注解是SprintBootApplication,标在哪个类上都有,接下来放行,这一步返回了listConfigurations,下面都在用,最后还把configurations返回了,这个configurations数组,就是我们容器中需要导入的组件,有96个,AutoConfiguration,听名字呢,什么的自动配置,所以说他的作用,最终会给容器中,会导入非常多的自动配置类,我们叫做AutoConfiguration,这些自动配置类的作用,就是给容器中导入这些场景,需要的所有组件,并配置好这些组件,这就是这些配置类,如果我们要做AOP的功能,那AOP的自动配置类就配置好,我们要做批处理功能,就有批处理的自动配置类,比如我们要做mongodb的功能,那么mongodb的配置就要配置好,这些自动配置呢,都是通过这些自动配置类完成的,我们给容器中导入了大量的自动配置类,自动配置类的细节,我们后来在讲自动配置类的时候再说,有了这些自动配置类,免去了我们手动编写配置,和注入功能组件等工作,这些工作都不用我们做了,由配置类帮我们做好,那自动配置类他怎么就找到的呢,其实我们有据可查,他在调用getCandidateConfigurations,获取配置文件,/*** Return the auto-configuration class names that should be considered. By default* this method will load candidates using {@link SpringFactoriesLoader} with* {@link #getSpringFactoriesLoaderFactoryClass()}.* @param metadata the source metadata* @param attributes the {@link #getAttributes(AnnotationMetadata) annotation* attributes}* @return a list of candidate configurations*/
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,AnnotationAttributes attributes) {List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());Assert.notEmpty(configurations,"No auto configuration classes found in META-INF/spring.factories. If you "+ "are using a custom packaging, make sure that file is correct.");return configurations;
}调了这么一个方法,SpringFactoriesLoader.loadFactoryNames,这个方法里面传两个参数,第一个参数的值点进来/*** Return the class used by {@link SpringFactoriesLoader} to load configuration* candidates.* @return the factory class*/
protected Class<?> getSpringFactoriesLoaderFactoryClass() {return EnableAutoConfiguration.class;
}叫EnableAutoConfiguration.class,第二个参数就是getBeanClassLoader类加载器机制了,然后他的作用是什么,他用classLoader类加载器,获取这个资源,获取完这个资源以后呢,它会把这个资源当做property配置文件,从Properties中拿出factoryPropertyName,我们工厂的名字,从哪里得呢,叫做"META-INF/spring.factories",他的作用就是从类路径下,META-INF/spring.factories中,获取EnableAutoConfiguration指定的值,那么我们可以来看一下,我们导入的jar包类路径里边

# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer# Auto Configuration Import Listeners
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnClassCondition# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration,\
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration,\
org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,\
org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,\
org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration# Failure analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\
org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\
org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer# Template availability providers
org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider=\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.mustache.MustacheTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.web.JspTemplateAvailabilityProvider
就是我们导入的自动配置类,所以我们一句话总结,Spring在启动的时候,从类路径下,这个文件夹中获取值,将这些值作为自动配置类,导入到容器中,然后我们这个自动配置类就生效了,他一生效以后呢,就能帮我们进行自动配置工作了,他只要一进行自动配置工作,我们就不用写那么多的代码了,其实为什么会有这么多神奇的效果,就是这个自动配置类,我们没写的配置,其实人家都帮我们来写了,比如我们来看一个自动配置类,我们现在是WEB应用,跟WEB有关的看一下org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\这里有WEB MVC,@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,WebMvcConfigurerAdapter.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {这个注解以后我们详细再说,我们先来说一个@Bean@Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {return new OrderedHiddenHttpMethodFilter();
}给容器中添加一个组件,这个后来也会说的,比如给容器中添加filter组件,包括给容器中做一些配置,给容器中添加视图解析器@Bean
@ConditionalOnBean(View.class)
@ConditionalOnMissingBean
public BeanNameViewResolver beanNameViewResolver() {BeanNameViewResolver resolver = new BeanNameViewResolver();resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);return resolver;
}@Bean
@ConditionalOnMissingBean
public InternalResourceViewResolver defaultViewResolver() {InternalResourceViewResolver resolver = new InternalResourceViewResolver();resolver.setPrefix(this.mvcProperties.getView().getPrefix());resolver.setSuffix(this.mvcProperties.getView().getSuffix());return resolver;
}我们以前的配置都给配置类给替换了,我们需要自己指定的配置,自动配置类都帮我们做了,其实在底层用Spring的时候,一个都不能少,这个被Spring官方替我们做了,那么我们主要来看,这些自动配置类,其实都是在springboot.autoconfigure包下,这个就是对整个J2EE的大整合,比如有跟高级消息队列的,aop的,有做缓存的,有左DAO的整合,一篮子解决方案全都在这,自动配置,所以我们这个SpringBoot就会这么强大,J2EE整体的解决方案,和自动配置,都在我们这个包里边,spring-boot-autoconfigure-1.5.12.RELEASE-sources.jar他里面来帮我们做了这个事,所以我们用了SpringBoot就不用这个东西了,一切都有人来帮我们配,在自动配置包里边,看每一种功能,都是怎么配的,如果不满意,我们通过不断地学习,也可以自己来改善一些配置

SpringBoot_入门-HelloWorld细节-自动配置相关推荐

  1. SpringBoot_入门-HelloWorld细节-场景启动器(starter)

    前面我们编写了一个springboot,通过这个helloworld我们发现,Springboot确实简单,他只需要写一个主程序,来启动Springboot的应用,接下来我们就按照我们的业务逻辑,编写 ...

  2. SpringBoot_数据访问-JDBC自动配置原理

    整合最基本的JDBC和数据源,第一个MYSQL,导入mysql驱动的,第二个我们使用原生的JDBC,后面使用Mybatis和JPA再选相应的内容就行了,为了演示方便我也把WEB模块选中,我们在pom文 ...

  3. Spring Boot概述与入门特点配置方式注入方式yim配置文件与多文件配置Spring Boot自动配置原理lombok应用

    1. Spring Boot概述 Spring Boot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品: 首页Spring Boot简介可以 ...

  4. SpringCloud 入门教程(三): 配置自动刷新

    Spring Cloud 入门教程(三): 配置自动刷新 之前讲的配置管理, 只有在应用启动时会读取到GIT的内容, 之后只要应用不重启,GIT中文件的修改,应用无法感知, 即使重启Config Se ...

  5. SpringBoot入门-自动配置原理

    3.自动配置原理入门 3.1 引导加载自动配置类 @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFil ...

  6. SpringBoot入门(四)——自动配置

    本文来自网易云社区 SpringBoot之所以能够快速构建项目,得益于它的2个新特性,一个是起步依赖前面已经介绍过,另外一个则是自动配置.起步依赖用于降低项目依赖的复杂度,自动配置负责减少人工配置的工 ...

  7. Spring Boot(1) 入门、自动配置

    Hello,Spring Boot 1.创建一个普通的maven项目 2.pom.xml引入依赖 <parent><groupId>org.springframework.bo ...

  8. SpringBoot (一) 入门、配置、自动配置源码剖析理解

    文章目录 0 Spring Boot 1 Overview 1.1 Introduce **Spring** SpringBoot 微服务 1.2 快速上手 Hello World pom.xml s ...

  9. springboot:自动配置原理入门

    1.引导加载自动配置类 @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Fil ...

最新文章

  1. curl代理ippost php_php使用curl通过代理获取数据的实现方法
  2. 全球及中国自媒体行业营销模式及应用规模前景分析报告2021-2027年
  3. 属性页中的ON_UPDATE_COMMAND_UI
  4. android 版本28 通知栏图标,【专题分析】应用图标、通知栏适配
  5. control层alert弹出框乱码_【ArcGIS for JS】动态图层的属性查询(11)
  6. java21天打卡-day2
  7. Oracle Cluster verification utility failed 的解决方法
  8. 关于Mpush 消息推送 出现的问题
  9. 运维工程师常见软件故障_软件故障分类| 软件工程师
  10. scrapy--dytt(电影天堂)
  11. 微信小程序实现评论多级展开收起以及点赞功能
  12. 我的世界服务器怎么设置自动拾取,自动拾取Auto Pickup Mod
  13. uni-app仿饿了么点餐界面 左右菜单联动 滚动时商家信息、广告吸顶、弹窗下滑动关闭
  14. 2015美国大学计算机科学专业排名,USNews2015美国大学计算机科学专业研究生排名...
  15. oracle表空间怎么改名字,修改oracle数据文件和表空间名字
  16. 用UltraISO制作Ubuntu_18.04U盘启动盘
  17. [Computer Architecture读书笔记] 3.2 Basic Compiler Techniques for Exposing ILP
  18. OpenglES2.0 for Android:来做个地球吧
  19. 中国人寿在线笔试可以用计算机嘛,中国人寿集团校园招聘笔试经验
  20. springboot整合tomcat自带的websocket实现在线聊天及象棋网页对战功能

热门文章

  1. 网络摄像头1 mjpg-streamer使用方法
  2. 快慢法判断单链表中是否有循环链表
  3. J2EE中一些常用的名词【简】
  4. PHP代码审计笔记--变量覆盖漏洞
  5. 2012自学CCNP路由与交换之四交换机初始化
  6. jQuery实现的简单分页功能的详细解析
  7. 第一次打开Lightroom时的基本设置
  8. 查看 SELinux状态及关闭SELinu
  9. 简单排序——冒泡排序,选择排序,插入排序,对象排序
  10. 简单解决“ORA-27100: shared memory realm already exists”的问题