Thursday 5 September 2024

What is the purpose of @SpringBootApplication annotation?

                                         JAVA with Silan Software

@SpringBootApplication annotation represent that the corresponding application is a spring boot application. It is working for three annotations like @Configuration, @EnableAutoConfiguration, and @ComponentScan.

@Configuration : It is a class level annotation indicating that an object is a source of bean definitions. @Configuration classes declare beans through @Bean annotated method.

@EnableAutoConfiguration : It enables to spring boot to auto-configure the application context. So it automatically creates and registers beans based on both the included jar files in the class path and the beans defined by us.

@ComponentScan : It is used for auto detecting and registering spring managed components like beans, controllers, repository, services etc.

Java Spring Boot First Example in Spring Tool Suite(STS)

 

JAVA with Silan Software

First Spring Boot Application in STS

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

         <modelVersion>4.0.0</modelVersion>

         <parent>

                  <groupId>org.springframework.boot</groupId>

                  <artifactId>spring-boot-starter-parent</artifactId>

                  <version>3.3.3</version>

                  <relativePath/> <!-- lookup parent from repository -->

         </parent>

         <groupId>com.iter</groupId>

         <artifactId>SpringBootFirstExample</artifactId>

         <version>0.0.1-SNAPSHOT</version>

         <name>SpringBootFirstExample</name>

         <description>My First Spring Boot Example</description>

         <url/>

         <licenses>

                  <license/>

         </licenses>

         <developers>

                  <developer/>

         </developers>

         <scm>

                  <connection/>

                  <developerConnection/>

                  <tag/>

                  <url/>

         </scm>

         <properties>

                  <java.version>17</java.version>

         </properties>

         <dependencies>

                  <dependency>

                           <groupId>org.springframework.boot</groupId>

                           <artifactId>spring-boot-starter-web</artifactId>

                  </dependency>

 

                  <dependency>

                           <groupId>org.springframework.boot</groupId>

                           <artifactId>spring-boot-devtools</artifactId>

                           <scope>runtime</scope>

                           <optional>true</optional>

                  </dependency>

                  <dependency>

                           <groupId>org.springframework.boot</groupId>

                           <artifactId>spring-boot-starter-test</artifactId>

                           <scope>test</scope>

                  </dependency>

         </dependencies>

 

         <build>

                  <plugins>

                           <plugin>

                                    <groupId>org.springframework.boot</groupId>

                                    <artifactId>spring-boot-maven-plugin</artifactId>

                           </plugin>

                  </plugins>

         </build>

 

</project>

 

TestController.java

package com.soa.controller;

 

import org.springframework.web.bind.annotation.GetMapping;

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

 

@RestController

public class TestController {

        

         @GetMapping("/hello")

         public String display()

         {

                  return "Welcome to Java Spring Boot";

         }

        

 

}