“This is the 10th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
Spring Boot makes it easy to create standalone, production-grade Spring-based applications that can be “run straight”.
Spring Boot also provides a number of quick integration solutions for three-party libraries, which are often used as a starter in development. In most cases, Spring Boot does not require much Spring configuration.
Create a project
I recommend you to complete the creation directly on https://start.spring.io/, very suitable for the novice white and I like scientific use of tools. As shown below:
Now Spring, Spring Boot, and other related components are starting to build on Gradle. We use Gradle for dependency management in this and other articles in this series.
For Spring Boot, we will choose 3.x this time, and JDK will still choose 1.8 because it is also the mainstream version of most Internet companies.
The configuration file
The build.gradle configuration file looks like this:
Plugins {id 'org.springframework.boot' version '3.0.0-SNAPSHOT' id 'io.spring. Dependency -management' version RELEASE' id 'Java'} group 'IO. Zhengsh' version '1.0-snapshot' sourceCompatibility = 1.8 configurations { compileOnly { extendsFrom annotationProcessor } } repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' } tasks.named('test') { useJUnitPlatform() }Copy the code
In the configuration file, I mainly added: Spring-boot-starter-Web mainly provides spring MVC related dependencies so that we can easily define interfaces. Lombok is a handy code generation tool that can quickly generate GET and set methods, constructors, and constructors for POJOs, making code very concise and readable. The main attention is to use with IDEA and Eclipse plug-ins
The test code
Create interface API file:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
Copy the code
The interface address is the default access address of the service. When we initiate a GET request, we can print Greetings from Spring Boot! .
Then create the Boot file, this file is a must, it is very powerful, is our Spring Boot Boot file standard template, you can see that we in this process basically no XML business configuration, just completed the writing of Spring Boot application.
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code
@SpringBootApplication is an annotation to an application startup class that adds all of the following:
@Configuration
: Defines the bean source that marks the class as an application context.@EnableAutoConfiguration
: tells Spring Boot to start adding beans based on classpath Settings, other beans, and various property Settings. For example, ifspring-webmvc
On the classpath, this annotation marks the application as a Web application and activates key actions, such as SettingsDispatcherServlet
.@ComponentScan
: tells Spring to look for other components, configurations, and services in the packageio.xcyb
And let it find the controller.
The main() method uses Spring Boot’s SpringApplication.run() method to start the application.
Startup and access
After startup through IDEA, the following information is displayed
$ curl localhost:8080
Greetings from Spring Boot!
Copy the code
Project summary
In fact, Spring Boot is very simple, which greatly simplifies the cost of our development and shields the very complex Spring configuration. Currently, the configuration is basically 0. This is why Spring Boot is widely accepted by Java programmers. On the other hand, it provides very convenient integration/extension points and rich components, which will be updated in the following articles:
- Web components
- The I/O components
- Data Component (SQL/NoSQL)
- Message component (support mainstream MQ: Kafka, RocketMQ, RabbitMQ)
- Security component (support Auth2, Spring Security, LDAP)
- There are also components in the Spring Cloud family based on Spring Boot
Through the support of these components, we can quickly complete a Single Java application, greatly improve our development efficiency, but also reduce the learning cost of program development.
Reference documentation
- Docs. Spring. IO/spring – the boot…