Skip to content

Application main class

While creating the base application based on Spring Boot, we can see that it always contains a class with the main method, which is marked with the annotation @SpringBootApplication. Most often we do not change the content of this class.

main method

In projects based on Spring Boot, the main method usually has one line:

SpringApplication.run(SbApplication.class, args)

Depending on the dependencies we have in the pom.xml file, the above line of code may do something else. For example, when the dependencies include starter spring-boot-starter-web, the application will be launched by default on theTomcat server (and port 8080).

@SpringBootApplication annotations

The @SpringBootApplication annotation provides several functionalities to the application. They result from the fact that the @ SpringBootApplication annotation itself is an annotation consisting of several others. These include:

  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan

@SpringBootConfiguration

The @SpringBootConfiguration annotation works the same as the @Configuration annotation. TWitihn it's contents we can define other beans.

NOTE: You can find tons of examples on the internet where beans are defined inside an annotated class @SpringBootConfiguration. This is a good way to present an example, but in practice, we define beans in separate configuration classes.

@EnableAutoConfiguration

The @EnableAutoConfiguration annotation enables the auto-configuration mechanism. Thanks to it, the application creates a certain context based on the configuration and dependencies in the pom.xml file.

@ComponentScan

The @ComponentScan annotation is an annotation informing in which packages should be searched for the context beans. By default, this annotation scans the current package (and the packages inside it).

Below we present an example, for a class marked with the annotation @SpringBootApplication in the pl.sdacademy.sb package:

package pl.sdacademy.sb;

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

@SpringBootApplication
public class SbApplication {

  public static void main(String[] args) {
    SpringApplication.run(SbApplication.class, args);
  }

}

All classes where the package name starts with pl.sdacademy.sb will be put into the context. For example, the following two classes will go into the context of this app:

package pl.sdacademy.sb;

import org.springframework.stereotype.Component;

@Component
public class ClassWithinComponentScanRange {
}
package pl.sdacademy.sb.inner;

import org.springframework.stereotype.Component;

@Component
public class InnerClass {
}

but the next class will behave in a different way (so we won't be able to inject it anywhere):

package pl.sdacademy; // package name doesn't start with pl.sdacademy.sb;

import org.springframework.stereotype.Component;

@Component
public class OutOfContextClass {
}

Problems like the one seen above can be solved by adding the annotation @ComponentScan over any class that is marked with the annotationConfiguration (and thus also @SpringBootApplication). In the basePackages field we can indicate packages that should be used to search for beans inside of them. The following modification of the SbApplication class will cause theOutOfContextClass class to go into the context.

@SpringBootApplication
@ComponentScan(basePackages = "pl.sdacademy")
public class SbApplication {

  public static void main(String[] args) {
    SpringApplication.run(SbApplication.class, args);
  }
}