|
Navigation
Search
|
Spring Boot tutorial: Get started with Spring Boot
Wednesday December 17, 2025. 10:00 AM , from InfoWorld
Spring’s most popular offering, Spring Boot is a modern, lightweight extension of the original Spring Framework. Spring Boot offers the same power and range as the original Spring, but it’s streamlined with popular conventions and packages. Both agile and comprehensive, Spring Boot gives you the best of both worlds.
Dependency injection in Spring Boot Like the Spring Framework, Spring Boot is essentially a dependency Injection engine. Dependency injection is slightly different from inversion of control, but the basic idea is the same. Dependency injection is what Spring was created for originally, and it drives much of the framework’s power and popularity. Modern applications use variable references to connect the different parts of the application. In an object-oriented application, these parts (or components) are objects. In Java, we connect objects using classes that refer to each other. Spring’s genius was in moving the fulfillment of variable references out of hard-coded Java and into the framework itself. The Spring framework uses settings you provide to wire together the different parts of your application. Spring Boot takes things a step further, though, by simplifying the way you configure your application settings. It also provides out-of-the-box packages (detailed later in this article) that perform most common application tasks. As an example, in vanilla Java, you might have a Knight class that is associated with a Weapon: public class Knight { private Sword weapon = new Sword(); public void attack() { weapon.use(); } } In Spring, you create the two classes and annotate each one. Spring then automatically fulfills (or “injects”) the association between the two: @Component public class Knight { private final Weapon weapon; @Autowired public Knight(Weapon weapon) { this.weapon = weapon; } public void attack() { weapon.use(); } } In recent versions of Spring (4.3 and higher) even the @Autowired annotation is optional if the class has a single constructor. This update makes Spring Boot feel even more like an extension of the Java platform, although some critique it as a hidden dependency. Getting started with Spring Boot There are a couple of ways to get started with a new Spring Boot application. One is to use the start.spring.io website to download a zipped-up version of a new application. Personally, I prefer using the Spring Boot command line. Of course, to work with Spring, you will need Java installed first. SDKMAN is an excellent tool you can use to install both Java and Spring Boot. Or, if you are using Windows, consider WSL with SDKMAN or a native tool like Scoop. Assuming you already have Java installed, you can start with: $ sdk install springboot The Spring Boot CLI has many powers, but we just want a simple demo application to start with: $ spring init --group-id=com.infoworld --artifact-id=demo --package-name=com.infoworld.demo --build=maven --java-version=17 --dependency=web demo Although this looks like a big chunk of code, it only defines the basics of a Java application—the group, artifact, and package names, as well as the Java version and build tool—followed by one Spring Boot dependency: web. Web development with Spring Boot This web dependency lets you do all the things required for a web application. In Spring Boot, this kind of dependency is known as a “starter”; in this case, it’s the spring-boot-starter-web starter. This web starter adds a single umbrella dependency (like Spring MVC) to the Maven POM. When Spring Boot sees this dependency, it will automatically add web development support, like a Tomcat server. The above command also adds directories and files to the new demo/ folder. One of them is demo/src/main/java/com/infoworld/demo/DemoApplication.java, the main executable for the demo program: package com.infoworld.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } Configuring an application in Spring Boot The @SpringBootApplication annotation is a shorthand for three other annotations that will do most of the up-front configuration of an application: @EnableAutoConfiguration: This powerful annotation instructs Spring to search and configure the context automatically, by guessing and configuring the beans your application may need. In Spring, a bean is any class that can be injected. This annotation creates new beans (like the previously mentioned Tomcat server) based on your application dependencies. @ComponentScan: This annotation enables Spring to search for components. It will automatically wire together the components you have defined (with annotations like @Component, @Repository, or @Bean) and make them available to the application. @Configuration: This annotation lets you define beans directly inside the main class and make them available to the application. The overall effect of these three annotations is to instruct Spring to create standard components and make them available to your web application, while also allowing you to define some components yourself within the main class. These components are then wired up automatically. That’s what they call bootstrapping! Adding a web controller Since we added the web dependency when we created our example project, Spring Boot automatically made a variety of annotations available, including @RestController and @GetMapping. We can use these to generate endpoints for our web application. Then, we can drop the new file right next to the main DemoApplication class: // demo/src/main/java/com/infoworld/demo/HelloController.java package com.infoworld.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping('/') public String hello() { return 'Hello, InfoWorld!'; } } Spring Boot will automatically find this class and load it when we run the application. When we visit the root path, it will simply return a “Hello, InfoWorld!” string. Running a Spring Boot application Now we can run the application from the command line: $./mvnw spring-boot:run This command uses the bundled Maven program to call the built-in spring-boot:run target. This launches the application in dev mode. For a simple test, we can use cURL: $ curl Hello, InfoWorld! As you can see, Spring Boot makes it exceedingly quick and easy to go from a blank canvas to a working endpoint. Spring Boot starter packages In addition to the web starter, Spring Boot includes a large ecosystem of other powerful starter packages that provide out-of-the-box support. This section is a quick overview of some of the most important and popular Spring Boot starters. Core and web spring-boot-starter-web: The starter for RESTful APIs and an embedded Tomcat server. (You’ve already seen this one.) spring-boot-starter-webflux: This is the reactive version of the web starter, allowing for non-blocking endpoints using a Netty server. spring-boot-starter-thymeleaf: Adds the popular Thymeleaf HTML templating engine for Java. spring-boot-starter-websocket: Adds WebSocket support to Spring MVC. Data access spring-boot-starter-data-jpa: Adds the Spring JPA with Hibernate data layer. spring-boot-starter-data-mongodb: Adds MongoDB support. spring-boot-starter-data-redis: Adds support for working with the Redis key-value data store. spring-boot-starter-jdbc: Adds standard JDBC with a connection pool. spring-boot-starter-data-r2dbc: Adds Reactive (non-blocking asynchronous) relational database support. Security spring-boot-starter-security: Adds authentication and authorization support. This starter automatically configures the appropriate security based on the other starters in place. spring-boot-starter-oauth2-client: Provides OAuth2 and OpenID client support (i.e., JWT tokens sent with requests). spring-boot-starter-oauth2-resource-server: Used for handling OAuth requests, most commonly from the app’s own front-end clients, and validating the tokens sent to the endpoints. Messaging spring-boot-starter-amqp: Adds support for RabbitMQ using Spring AMQP. spring-boot-starter-kafka: Adds Apache Kafka for high-throughput messaging. spring-boot-starter-artemis: Adds Apache Artemis as a JMS broker. Operations and testing spring-boot-starter-actuator: Production-grade support for health checks, metrics, and monitoring. spring-boot-starter-test: Testing libraries, including JUnit 5, Mockito, and Spring Test. spring-boot-starter-validation: JavaBean Validation with Hibernate Validator. spring-boot-starter-aop: Aspect-oriented programming with Spring AOP and AspectJ. Configuring Spring Boot applications When we started the example application, it created an application.properties file. This is a text file that we can use to make configuration changes in a central place. For example, if we wanted to change the port that our app listens on from the default 8080, we could add this line: server.port=8081 Or, if we wanted to provide a string value to be used in the application, we could add it here (and then we’d be able to modify it without recompiling): welcome.greeting=Hello from InfoWorld! We can then inject the message into the controller: package com.infoworld.demo; import org.springframework.beans.factory.annotation.Value; // Import this import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Value('${welcome.greeting}') // Inject the property private String message; @GetMapping('/') public String hello() { return this.message; // Return the property } } Conclusion Thanks to its versatility and the structure it provides, Spring is a mainstay of the enterprise. Spring Boot gives you a low-effort entry to Spring without losing any of the Spring Framework’s power. It is an excellent tool to learn both for the present and the future technology landscape.
https://www.infoworld.com/article/4100488/spring-boot-tutorial-get-started-with-spring-boot.html
Related News |
25 sources
Current Date
Dec, Wed 17 - 14:13 CET
|







