@SpringBootApplication annotation example in Spring Boot

In this post, we will see @SpringBootApplication annotation example in Spring Boot.

 

  • This annotation is a combined form of @SpringBootConfiguration,@EnableAutoConfiguration, and @ComponentScan.
  • We use this annotation with class.
  • This annotation introduced in Spring  1.2, available in org.springframework.boot.autoconfigure package.
  • Optional elements  (exclude, excludeName, ScanBasePackageClasses, ScanBasePackages).

 

@SpringBootApplication annotation example in Spring Boot.

SpringMain.java

package springbootexample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringMain {
	public static void main(final String[] args) {
		final ConfigurableApplicationContext configurableApplicationContext = SpringApplication
				.run(SpringMain.class, args);
		 
		
	}
}

 

TestContoller.java

package springbootexample;


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

@RestController
public class TestController {
	@RequestMapping(value = "/rest")
	public String printHello(){
		
		return "hello";
	}
}

 

The directory structure should look likes below.

@SpringBootApplication annotation example in Spring Boot

/SpringBootAnnotExamp/src/main/resources/application.properties

server.port = 9093

@SpringBootApplication annotation example in Spring Boot

Let’s modify the SpringMain.java. We will use @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan instead of @SpringBootApplication.

SpringMain.java


package springbootexample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public class SpringMain {
	public static void main(final String[] args) {
		final ConfigurableApplicationContext configurableApplicationContext = SpringApplication
				.run(SpringMain.class, args);
		 
		
	}
}

 

Run this class and hit the rest URI on the browser.

That’s all about @SpringBootApplication annotation example in Spring Boot.

You may like.


 

Check @SpringBootApplication annotation docs.