Basic introduction to SpringBoot

SpringBoot is a new framework proposed by the Pivotal team. Its purpose is to simplify the construction of Spring applications.

Before using SpringBoot, we usually need to write some XML to configure the project when using Spring to build the project, and usually need to manually add maven configuration, which makes it difficult to build the framework and high time cost. When you debug a project locally or deploy it online, you usually compile the project into a WAR package and deploy it to Tomcat.

The emergence of SpringBoot, changed this situation, SpringBoot has the characteristics of automatic assembly, and SpringBoot embedded container, so that we do not install Tomcat, directly by running the main method to start, very convenient;

Spring-boot-starter -data-redis, spring-boot-starter-data-mongodb, etc. For me, who had been using Spring for previous projects, it was amazing that a simple configuration made it so easy for Spring to integrate Redis and mongodb. Therefore, I spent some time today learning about starter and how to customize starter.

Custom starter

First, we need to create a Configuration class. This class is mainly used to configure parameters and automatically configure external functions.

@Configuration
@EnableConfigurationProperties(CourseProperties.class)
public class CourseAutoConfigure {

    @Bean
    public CourseClient courseClient(CourseProperties courseProperties){
        return newCourseClient(courseProperties); }}Copy the code

The above code shows that we provide a CourseClient object, which we can then use to implement some functionality. Since we are only here to learn how to create a custom starter, we will definitely use some simple demos to understand the process.


public class CourseClient {

    private CourseProperties courseProperties;

    public CourseClient(a){}public CourseClient(CourseProperties courseProperties){
        this.courseProperties = courseProperties;
    }

    public String getName(a){
        returncourseProperties.getName(); }}Copy the code

As you can see, the functionality we’re exposing is the course name of our CourseProperties object;

@Data
@ConfigurationProperties(prefix = "spring.course")
public class CourseProperties {

    private String name;
    
}
Copy the code

If you are using SpringBoot, you may have noticed that idea always prompts you to write some configuration in YAML or properties. In fact, this implementation is very simple

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
Copy the code

Json = spring-configuration-metadata.json = spring-configuration-metadata.json = spring-configuration-metadata.json = spring-configuration-metadata.json = spring-configuration-metadata.json = spring-configuration-metadata.json

Starter integrated application

Our functionality is written, so how do we integrate it into our project? Here is divided into two ways, one is active, one is passive;

Active activation means that the starter can take effect only when we actively declare to Enable the starter. In this case, the @import annotation is used to mark the annotation to the user-defined @enable annotation.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({CourseAutoConfigure.class})
public @interface EnableCourseClient {
}
Copy the code

Passive enabler means that the starter component is already available to the SpringBoot application when it is integrated into the SpringBoot application. Create a meta-INF /spring.factories write

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.learn.springboot.starter.autoconfigure.CourseAutoConf igureCopy the code

Integrated application testing

We created a New SpringBoot Web application, and first we tested it in an active way

@EnableCourseClient
@SpringBootApplication
public class Application {

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

    @Autowired
    private CourseClient courseClient;

    @GetMapping("courseName")
    public String getCourseName(a){
        returncourseClient.getName(); }}Copy the code

Configure it in application.properties

spring.course.name=python
Copy the code

Browser to access: localhost: 8080 / courseName can return to normal python

If we remove the @enablecourSeclient annotation, we will get an error at startup

2021-05- 08 09:51:53.924  INFO 58146 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-05- 08 09:51:53.944 ERROR 58146 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field courseClient in com.learning.springboot.controller.CourseController required a bean of type 'com.learn.springboot.starter.autoconfigure.CourseClient' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.learn.springboot.starter.autoconfigure.CourseClient' in your configuration.

Copy the code

Now let’s do the passive test, remove the @enablecourSeclient and write to meta-INF/Spring.Factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.learn.springboot.starter.autoconfigure.CourseAutoConf igureCopy the code

Browser to access: localhost: 8080 / courseName can return to normal python

Other supplementary

Sometimes we want to dynamically control whether or not the starter is available through configuration. In this case, we can use the @condition annotation, for example

@Bean
@ConditionalOnProperty(prefix = "spring.course", value = "enabled", havingValue = "true")
public CourseClient courseClient(CourseProperties courseProperties){
    return new CourseClient(courseProperties);
}
Copy the code

The configuration is as follows:

spring.course.name=python
spring.course.enabled=true
Copy the code

If you change true to false, you will also find startup errors

Problems encountered in the learning process

During the learning process, it is found that the starter can be used normally without either active or passive activation. The package name of the starter project is the same as that of the application package.