A quick tutorial for springboot beginners

Time:2021-1-26

What is springboot?

Springboot is a micro framework in spring family, which is designed to simplify the initial construction and development process of new spring applications.

Spring boot makes it easy to create stand-alone, production level spring based applications that you can “run.”.

features:

  • Creating independent spring applications
  • Directly embed tomcat, jetty or undertow (no need to deploy war file)
  • Provide self righteous “getting started” dependencies to simplify build configuration
  • Automatically configure spring and third-party libraries as much as possible
  • Provide production ready functions, such as indicators, health check and external configuration
  • Absolutely no code generation, no XML configuration

What’s springboot going to solve?

With the slow development of Java language, java development is particularly cumbersome, with various configurations, low development efficiency, complex deployment process and third-party technology integration, which is very difficult and complex.

In the above environment, springboot comes from operation. It uses the concept of “habit due to configuration” to make your project run quickly. Using springboot, it is easy to create an independent running environment. It has built-in tomcat, servlet, spring and other basic development functions.

What are the advantages of springboot?

Rapid construction project, agile development

The project can run independently without external dependence on servlet container.

There is no configuration support for the mainstream framework, which simplifies the development and can also modify the default value.

Provides runtime application monitoring. It greatly improves the efficiency of development and deployment.

Natural integration with spring cloud.

Limitation: converting an existing or traditional spring framework project into a spring boot application is a very difficult and time-consuming process. It’s only for new spring projects.

What is micro service?

Firstly, there is no official definition of microservice, so it is difficult to describe microservice directly.

It is a “software architecture style”, an application should be a group of small services. Each small service runs in its own environment and communicates with each other through HTTP.

The core of microservice is to split the traditional one-stop application into one service according to the business, and thoroughly decouple it. Each microservice provides a service with a single business function. One service does one thing. From a technical point of view, it is a small and independent processing process, similar to the concept of process, which can be started or destroyed independently and has its own independent data library

Each functional element is a software unit that can be replaced and upgraded independently. The advantage of it is that it can be combined at will. Greatly enhanced the flexibility of the soft a architecture.

Start the first project of springboot

Environmental preparation:

  • Above jdk8.0.
  • Above Maven 3. X, you need to set the running environment of jdk8.
  • Development tools: STS, idea.

Quickly start a springboot project:

Using idea first

Fill in the coordinates:

Here, you can choose which starter according to the function you need. I will select a web module.

After creating the directory:

He will generate a main program class for us by default. We can start springboot directly through the main program entrance

By default, several directories and files will be generated under the resources template.

Static: mainly used to save all static resources, including JS, CSS, image, etc.

Templates: save all template pages. Springboot deploys projects in the form of jar package by default, so JSP pages are not supported by default, but template engine (freemaker, thymeleaf) can be used.

application.properties : spring boot default configuration file. By default, springboot does not need a configuration file, but you can modify the default value through this class. For example, modify the port number.

application.properties perhaps application.yml Will be the core configuration file.

In springboot, we must inherit the spring boot starter parent package. This is the default configuration information of springboot, which greatly improves our development efficiency.


/**
 * Author: SimpleWu
 * date: 2018/11/27
 */
//@RestController
@Controller
public class HelloController {

 @ResponseBody
 @RequestMapping(value = "hello")
 public String hello(){
 return "SpringBoot ------- hello";
 }
}

Operation mode:

1. Run the main program directly with idea

2. Use Maven command to form a jar package and use Java – jar * *. Jar to run on the console. There is no need to install Tomcat and other tools, which greatly simplifies the deployment process.

matters needing attention:

In springboot, we just need to use @ springbottapplication annotation to modify the entry method class, and then run the main method to start springboot, which is very convenient.

Note: if you remove the @ springbootapplication annotation, you will run an error.

Our main program is springbooth ellowroldApplication.java It’s in package com.simple.springboot ; next

By default, it will only scan to the same package or sub package.

Springboot starter

In springboot, there is a very important concept called “Scene Launcher”, which helps us import the components that the corresponding module depends on for normal operation. Springboot will extract all the function scenarios and make them into starters one by one. It only needs to introduce these starters into the project, and the dependent environments of the related scenarios will be imported. You can import any function you want.

@SpringBootApplication

The spring boot application annotation indicates that this class is the main configuration class of spring boot. Spring boot should run the main method of this class to start the spring boot application.

The annotation is actually a composite annotation, which contains some other annotations.


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

@Springboot configuration inherits from @ configuration, and their functions are the same, indicating that the current class is a configuration class,
One or more instances of methods marked with @ bean annotation declared in the current class will be included in the spring container, and the instance name is the method name

@Enable autoconfiguration: to turn on autoconfiguration means to enable autoconfiguration. Before we need to configure things, now we do not need to configure, springboot help us automatically configure. There are two more important points in the annotation

  • @Autoconfiguration package: automatically configure the package through @ import (auto C) onfigurationPackages.Registrar.class )Class to specify the corresponding package. The default is to import the package of @ springbootapplication and all its sub packages into the container.
  • @Import(AutoConfigu rationImportSelector.class ): a selector for which components to import. All the components that need to be imported will be returned in the form of full class names. These components will be returned to a list configurations collection. These classes are the automatic configuration files of various components, and then added to the container.

Automatic configuration of springboot

The core function of springboot is auto configuration class. A large number of auto configuration classes will be loaded in springboot.

We can check whether the function we need has the automatic configuration class written by spring boot by default. If there is, there is no need to configure. If not, you can view the relevant properties according to the corresponding xproperties class

Then, modify the corresponding attribute value in the configuration file.

Conditional classes begin with @ conditional. The corresponding property class ends with properties.

We will load a large number of autoconfiguration configuration classes through springboot, which correspond to the configuration information of related components.

This class configures related properties through full annotation.

In this configuration class, we can also see some classes that start with @ conditional and end with properties.

Conditional extension:

There are a large number of annotations beginning with @ conditional in the springboot auto configuration class. The purpose of these annotations is that they will take effect only when certain conditions are met.

Although many auto configuration class files are loaded by default, due to the limitation of conditional, not all configuration classes are loaded. This can be done by application.properties Set debug = true in the file. Open the log information of springboot to see which auto configuration classes are loaded.

@Conditional extension annotation Function (judge whether the current specified conditions are met)
@ConditionalOnJava Does the Java version of the system meet the requirements
@ConditionalOnBean The specified bean exists in the container;
@ConditionalOnMissingBean The specified bean does not exist in the container;
@ConditionalOnExpression Satisfies the spel expression specification
@ConditionalOnClass There are specified classes in the system
@ConditionalOnMissingClass There is no specified class in the system
@ConditionalOnSingleCandidate There is only one specified bean in the container, or this bean is the preferred bean
@ConditionalOnProperty Does the property specified in the system have the specified value
@ConditionalOnResource Does the specified resource file exist in the classpath
@ConditionalOnWebApplication Now it’s the web environment
@ConditionalOnNotWebApplication This is not a web environment
@ConditionalOnJndi There is a specified entry in JNDI

Conclusion:

Springboot is a micro service framework, which continues the core ideas of spring framework, IOC and AOP, and simplifies the development and deployment of applications.

Springboot is not a solution to replace spring, but a tool to enhance the experience of spring developers.

Well, the above is the whole content of this article, I hope the content of this article has a certain reference learning value for your study or work, thank you for your support for developer.