SpringBoot (1) : Features overview

Wechat public account — green plum main code, another paradise of wisdom, waiting for you to come ~

In the past two years, Spring Boot has become the main framework for Java Web development and has become increasingly popular. So why use Spring Boot at all?

1. The birth of SpringBoot

As we know, Java has been used in the IT world for Web application development for about 20 years and is now a full-fledged language. The most popular framework for Java developers is Spring, which has become the true standard for Java EE development.

However, with the development of new technologies and the era of scripting languages (Node JS, Ruby, Groovy, Scala, etc.), the use of Spring in Java EE gradually becomes cumbersome, and a large number of XML files exist in the project, so the tedious configuration of the project itself and the configuration problems of integrating third-party frameworks. Problems such as low development efficiency and deployment efficiency are gradually increasing. With constant community feedback, the Spring team developed a framework based on Spring: Spring Boot.

Spring Boot is indeed a worthy javaEE development disrupter ~ and Spring has officially promoted Spring Boot as the company’s top project, put on the first position on the official website, so the continued development of Spring Boot is also expected. Let’s take a look at the core features of SpringBoot.

2. Core functions

The core features of Spring Boot include the following:

1) Automatic Configuration -Auto Configuration 2) Starter Dependency 3) CLI interface -Spring Boot CLI 4) Running monitoring -ActuatorCopy the code

The most important and most commonly used of these are automatic configuration and startup dependencies

Let’s start with automatic configuration

Spring Boot is automatically configured based on the Java configuration in the class, the configuration items in the configuration file application.properties, and the configuration context, based on the jar dependencies added. These automatic configurations are achieved by relying on spring-boot-Autoconfiguration

Doesn’t it sound a little confusing at first? It’s like putting an elephant in a refrigerator. The loading process is divided into three steps:

A. Load the configuration file

Sring Boot loads the properties in Spring.Factories when it starts

org.springframework.boot.autoconfigure.EnableAutoConfiguration

Property is configured with all the auto-configuration classes supported by Sping Boot.

B. Declare related classes

Enter a class and declare the corresponding class by a series of annotations such as @Conditional

C. Load configuration items

Load the corresponding configuration items using the default configuration items in the corresponding propertis file. If there is a configuration in the project in the form of Java or a configuration file, use the value of that configuration directly, otherwise use the default values

This is why we have no port configured for the Web service, but the service starts with port 8080 by default; Once configured, the project will start using our port number

If there is a little bit of confusion, let’s go into the source code and use examples to see

Take an example of SpringBoot loading a Redis client

If we introduce redis client dependencies (jedis and any other kind of client) in our project, Spring Boot will load the following configuration according to the Redis auto-configuration class RedisAutoConfiguration

ConditionalOnClass()** @conditionalonclass ()**

2) If yes, all properties of redis in RedisProperties will be enabled;

This class uses a prefix and attributes to combine our attributes, where specific attributes can be cascaded, such as spring.redis. Host for spring port configuration.

3) Then pass the code

@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
Copy the code

Introducing LettuceConnectionConfiguration, JedisConnectionConfiguration respectively

These two client classes (if you are interested, go to these classes and see that they also use the ** @Conditional ** annotation to continue the judgment), each client will make its own connection and configure it accordingly

RedisTemplate and stringRedisTemplate are created respectively. They both use the @conditionalonmissingBean annotation, which means we can inject them directly for use. They can also be overridden by your own definition, depending on the business scenario.

As a result, as soon as we introduce the client dependencies of reids, Spring Boot automatically configures the redis address, port number, etc., and declares the redisTemplate for us.

If you are connecting to local Redis and the port is the default port, all you need to do is inject the redisTemplate directly and use the methods it provides to manipulate redis. Isn’t that amazing!!

Now follow the source code walk again, it will be easier to figure out ~

At this point, we understand the mysterious face of Spring Boot automatic configuration ~

We will continue to guide you to implement your own automatic configuration next time. For a deeper understanding of the principle of automatic configuration, please look forward to oh ~

Okay, now that we’ve got auto configuration out of the way, let’s take a look at starting dependencies again,

Startup dependencies are another core feature of SpringBoot. This is reflected in the following two points

1) Direct to features

2) Get all dependencies in one station, no more copy and paste

In other words, with Spring Boot, we know what we need to do. As long as we introduce a corresponding starter-denpendency,Spring Boot will help us configure the corresponding dependencies according to our needs, instead of introducing multiple dependencies one by one. Avoid poM files becoming verbose and prone to dependency conflicts

For example, if we want to implement web functionality, we directly introduce Spring-boot-starter-Web, which includes JSON, Tomcat, Web, WebMVC, and Hibernate validation dependencies. If there is no particular business need, then we are perfectly good enough to use this set of dependencies

Note: All starter can be viewed through the official website

Docs. Spring. IO/spring – the boot… You can also use the POM after each starter to see which dependencies are introduced for that dependency

3. The advantages

1) Isomerism

Each microservice can be stored in different languages and databases according to the service situation.

2) elastic

With microservices, if a component becomes unavailable, there is no cascading expansion.

3)

Individual services are not easy to scale, and multiple smaller services can be scaled on demand. For example, to update the charging rules of e-commerce platforms, it may only be related to individual services such as order services and user services, so it is only necessary to expand these affected services.

4. The cost of implementing microservices

Of course, nothing is perfect, and Spring Boot comes at a price

1) The complexity of distributed systems

With the Spring Boot microservices architecture, we naturally had to consider the service management issues of distributed systems.

2) The complexity of development, testing and many other r&d processes

If a service depends on other services, starting the service requires that all the services it depends on are started in the same way. For example, in the microservice system, there will always be A service dependent on B service, dependent on C service, etc. Development tests can become complicated.

3) Deployment, monitoring and other operation and maintenance complexity

Likewise, deployment, monitoring, and the number of services to focus on can become more complex.

5. To summarize

Basically, Spring Boot is a secondary encapsulation of the Spring Framework. To simplify development and allow programmers to focus more energy and time on the business. Avoid tedious configuration operations, thus reducing the number of bugs encountered. For operations personnel, although there may be more services to care about, maintenance of each service can be elegantly maintained using simple scripts

Therefore, in general, the emergence of Spring Boot greatly reduces the complexity of development and operation and maintenance, which is also an inevitable trend of software development