What does the @SpringBootApplication annotation do in a Spring Boot project?
It marks the main class of a Spring Boot application and combines three important annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan to simplify setup.
Click to reveal answer
intermediate
Which three annotations are combined inside @SpringBootApplication?
1. @Configuration - marks the class as a source of bean definitions.<br>2. @EnableAutoConfiguration - tells Spring Boot to start adding beans based on classpath settings.<br>3. @ComponentScan - enables scanning for other components, configurations, and services in the package.
Click to reveal answer
beginner
Why is @ComponentScan important in @SpringBootApplication?
It tells Spring where to look for other components like controllers and services, usually scanning the package of the main class and its sub-packages automatically.
Click to reveal answer
intermediate
What role does @EnableAutoConfiguration play in @SpringBootApplication?
It automatically configures Spring application based on the dependencies found on the classpath, reducing the need for manual configuration.
Click to reveal answer
beginner
Can you use @SpringBootApplication without specifying a base package for scanning?
Yes, by default it scans the package of the class annotated with @SpringBootApplication and its sub-packages, so usually no need to specify base packages explicitly.
Click to reveal answer
What annotation does @SpringBootApplication NOT include?
A@Configuration
B@EnableAutoConfiguration
C@ComponentScan
D@RestController
✗ Incorrect
@SpringBootApplication includes @Configuration, @EnableAutoConfiguration, and @ComponentScan but NOT @RestController.
What is the main benefit of using @EnableAutoConfiguration?
AManually configure beans
BAutomatically configure beans based on dependencies
CScan for components
DStart the application server
✗ Incorrect
@EnableAutoConfiguration automatically configures beans based on the dependencies found on the classpath.
Where does @ComponentScan look for components by default?
APackage of the annotated class and sub-packages
BExternal libraries
COnly the annotated class
DRoot package of the project
✗ Incorrect
@ComponentScan scans the package where the annotated class is and all its sub-packages.
Which annotation would you use to mark a class as a source of bean definitions?
A@ComponentScan
B@EnableAutoConfiguration
C@Configuration
D@SpringBootApplication
✗ Incorrect
@Configuration marks a class as a source of bean definitions.
Is it necessary to add @ComponentScan explicitly when using @SpringBootApplication?
ANo, it is included by default
BOnly for external packages
CYes, always
DOnly for testing
✗ Incorrect
@ComponentScan is included inside @SpringBootApplication, so explicit use is usually not needed.
Explain what @SpringBootApplication does and why it is useful in a Spring Boot project.
Think about how it reduces the need for multiple annotations.
You got /4 concepts.
Describe the role of @EnableAutoConfiguration and how it helps developers.
Focus on automation and dependency detection.
You got /4 concepts.
Practice
(1/5)
1. What does the @SpringBootApplication annotation primarily do in a Spring Boot project?
easy
A. It only enables component scanning in the project.
B. It combines @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations.
C. It disables auto-configuration to allow manual setup.
D. It marks a class as a REST controller.
Solution
Step 1: Understand the purpose of @SpringBootApplication
This annotation is a shortcut that bundles three annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Step 2: Identify what each bundled annotation does
@Configuration marks the class as a source of bean definitions, @EnableAutoConfiguration enables automatic configuration based on classpath settings, and @ComponentScan scans for components in the package.
Final Answer:
It combines @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations. -> Option B
Quick Check:
@SpringBootApplication = Combination of 3 annotations [OK]
Hint: Remember it bundles config, auto-config, and scanning [OK]
Common Mistakes:
Thinking it only enables component scanning
Assuming it disables auto-configuration
Confusing it with @RestController
2. Which of the following is the correct way to use @SpringBootApplication in a Spring Boot main class?
easy
A. Place @SpringBootApplication on the main class and include a main method with SpringApplication.run().
B. Use @SpringBootApplication on any class without a main method.
C. Add @SpringBootApplication on every component class.
D. Use @SpringBootApplication only on configuration classes without main methods.
Solution
Step 1: Identify where @SpringBootApplication belongs
This annotation should be on the main class that starts the Spring Boot application.
Step 2: Confirm the main method usage
The main class must have a main method that calls SpringApplication.run() to launch the app.
Final Answer:
Place @SpringBootApplication on the main class and include a main method with SpringApplication.run(). -> Option A
Quick Check:
Main class + @SpringBootApplication + main method = correct setup [OK]
Hint: Use @SpringBootApplication only on main app class with main() [OK]
Common Mistakes:
Putting @SpringBootApplication on every component
Omitting the main method
Using it on classes without starting the app
3. Given this main class code snippet, what will happen when you run it?
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
medium
A. The Spring Boot application starts with auto-configuration and component scanning.
B. The application throws a runtime error due to missing @ComponentScan.
C. The application starts but without any auto-configuration.
D. The code will not compile because @SpringBootApplication is missing parameters.
Solution
Step 1: Analyze the use of @SpringBootApplication
The annotation enables auto-configuration and component scanning by default.
Step 2: Check the main method execution
The SpringApplication.run() method starts the Spring Boot app with all default behaviors.
Final Answer:
The Spring Boot application starts with auto-configuration and component scanning. -> Option A
Quick Check:
@SpringBootApplication triggers auto-config and scanning [OK]
Hint: Default behavior includes auto-config and scanning [OK]
Common Mistakes:
Assuming @ComponentScan is required separately
Thinking auto-configuration is off by default
Believing parameters are mandatory for @SpringBootApplication
4. You have this code snippet:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
What is the purpose of the exclude attribute here?
medium
A. It changes the base package for component scanning.
B. It enables additional auto-configuration classes.
C. It disables the specified auto-configuration classes during startup.
D. It marks the class as a configuration without scanning.
Solution
Step 1: Understand the exclude attribute in @SpringBootApplication
The exclude attribute tells Spring Boot to skip certain auto-configuration classes.
Step 2: Identify the effect of excluding DataSourceAutoConfiguration
This prevents Spring Boot from automatically configuring a database connection, useful if you want manual setup.
Final Answer:
It disables the specified auto-configuration classes during startup. -> Option C