SpringBoot_ study notes

1. The difference between Spring and SpringBoot

2. Create a SpringBoot project

2.1 Created by maven

2.1.1 maven settings

Add to the profiles tag of the settings.xml configuration file of maven

<profile>
  <id>jdk-1.8</id>
  <activation>
    <activeByDefault>true</activeByDefault>
    <jdk>1.8</jdk>
  </activation>
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
  </properties>
</profile>

2.1.2 Create a maven project and import springboot dependencies

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2.1.3 Write a main program; start the Spring Boot application

/**
 *  @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    
    

    public static void main(String[] args) {
    
    

        // Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

2.1.4 Write related Controller and Service

@Controller
public class HelloController {
    
    

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
    
    
        return "Hello World!";
    }
}

Run the main program test

2.2 Created by spring initializr wizard

2.3 Download and import through the official website

3. Simplify deployment

To create an executable jar, we need to spring-boot-maven-pluginadd it to pom.xml.

<!-- 这个插件,可以将应用打包成一个可执行的jar包;-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Mark this application into a jar package and execute it directly with the command of java -jar;

4. Import the configuration file processor

After importing, you will be prompted to write the configuration file

<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

In fact, this configuration file processor is only a reminder when writing, and it is not needed when packaging and running, so for unnecessary classes, you can add the following configuration to exclude

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

5. Container function

5.1 Component addition

Associated spring assembly mechanism

5.1.1 Use @Configuration to add components

Instead of adding beans to the container in the configuration file of beans.xml, use the configuration class.
1. The configuration class uses the @Bean annotation to register the component to the container on the method, and the default is also single-instance;
2. The configuration class itself is also Component
3. proxyBeanMethods: Proxy bean method
Full(proxyBeanMethods = true) ---- 全模式[Ensure that the component returned by how many times each @Bean method is called is a single instance]
Lite(proxyBeanMethods = false) ---- 轻量模式[ How many times each @Bean method is called, the returned components are newly created]

  1. @Configuration(proxyBeanMethods=true): The default is true, which means that the configuration class exists in the container as a proxy object; the bean is created by the proxy object; (the proxy object will find the bean in the container every time, take it if you have it, Create if you don't have it)
  2. Full: There is a dependency between the configuration columns, the method will be called to get the single-instance component, and the Full mode is used
  3. Lite: There is no dependency between the configuration classes, which speeds up the container startup process (no judgment required);

5.1.2 使用@Controller,@Service,@Repository,@Component

5.1.3 Use @ComponentScan, @Import

These two annotations are marked on the component class in the container
@Import: third-party components can be imported

5.1.4 @Conditional can be assembled only when the specified conditions are met

5.1.5 @ImportResource import resources

@ImportResource("classpath:beans.xml")  //导入spring的配置文件

5.2 Configuration binding

5.2.1 @ConfigurationProperties

The group @Componentmust be combined with annotations (the components must be placed in the container)

@Component   //只有在容器中的组件才会拥有spring提供的功能
@ConfigurationProperties(prefix = "mycar")
public class Car {
    
    
    private String name;
    private Double price;
    ...
}
mycar.price=1000
mycar.name=byd

5.2.2 @ConfigurationProperties+ @EnableConfigurationProperties

This combination must be placed on the configuration class, because the configuration class belongs to the component in the container;
the role of @EnableConfigurationProperties: 1. Turn on the configuration binding function of the component; 2. Register the component in the container,
so this method is not needed on the component Annotated @Component will also be managed by the container;

//@Component   //只有在容器中的组件才会拥有spring提供的功能
@ConfigurationProperties(prefix = "mycar")
public class Car {
    
    
    private String name;
    private Double price;
    ...
}
//配置类
@Configuration
@EnableConfigurationProperties(Car.class) //开启Car的配置绑定功能;把Car注册到容器中
public class MyConfig {
    
    
}    

6. Principle of automatic configuration

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

6.1 Boot Load Auto Configuration Class

@SpringBootApplication==>

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
    
     @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication{
    
    }

6.1.1 @SpringBootConfiguration: The bottom layer of this annotation is @Configuration, so its role represents that the main program startup class is a configuration class;

6.1.2 @ComponentScan: Specify which to scan

6.1.3 @EnableAutoConfiguration :

@AutoConfigurationPackage
@Import({
    
    AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    
    

6.1.3.1 @AutoConfigurationPackage (Auto Configuration Package)

@Import({
    
    Registrar.class})
public @interface AutoConfigurationPackage {
    
    

The bottom layer imports a Registrarcomponent to the container, and uses Registrar to import a series of components to the container (import all the components under the specified package (the package where the Main program is located))

6.1.3.2 @Import({AutoConfigurationImportSelector.class})

6.2 Turn on automatic configuration items as needed

6.3 Customized modification of automatic configuration

Guess you like

Origin blog.csdn.net/qq_40084325/article/details/111625954