This is the 30th day of my participation in the August Text Challenge.More challenges in August

Quick use of Spring Boot

1 Create a Spring Boot project

Open IDEA software, click File->New->Project, and select Spring Initializr.

Select the configuration as shown above and click the Next button

Select the project configuration as described above and click the Next button.

Select Spring Web in Web, and next select the code storage address to complete the creation of the project.

2 Spring Boot project entry class

@SpringBootApplication
public class SpringbootdemoApplication {

    public static void main(String[] args) { SpringApplication.run(SpringbootdemoApplication.class, args); }}Copy the code

In the project startup class, add a print statement

@SpringBootApplication
public class SpringbootdemoApplication {

    public staticvoid main(String[] args) { SpringApplication.run(SpringbootdemoApplication.class, args); System.out.println(" project started successfully "); }}Copy the code

Start the project and view the console log:

Create a controller class:

@RestController
public class HelloWorldController {

    @GetMapping("/query")
    public String query(@RequestParam("username") String username, @RequestParam("password")String password){

        return "<h1>"+"username"+username+"password"+password+"</h1>"; }}Copy the code

Restart the project and type in your browser:

http://localhost:8080/query?username=1234&password=5678

3 annotations @ SpringBootApplication

The annotation @SpringBootApplication on the startup class is Spring Boot bootConfiguration: @SpringBootConfiguration,@EnableAutoConfiguration, and @ComponentScan

Source code:

@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
  • @springBootConfiguration Configures class annotations

  • @enableAutoConfiguration Enables automatic configuration annotations

  • @ComponentScan Component scans annotations

1 annotations @ SpringBootConfiguration

Source code:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
public @interface SpringBootConfiguration {
	@AliasFor(annotation = Configuration.class)
	boolean proxyBeanMethods(a) default true;

}
Copy the code

The @SpringBootConfiguration annotation encapsulates the @Configuration annotation.

Inside the @Configuration annotation is the @Component annotation.

The code is as follows:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    @AliasFor( annotation = Component.class )
    String value(a) default "";

    boolean proxyBeanMethods(a) default true;
}
Copy the code

The @Component annotation instantiates ordinary Java objects into the Spring container, similar to bean objects in XML configuration files (see figure below).

<bean id="user" class="com.cf.entity.User"/>
Copy the code

The @Configuration annotation indicates that this is a Spring Configuration file, just like an XML Configuration file.

The Spring container can scan we add @ Component annotation class, Bean registered logic in class org. Springframework. Context. The annotation. ClassPathScanningCandidateComponentProv Ider# registerDefaultFilters

C.2-note @ EnableAutoConfiguration

Partial source code:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    ...
Copy the code

The @enableAutoConfiguration annotation uses @import to Import the Configuration class annotated by @Configuration.

The @import is used to consolidate all Bean configurations defined in the @Configuration annotation

Use the @enableAutoConfiguration annotation to enable automatic configuration of Spring application contexts.

The default package path that Spring Boot scans is the root package of the entry class and its subpackages.

3 annotations @ ComponentScan

The @ComponentScan annotation is equivalent to < Context: Component-scan > in an XML configuration file.

@ ComponentScan processing class is org. Springframework. Context. The annotation. ConfigurationClassParser. This annotation instructs Spring to scan Spring annotated classes under the corresponding package and register them with reflection into the Bean container.

4 XML configuration and annotation configuration

Spring provides two ways to configure beans:

  • The XML configuration
  • Annotation configuration

1 the XML configuration

Advantages: You can easily adjust the Bean management mode and, according to the specification, you don’t have to spend time dealing with dependencies between beans.

Disadvantages: The system is very large, the more complex the function, the more XML configuration files, the relationship is easy to confusion.

2 Annotation Configuration

Advantages: Easy to configure, do not need to know the system object creation, just need to use

Disadvantages: When modifying and deleting a Bean object, you cannot be sure that other beans depend on it (because we are not responsible for maintenance, the container is responsible for managing Bean objects).

2 summary

As mentioned above, Spring Boot well realizes the concept of convention over configuration in terms of development. It can build a project very quickly and carry out rapid development. This programming concept is worth learning.