Spring Boot provides the @SpringBootApplication annotation [from Spring Boot 1.2.0] in order to enable auto-configuration feature, the @SpringBootApplication annotation performs the work of the three annotations @Configuration, @ComponentScan and @EnableAutoConfiguration.
- @Configuration – Allows you to register beans or import additional configuration class
- @ComponentScan – Enables Component scanning of the packages specified
- @EnableAutoConfiguration – Enables Spring Boot’s auto-configuration feature
In short @SpringBootApplication annotation in equivalent to use @Configuration, @ComponentScan, @EnableAutoConfiguration annotations with their default configuration.
@SpringBootApplication Annotation parameters
@SpringBootApplication annotation takes up four optional parameters
- exclude: This parameter excludes the list of the classes from the auto-configuration
- excludeNames: This parameter excludes the list of fully qualified class names from the auto configuration
- scanBasePackageClasses: This parameters provides the list of classes in the other packages to which has to be applied for scanning
- scanBasePackages: This parameters provides the list of packages which has to be applied for scanning
Folder Structure:
- Create a Maven project (maven-archetype-quickstart) “SpringBootApplication” and create a package for our source files “com.javainterviewpoint” under src/main/java
- Now add the following dependency in the POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javainterviewpoint</groupId> <artifactId>SpringBootApplication</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringBootApplication</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
- Create the Java classes App.java and HelloController.java under com.javainterviewpoint folder.
The spring-boot-starter-parent is a special starter, it provides useful Maven defaults. Since we are developing a web application, we also need to add spring-boot-starter-web dependency.This will add dependencies such Tomcat, Jackson, Spring boot etc which are required for our application.
@SpringBootApplication Annotation Example
Dependency Tree
[INFO] ------------------------------------------------------------------------ [INFO] Building SpringBootApplication 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ SpringBootApplication --- [INFO] com.javainterviewpoint:SpringBootApplication:jar:0.0.1-SNAPSHOT [INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.0.2.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter:jar:2.0.2.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot:jar:2.0.2.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.2.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.2.RELEASE:compile [INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile [INFO] | | | +- ch.qos.logback:logback-core:jar:1.2.3:compile [INFO] | | | \- org.slf4j:slf4j-api:jar:1.7.25:compile [INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile [INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile [INFO] | | \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile [INFO] | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile [INFO] | +- org.springframework:spring-core:jar:5.0.6.RELEASE:compile [INFO] | | \- org.springframework:spring-jcl:jar:5.0.6.RELEASE:compile [INFO] | \- org.yaml:snakeyaml:jar:1.19:runtime [INFO] +- org.springframework.boot:spring-boot-starter-json:jar:2.0.2.RELEASE:compile [INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.5:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile [INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.5:compile [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.5:compile [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.5:compile [INFO] | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.5:compile [INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.2.RELEASE:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.31:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.31:compile [INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.31:compile [INFO] +- org.hibernate.validator:hibernate-validator:jar:6.0.9.Final:compile [INFO] | +- javax.validation:validation-api:jar:2.0.1.Final:compile [INFO] | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile [INFO] | \- com.fasterxml:classmate:jar:1.3.4:compile [INFO] +- org.springframework:spring-web:jar:5.0.6.RELEASE:compile [INFO] | \- org.springframework:spring-beans:jar:5.0.6.RELEASE:compile [INFO] \- org.springframework:spring-webmvc:jar:5.0.6.RELEASE:compile [INFO] +- org.springframework:spring-aop:jar:5.0.6.RELEASE:compile [INFO] +- org.springframework:spring-context:jar:5.0.6.RELEASE:compile [INFO] \- org.springframework:spring-expression:jar:5.0.6.RELEASE:compile
HelloController.java
package com.javainterviewpoint; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String hello() { return "Welcome to JavaInterviewPoint"; } }
- We have created our HelloController under com.javainterviewpoint package
- We have a single method in the controller
- hello() – This method will send a response “Welcome to JavaInterviewPoint”
App.java
package com.javainterviewpoint; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication () public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args); } }
- @SpringBootApplication annotation does the work of @EnableAutoConfiguration, @Configuration and @ComponentScan annotations together
Output:
Select the Project –>Run As –> Run Configuration –>Maven –> New Configuration. In the Main tab, key in the Goals as “spring-boot:run” and click on Run.
Hit on the url : http://localhost:8080/hello
Leave a Reply