search
HomeJavaJavaBaseHow to understand the springboot framework

Spring Boot is a new framework provided by the Pivotal team. It is designed to simplify the initial construction and development process of new Spring applications. The framework uses an ad hoc approach to configuration, eliminating the need for developers to define boilerplate configurations.

How to understand the springboot framework

The operating environment of this tutorial: windows7 system, java10 version, DELL G3 computer.

1. Origin

Spring Boot is a new framework provided by the Pivotal team. It is designed to simplify the initial construction and development process of new Spring applications. The framework uses an ad hoc approach to configuration, eliminating the need for developers to define boilerplate configurations. To understand it in my own words, Spring Boot is actually not a new framework. It configures the use of many frameworks by default, just like Maven integrates all Jar packages and Spring Boot integrates all frameworks.

2. Solve the problem

Build services faster and more conveniently, greatly saving workload, as follows:

  • 1) Configure web.xml and load it Spring and Spring mvc

  • 2) Configure database connection and configure Spring transaction

  • 3) Configure reading of loaded configuration files and enable annotations

  • 4) Configuration log file

  • ...

  • After the configuration is completed, deploy Tomcat for debugging. It may be necessary for even a small function, so it is really troublesome to do it all over again! ! !

  • The introduction of springboot will make it a thing of the past

3. Getting started with springBoot

There are many ways to build it, with Idea Introduction to building a project

  • 1. Select File -> New —> Project... A box to create a new project will pop up

  • ##2. Select Spring Initializr, Next will also have a configuration interface similar to the above. Idea helped us with the integration

  • 3. After filling in the relevant content, click Next, select the dependent package and click Next, and finally confirm Click Finish if the information is correct.

Project structure introduction

How to understand the springboot framework

#Using the default configuration can save a lot of configuration, of course you can also change it according to your own preferences

Finally, start the Application main method, and now a java project is set up!

Introducing the Web module

1. Add a module that supports web in pom.xml:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

There are two modules by default in the pom.xml file:

spring-boot-starter: core module, including automatic configuration support, logging and YAML;

spring-boot-starter-test: test module, including JUnit, Hamcrest, Mockito.

2. Write controller content

@RestController
public class HelloWorldController {
    @RequestMapping("/hello")
    public String index() {
        return "Hello World";
    }
}
@RestController means that all methods in the controller are output in json format, so there is no need to write any jackjson configuration!

3. Start the main program, open the browser and visit http://localhost:8080/hello, and you can see the effect. It is very simple!

How to do unit testing

Open the test entrance under src/test/, write a simple http request to test; use mockmvc, and use MockMvcResultHandlers.print() to print out the execution results.

@RunWith(SpringRunner.class)
   @SpringBootTest
public class HelloWorldControlerTests {
    private MockMvc mvc;
    @Before
    public void setUp() throws Exception {
        mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build();
    }
    @Test
    public void getHello() throws Exception {
    mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
    }
}

Debugging of development environment

Hot start is already very common in normal development projects. Although in the process of developing web projects, an error is always reported when starting and restarting the project; but Spring Boot does Debugging support is very good, and modifications can take effect in real time. You need to add the following configuration:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
   </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
            </configuration>
        </plugin>
   </plugins>
</build>

This module will be disabled when running in a complete packaging environment. If you launch the application using java -jar or a specific classloader, it will consider this a "production environment".

Summary

Using Spring Boot can build projects very conveniently and quickly, so that we don’t have to worry about the compatibility between frameworks, applicable versions and other issues. We want to use anything, just add One configuration is enough, so using Spring Boot is very suitable for building microservices.

Recommended related video tutorials:

Java video tutorial

The above is the detailed content of How to understand the springboot framework. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Java's Nashorn engine for scripting with JavaScript?How do I use Java's Nashorn engine for scripting with JavaScript?Mar 14, 2025 pm 05:00 PM

Java's Nashorn engine enables JavaScript scripting within Java apps. Key steps include setting up Nashorn, managing scripts, and optimizing performance. Main issues involve security, memory management, and future compatibility due to Nashorn's deprec

How do I use Java's enums to represent fixed sets of values?How do I use Java's enums to represent fixed sets of values?Mar 14, 2025 pm 04:57 PM

Java enums represent fixed sets of values, offering type safety, readability, and additional functionality through custom methods and constructors. They enhance code organization and can be used in switch statements for efficient value handling.

How do I use Java's try-with-resources statement for automatic resource management?How do I use Java's try-with-resources statement for automatic resource management?Mar 14, 2025 pm 04:59 PM

Java's try-with-resources simplifies resource management by automatically closing resources like file streams or database connections, improving code readability and maintainability.

What is the Java Virtual Machine (JVM) and how does it work internally?What is the Java Virtual Machine (JVM) and how does it work internally?Mar 14, 2025 pm 05:05 PM

The article discusses the Java Virtual Machine (JVM), detailing its role in running Java programs across different platforms. It explains the JVM's internal processes, key components, memory management, garbage collection, and performance optimizatio

What are different garbage collection algorithms in Java (Serial, Parallel, CMS, G1, ZGC)?What are different garbage collection algorithms in Java (Serial, Parallel, CMS, G1, ZGC)?Mar 14, 2025 pm 05:06 PM

The article discusses various Java garbage collection algorithms (Serial, Parallel, CMS, G1, ZGC), their performance impacts, and suitability for applications with large heaps.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!