Building an Application with Spring Boot

For instance, let’s create a simple Web Application. Controller code is shown below:

package com.example.springboot;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {
	@RequestMapping("/")
	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. @RequestMapping 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.

Then an application class:

package 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 package (com/example in the example above), letting it find the controllers.

The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Notice, there was not a single line of XML. And there is no web.xml file, either. This web application is 100% pure Java and there is no need 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 an application or that were automatically added by Spring Boot. It sorts them and prints them out. More exact information about CommandLineRunner (and ApplicationRunner) interface can be found here.

Previous

Leave a Reply

Your email address will not be published. Required fields are marked *