1.

Which Annotation do you use to enable Spring Boot? Elaborate?

Answer»

@SpringBootApplication is the primary annotation which needs to be added to the Application class or the MAIN class to the project and enables features like JAVA-BASED Spring configuration, component scanning, and auto-configuration. An Application class is used to BOOTSTRAP and launch a Spring application from a Java main method. This class automatically creates the ApplicationContext from the classpath, scan the configuration classes and launch the application.

@SpringBootApplication is essentially a combination of below annotations:

  • @Configuration: is used to mark a class as a source of bean definitions.
  • @ComponentScan: is used to have Spring scan the package for @Configuration classes.
  • @EnableAutoConfiguration: to enable Spring to determine the configuration based on the classpath.

Following parameters are accepted in the @SpringBootApplication annotation:

  • exclude: This is used to exclude the list of classes from the auto configuration.
  • excludeNames: To exclude the list of fully qualified class names (class names with package info) from the auto configuration.
  • scanBasePackageClasses: To notify Spring the list of classes that has to be applied for the @ComponentScan.
  • scanBasePackages: To provide a list of packages that have to be applied for the @ComponentScan. 

Example:

package com.example.demoApplication; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication(exclude = { SecurityConfiguration.class }) public class Application { public static VOID main(String[] args) { SpringApplication.run(Application.class, args); } }


Discussion

No Comment Found