1 Guide project

Because Spring Boot projects are run as executable JAR files, Spring Boot projects define a main class that provides the main method, which is executed when the JAR runs. This main class binds the Spring framework in the form of annotations to complete the basic Spring configuration.

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DeniroSpring5Application { public static void main(String[] args) { SpringApplication.run(DeniroSpring5Application.class, args); }}Copy the code

The @SpringBootApplication annotation indicates that this is a SpringBoot application project.

The @SpringBootApplication annotation is actually a composite annotation:

@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 {
  ...
}
Copy the code
annotations instructions
@SpringBootConfiguration Declaring this class as a Configuration class is a special form of the @Configuration annotation.
@EnableAutoConfiguration Enable automatic configuration of Spring Boot
@ComponentScan Enable component scanning. Classes configured with @Component, @Controller, @Service, and so on will be scanned by Spring and registered as components in the Spring context.

Here’s a quick review of Java meta-annotations:

annotations instructions
@Documented Record the annotation type information into the generated Javadoc document.
@Target(ElementType.TYPE) Specifies the range of objects that an Annotation decorates, and the TYPE representation is used to describe a class, interface (including Annotation types), or enum declaration.
@Retention(RetentionPolicy.RUNTIME) Retentionpolicy. RUNTIME indicates that the annotation is not only saved to the class file, but still exists after the JVM loads the class file.
@Inherited If the annotation is defined on a class, subclasses automatically inherit the annotation. This annotation is valid only if it is used on a class.

The main method of the main class, call SpringApplication. Run (DeniroSpring5Application. Class, args); Perform the project boot process, which creates the Spring application context. The static run() method takes two input arguments, one to the configuration class and one to the command line argument.

2 Test Items

Spring Initializr provides us with a basic test class while creating the project:

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DeniroSpring5ApplicationTests {

    @Test
    void contextLoads() {}}Copy the code

Running results:

There is only one empty test method in this class, which is empty but ensures that the Spring application context is loaded correctly. If the changes we make make it impossible to create a Spring application context, the test method will fail.

Here using the org. Junit. Jupiter. API. The Test as a Test depend on the class, this class is to provide new junit 5 classes.