LEARING SPRING BOOT

LEARING SPRING BOOT

Building an Application with Spring Boot

What You Need

  • About 15 minutes
  • A favorite text editor or IDE
  • Gradle 7.5+ or Maven 3.5+
  • You can also import the code straight into your IDE:
  • VScode


Starting with Spring Initializr

You can use this pre-initialized project and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.

To manually initialize the project:

  1. Go to to Spring initializer.This service pulls in all the dependencies you need for an application and does most of the setup for you.
  2. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.
  3. Click Dependencies and select Spring Web.
  4. Click Generate.


Create a Simple Web Application

Now you can create a web controller for a simple web application, as the following listing (from src/main/java/com/example/springboot/HelloController.java) shows:

COPYpackage com.example.springboot;

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!";
	}

}        

The class is flagged as a @RestController, meaning it is ready for use by Spring MVC to handle web requests. @GetMapping maps / to the index() method. When invoked from a browser or by using curl on the command line, the method returns pure text. That is because @RestController combines @Controller and @ResponseBody, two annotations that results in web requests returning data rather than a view.


Create an Application class

The Spring Initializr creates a simple application class for you. However, in this case, it is too simple. You need to modify the application class to match the following listing (from src/main/java/com/example/springboot/Application.java):

COPYpackage com.example.springboot;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Bean
	public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
		return args -> {

			System.out.println("Let's inspect the beans provided by Spring Boot:");

			String[] beanNames = ctx.getBeanDefinitionNames();
			Arrays.sort(beanNames);
			for (String beanName : beanNames) {
				System.out.println(beanName);
			}

		};
	}

}        

@SpringBootApplication is a convenience annotation that adds all of the following:

  • @Configuration: Tags the class as a source of bean definitions for the application context.
  • @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up a DispatcherServlet.
  • @ComponentScan: Tells Spring to look for other components, configurations, and services in the com/example package, letting it find the controllers.

The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there was not a single line of XML? There is no web.xml file, either. This web application is 100% pure Java and you did not have to deal with configuring any plumbing or infrastructure.

There is also a CommandLineRunner method marked as a @Bean, and this runs on start up. It retrieves all the beans that were created by your application or that were automatically added by Spring Boot. It sorts them and prints them out.

要查看或添加评论,请登录

Ebinazer Selvaraj的更多文章

  • Docker – Deploying WebApps on Docker

    Docker – Deploying WebApps on Docker

    Read Discuss Prerequisite: Docker Docker is an open-source containerization platform for apps and services. It allows…

  • Spring REST API

    Spring REST API

    Spring Boot is a powerful framework that makes it easy to create RESTful APIs. In this article, we will go through a…

  • Securing a Web Application

    Securing a Web Application

    Create an Unsecured Web Application Before you can apply security to a web application, you need a web application to…

  • BUILDING A WEB APPLICATION

    BUILDING A WEB APPLICATION

    Create a Web Controller In Spring’s approach to building web sites, HTTP requests are handled by a controller. You can…

  • Accessing Data with JPA

    Accessing Data with JPA

    Define a Simple Entity In this example, you store objects, each annotated as a JPA entity. The following listing shows…

  • Building Your First Spring Boot Application

    Building Your First Spring Boot Application

    How to complete this guide Like most Spring Getting Started guides, you can start from scratch and complete each step…

社区洞察

其他会员也浏览了