Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to interpret Hello World program line by line in Spring Boot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to interpret the Hello World program line by line in Spring Boot, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. Preface

I have been studying Spring boot for a short time. Recently, I will sort out the research things and share them with you. There will be about 10 to 20 blogs. The whole series will be based on a simple blog system, because many things in theory alone are not easy to understand, and if you can't systematically grasp some knowledge points through a simple Mini Program each time. So based on a simple system, see how a complete system can be implemented through spring boot. In addition to the basic knowledge points of Spring boot, this series will also cover the combination of Spring boot with databases, redis, message queues, and multi-instance deployment. Students who are interested can pay attention to it.

II. Brief introduction of Spring boot

Spring boot can be seen from the name that it is a framework based on Spring, so students who are not familiar with Spring have to learn Spring first. Second, Spring boot helps us integrate a lot of commonly used functions, making the whole configuration easier. Students who have used Spring should know that although Spring has been trying to reduce the complexity of configuration, it is quite troublesome to configure a fully available (web) environment, such as log, database, cache, etc., and then configure tomcat, and finally publish the program to the tomcat directory. Spring boot, on the other hand, greatly simplifies the process, providing a lot of starter, as long as the corresponding jar package is introduced. For example, we need to integrate tomcat, and we only need to introduce tomcat's starter:

Org.springframework.boot spring-boot-starter-tomcat

Note: the examples in this article are all based on Maven, so if you are not familiar with Maven, you can first take a look at how to use it. If you are familiar with gradle, you can adjust the configuration accordingly.

We can look at the starter provided by Spring boot from the official documentation:

I've only intercepted a small portion here, and you can see that Spring boot supports caching, batch processing, mq, es, and so on, and the complete list refers to the official documentation. The rest will not be explained much, followed by examples to explain the entire Spring boot function, let's first take a look at Spring boot to achieve a web version of Hello World!

3. Hello World program

3.1 Hello World source code

Step 1: import the jar package

Org.springframework.boot spring-boot-starter-parent 1.5.8.RELEASE org.springframework.boot spring-boot-starter-web

Step 2: write the controller class

Package com.pandy.blog;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import java.util.HashMap;import java.util.Map;@Controllerpublic class HelloWorld {@ RequestMapping ("/ hello") @ ResponseBody public Map hello () {Map map = new HashMap (); map.put ("hello", "world"); return map;}}

Step 3: write the startup class (storage)

Package com.pandy.blog; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {public static void main (String [] args) throws Exception {SpringApplication.run (Application.class, args);}}

Run the main method of the class, then visit http://localhost:8080/hello, and you can see the following result:

Do you feel happy? Without a single line of configuration, you can run a Web application directly. But after having fun, have you ever thought about how this is achieved? Next we parse the above code one by one, although there are not many lines, there are still a lot of things for us to learn and understand.

3.2 pom file analysis

Let's start with the pom file, where only two dependencies are introduced in the pom file. The first is spring-boot-starter-parent, and friends who are familiar with Maven should know that Maven can inherit configuration from the parent pom file, just like classes. We can take a look at the pom file of spring-boot-starter-parent. Because of the space problem, there are only two parts in it. Other things are easier to understand. You can read them by yourself. The first part is:

This file inherits another pom file, that is, spring-boot-dependencies, which actually contains a lot of jar, which is used to manage the version of the jar package that spring boot depends on, so you can see later that you no longer need to specify the version number when introducing jar into each component. Another place to explain is the management of configuration files:

As you can see, by default, the files in the / src/main/resources directory are added to the classpath as resource files. In addition, only three application*.yml,application*.yaml,application*.properties files are filtered in this place. What does this filter mean? Anyone who has configured spring mvc should know that when configuring a database, we usually configure the information of the database in a properties file and then introduce it in the configuration file of spring. The function of this filter is to replace the name-value pair configured in the configuration file with the ${xxx} character in the configuration file of spring when compiling, but this function is not necessary, even if it is not replaced. Spring can also read configuration items at run time.

To sum up: the role of spring-boot-starter-parent:

1) jar package management.

2) default profile management.

3) commonly used plug-in management.

In fact, from the analysis of what can be seen, the core function of spring-boot-starter-parent is to manage all the jar packages that Spring boot depends on. However, there is an obvious problem with the way to access parent. Many companies have their own parent files, and maven has no way to configure multiple parent. What should I do if I don't use spring-boot-starter-parent? Do you need to import your own jar package? In fact, Spring boot provides another way to solve this problem by adding spring boot dependency management to its own pom file:

Org.springframework.boot spring-boot-dependencies 1.5.9.RELEASE pom import

In fact, as can be seen from the above analysis, this is also the parent of spring-boot-starter-parent 's pom file, and this pom file mainly manages a lot of jar packages. So after importing this, you don't need to import a jar by yourself. But in this case, the plug-ins in spring-boot-starter-parent will not be available, and the replacement function of the default configuration file will be lost. However, this does not matter, on the one hand, these features are not necessary, on the other hand, if necessary, it is easy to add their own.

3.3 HelloWorld class resolution:

If we take a look at the class HelloWorld, we should know when we have used Spring mvc. In fact, this class has nothing to do with Spring boot, and the business code has nothing to do with spring. This is also a principle that spring has always adhered to, with minimal intrusiveness, which is also one of the main reasons for the success of Spring. There are three annotations related to spring in this class, @ Controller,@RequestMapping,@ResponseBody, but all three annotations are also provided by Spring mvc. I don't have much to do with Spring boot, so I won't go into details here. If it's not very clear, you can take a look at the content of Spring MVC. The basic functions of the three notes are as follows:

Controller: identified as a controller, spring automatically instantiates this class.

RequestMapping:url mapping.

ResponseBody: automatically converts the returned result to a json string.

3.4 Application class parsing

Finally, if we take a look at the Application class, you will find that there is less in this class, with a total of one line of useful code, that is, SpringApplication.run (Application.class, args). The purpose of this method is to load the Application class. Is there anything special about the Application class? You can take a look. In fact, the only special feature of this class is an annotation @ SpringBootApplication, so the operation of Spring boot must have a lot to do with this annotation. We can take a look at the source code of this 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 {

The main method of the annotation is not to say, as you can see, the main purpose is to provide aliases for the above notes. The first four annotations (@ Target (ElementType.TYPE), @ Retention (RetentionPolicy.RUNTIME), @ Documented,@Inherited) should all know that unfamiliar friends go to see for themselves how JDK implements custom annotations. Let's explain the last three annotations in detail: @ SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan.

Take a look at SpringBootConfiguration first. This annotation is relatively simple. The source code is as follows:

@ Target ({ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Configurationpublic @ interface SpringBootConfiguration {}

This annotation only inherits @ Configuration, as you should know, Spring provides three ways of configuration: (1) xml file configuration (2) annotation configuration (3) Java class configuration. And @ Configuration is an annotation that identifies a class as a configuration class. After Spring 4, it is more popular to configure through the Java class, so Spring boot also tends to this kind of square configuration. And as can be seen from the source code, the role of SpringBootConfiguration is to identify the class as the configuration class.

Next, let's take a look at the @ EnableAutoConfiguration annotation. The source code of this annotation is a bit complicated. I won't go into details here, but the following article will elaborate on the implementation of the parsing phase. Here to talk about the role of the annotation, its main function is to achieve automatic configuration, what is called automatic configuration? That is, Spring boot will automatically configure according to the jar package you introduce. For example, jar,spring boot with HSQLDB in classpath will automatically configure an in-memory database for you. In this example, we can also see that because we introduced Spring-mvc, tomcat and other related jar,spring boot, we will guess that you are a web project, and then automatically do some spring mvc configuration, such as support for static resources, support for automatically converting the returned results to json format data, and so on. These are the results of automatic configuration. Students who are familiar with Spring Enable* annotations should be able to understand this annotation more easily because there are many similar annotations in Spring.

Finally, let's take a look at @ ComponentScan. This annotation is not provided by Spring boot, but the packages or classes provided by Spring and scanned by Spring, that is, which packages and classes are automatically managed by the Spring IoC container. IoC instantiates these classes according to configuration.

Now let's summarize the role of the SpringBootConfiguration annotation:

1) Mark that this class is a configuration class.

2) specify the scanned package to facilitate the instance and lifecycle management of the Spring IoC container.

3) automatic configuration, through the introduction of the jar package, guess the user's intention for automatic configuration.

The above is all the contents of the article "how to interpret Hello World programs line by line in Spring Boot". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report