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.
/SpringBootAnnotExamp/src/main/resources/application.properties
server.port = 9093
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.
- @RestController and @Controller annotation example in Spring Boot.
- @RequestMapping annotation example In Spring Boot.
- @RequestBody and @ResponseBody annotation example in Spring Boot.
- @PathVariable and @RequestParam annotations in Spring Boot.
- @RequestHeader annotation example by using Spring Boot.
- @Component, @Controller, @Service and @Repository annotations example using Spring Boot.
- @Configuration annotation example using spring boot.
- @ComponentScan example in spring boot.
- Content negotiation example using Spring Boot.
- Filter example in Spring Boot.
- Spring Boot interceptor example.
- Spring restful web services example using spring boot.
- Deploy multiple war files in JBoss to different port.
- Spring boot datasource configuration using tomcat.
- Jboss 7 EPA datasource configuration using oracle and spring boot.
- @ControllerAdvice Global Error Handling Example in Spring Boot
- @ExceptionHandler Example in Spring Boot.
- Spring Data JPA example using spring boot.
- Spring batch basic.
- Spring batch task scheduling example using spring boot.
- Spring transaction management basic.
- Spring transaction management example using spring boot.
- Spring security default authorization example using spring boot.
- Spring security inMemoryAuthentication and authorization example using spring boot.
- How to deploy multiple war files in Jboss in same port.
- @Transactional noRollbackForClassName example using spring boot
- @Transactional rollbackForClassName example using spring boot
- @Transactional readonly true example in spring boot
- @Transactional noRollbackFor example using spring boot.
- @Transactional rollbackFor example using spring boot.
Check @SpringBootApplication annotation docs.