SpringBoot entry to the master :( B) start parsing principle

Here Insert Picture Description

Foreword

In front of the SpringBoot experience for us to do auto-configuration, quick and easy, complicated profiles compared to before, just not too good, so we can not help but wonder, in the end is how to do it, let us work together to explore what


text

We have developed a Spring Boot any project, will be used as start classes

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

As can be seen from the above code, Annotation definitions (@SpringBootApplication) and class definition (SpringApplication.run) most dazzling, so to demystify SpringBoot, we want these two to start on it.

@ SpringBootApplication : the Spring marked the Boot application instructions on a class of this class is the main class configuration of SpringBoot, SpringBoot should run the main method of this class to start SpringBoot application;
look at his source:

@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 {

SpringBootApplication is a combination of notes, although the definition has been used more Annotation original labeling information, it is important only three Annotation:

  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan

So, if we start using SpringBoot class as follows, the whole class started SpringBoot application can still function as before and so on:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Each write three more tired, so write a @SpringBootApplication convenient point. The next sections describe these three Annotation.

@ 1. SpringBootConfiguration : the Spring of the Boot configuration class;

Marked on a class, he said it was a Spring Boot configuration class;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

}

After @Configuration no stranger to us, that it is the Spring Ioc @ Configuration container JavaConfig form of configuration classes used, SpringBoot recommended community-based configurations JavaConfig, so here start classes marked @Configuration, itself configuration class is actually a IoC container. Configuration class is a container assembly; @Component

A few simple examples under review, XML config configuration with a difference:

Forms of expression levels
based on XML configuration way is this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
       default-lazy-init="true">
    <!--bean定义-->
</beans>

The JavaConfig-based configuration is this:

@Configuration
public class MockConfiguration{
    //bean定义
}

Any marked a Java class definition is a @Configuration JavaConfig configuration class.

Sign bean defined level
based on the configuration in the form of XML is this:

<bean id="userService" class="..UserServiceImpl">
    ...
</bean>

The form-based configuration JavaConfig is this:

@Configuration
public class SpringConfiguration{
    @Bean
    public UserService userService(){
        return new UserServiceImpl();
    }
}

Any method @Bean marked, the return value will be defined as a bean registered Spring IoC container, a default name to the method defined bean id.

Expression level dependency relationship injection
to express dependencies between bean and bean, typically in XML form is:

<bean id="mockService" class="..MockServiceImpl">
    <propery name ="dependencyService" ref="dependencyService" />
</bean>

<bean id="dependencyService" class="DependencyServiceImpl"></bean>

The form-based configuration JavaConfig is this:

@Configuration
public class SpringConfiguration{
    @Bean
    public UserService userService(){
        return new UserServiceImpl(dependencyService());
    }
    
    @Bean
    public DependencyService dependencyService(){
        return new DependencyServiceImpl();
    }
}

If you define a class JavaConfig the bean rely on other bean, directly call the corresponding dependent bean creation method on it.


@ 2. ComponentScan : turn on auto-configuration feature;

@ComponentScan this comment is very important in the Spring, which correspond to elements in the XML configuration, @ ComponentScan function is actually qualified automatically scan and load components (such as @Component and @Repository etc.) or bean definitions, eventually these bean definitions IoC loaded into the container.

We can fine-grained range of attributes such as custom @ComponentScan by basePackages automatic scanning, if not specified, the default will be the Spring framework to achieve a scan package from the class declaration @ComponentScan lies.

Note : So SpringBoot start classes is best placed in the root package, because the default is not specified basePackages.

@ 3. EnableAutoConfiguration : turn on auto-configuration feature;

SpringBoot reason may be zero-configuration, its core is @EnableAutoConfiguration this Annotation, what we used to be configured, Spring Boot to help us to automatically configure; and @ EnableAutoConfiguration tell SpringBoot open the automatic configuration capabilities; the automatic configuration to take effect;
if you remember Spring framework provides a variety of names beginning with @Enable the Annotation definitions? For example, @ EnableScheduling, @ EnableCaching, @ EnableMBeanExport etc., @ EnableAutoConfiguration ideas and ways of doing things is actually the same strain
quick overview that, with the support ** @ Import of collecting and registering bean definitions ** related to a particular scene.
@EnableScheduling @Import by the scheduling framework Spring bean definitions are loaded into the relevant IoC container.
@EnableMBeanExport IoC is loaded into the container through the associated definitions @Import JMX bean.
And also with the help of @EnableAutoConfiguration @Import will load all the bean definitions that meet the criteria to automatically configure the IoC container, and nothing more!

@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

@ AutoConfigurationPackage : Automatic configuration package

​ @Import(AutoConfigurationPackages.Registrar.class):

Spring's annotation @Import, introduced into a container to assembly; introduced by the components AutoConfigurationPackages.Registrar.classWhere all components of all primary package configuration class (@SpringBootApplication class label) and the lower sub-packets of scanned Spring container inside;

​ @Import(EnableAutoConfigurationImportSelector.class);

Import component to the container?

EnableAutoConfigurationImportSelector : which introduced the selector assembly;

All of the components needed to return the introduced full class name manner; these components will be added to the container;

Will give a lot of container imported automatic configuration class (xxxAutoConfiguration); this scenario is to import all the components required for container and configure these components;
Here Insert Picture Description
With automatic configuration class, eliminating the need to manually write the configuration we inject functional components and other work;
by means of a Spring framework existing tools: SpringFactoriesLoader support, SpringFactoriesLoader.loadFactoryNames (EnableAutoConfiguration.class, classLoader); Spring boot acquired from the META-INF class path / spring.factories at startup EnableAutoConfiguration specified value, these values as auto-configuration classes into the container, the class will take effect automatically configured for automatic configuration to work for us; what we need before their own configuration, automatic configuration class to help us;

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
J2EE overall integration solutions and automatic configuration are spring-boot-autoconfigure in

Here Insert Picture Description

Published 40 original articles · won praise 9 · views 20000 +

Guess you like

Origin blog.csdn.net/aawmx123/article/details/102536254