๐ฑ Spring Boot Interview Series – Q4 ๐ก: @SpringBootApplication — The Swiss Army Knife of Spring Boot! ๐คฏ๐ฟ️
๐ฏ @SpringBootApplication — The Swiss Army Knife of Spring Boot ๐ ️
Deep-dive with colors, code, jokes, and interview ammo.
1️⃣ What does @SpringBootApplication
actually do?
It’s a meta-annotation that turns a plain Java class into your Spring Boot app’s “control center”. It:
- ๐ฆ Marks the class as a configuration source (beans live here).
- ๐ค Triggers auto-configuration based on what’s on the classpath.
- ๐ฐ️ Starts a component scan from this package downward.
// src/main/java/com/javabeanbag/Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
๐ง Memory trick: Think of it as “Config + AutoConfig + Scan” packed into one.
2️⃣ What is it combining under the hood?
- ๐ @Configuration — declares bean methods.
- ⚙️ @EnableAutoConfiguration — wires defaults based on classpath.
- ๐ @ComponentScan — finds
@Component
,@Service
,@Repository
,@Controller
, etc.
3️⃣ Package Gotcha: “Why are my beans invisible?” ๐ค
@ComponentScan
starts at the package of your main class and scans subpackages only.
If your beans live in parallel or unrelated packages, they won’t be discovered.
✅ Fixes (pick one):
- Place the main class in a top-level “root” package that contains all modules as subpackages. (Cleanest!)
- Specify scan bases explicitly:
@SpringBootApplication(scanBasePackages = {
"com.javabeanbag", // your app
"com.partner.lib.adapters" // external package
})
public class Application { ... }
๐ฝ Using JPA? You may also need:
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EntityScan(basePackages = "com.data.entities")
@EnableJpaRepositories(basePackages = "com.data.repos")
public class Application { ... }
4️⃣ Excluding Auto-Configuration (Annotation & Property)
Sometimes Boot is too helpful (it configures Hibernate because you added spring-boot-starter-data-jpa
),
but you want to use plain JDBC or a custom setup. Exclude the auto-config you don’t want:
๐ง Annotation way
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
@SpringBootApplication(exclude = { HibernateJpaAutoConfiguration.class })
public class Application { ... }
๐งพ Properties way
# application.properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
✨ When to exclude?
- To take manual control over a subsystem (e.g., custom DataSource).
- You have a conflicting bean and want Boot to back off entirely.
- A library is on the classpath but you’re not using that feature.
5️⃣ “Unknown” Facts & Gotchas (Interview Candy) ๐ฌ
-
๐งฌ Boot 3+ auto-config discovery changed: many auto-configs are listed under
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
(not onlyspring.factories
as in older versions). Knowing both impresses interviewers. -
๐งช
@SpringBootConfiguration
exists (Boot’s specialized@Configuration
) and is meta-used by@SpringBootApplication
. -
๐งญ Boot’s auto-config generally backs off if you declare your own bean (
@ConditionalOnMissingBean
is common). -
๐งน You can narrow scanning using
@ComponentScan(includeFilters/excludeFilters)
with custom stereotypes for massive monorepos.
@SpringBootApplication
@ComponentScan(
basePackages = "com.bigcorp",
includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com\\.bigcorp\\.feature\\..*")
)
public class Application { ... }
6️⃣ Fun Analogy ๐
@SpringBootApplication is like a hotel “all-inclusive” wristband: it gets you food (config), activities (auto-config), and room access (component scan). Sometimes you still say “no thanks” to the karaoke (exclude a specific auto-config).
7️⃣ Mini Examples You’ll Use Often
A) Split packages (library + app)
@SpringBootApplication(scanBasePackages = {"com.app", "com.lib.common"})
public class Application { ... }
B) Override Boot’s DataSource
@SpringBootApplication(exclude = { org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class })
public class Application { ... }
// Then define your own @Bean DataSource...
C) Keep Boot’s JPA but custom Hibernate props
# application.properties
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
8️⃣ Interview Questions (with quick hints)
- What does @SpringBootApplication bundle? → Config + AutoConfig + ComponentScan.
- How does Boot decide what to auto-configure? → Classpath checks + conditions; backs off if you define beans.
- Why did my component not load? → Scanning starts at main class package; fix via root package or
scanBasePackages
. - How to exclude an auto-config and when? → Annotation/property; use when conflicting or unwanted defaults.
- What changed in Boot 3 auto-config discovery? → Uses
AutoConfiguration.imports
file (not justspring.factories
).
๐ค AutoConfiguration.import vs spring.factories — What’s the Difference?
In Spring Boot, both spring.factories
and @AutoConfiguration.import
are used to load auto-configuration classes — but they work differently, and newer Spring versions have moved towards @AutoConfiguration.import.
1️⃣ Old Style — META-INF/spring.factories
- We place a file at
META-INF/spring.factories
in our JAR. - Inside it, we map the
org.springframework.boot.autoconfigure.EnableAutoConfiguration
key to our auto-configuration classes.
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.MyAutoConfiguration
✅ Pros: Works in Spring Boot 1.x and 2.x (backward compatible)
❌ Cons: Harder to read for large projects, all config in one big file.
2️⃣ New Style — META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
- Spring Boot 2.7+ introduced this cleaner approach.
- Instead of a properties file, we list configuration classes line-by-line.
com.example.MyAutoConfiguration
com.example.OtherAutoConfiguration
✅ Pros: Cleaner, supports modular design, no key/value mapping.
❌ Cons: Not supported in older Spring Boot versions.
๐ Which Should You Use?
- If you need backward compatibility (Spring Boot 2.6 or older) → use spring.factories.
- If you are on Spring Boot 2.7+ or 3.x → use AutoConfiguration.imports.
- Both ultimately help Spring discover and load your auto-configuration classes at startup.
๐ก Example Scenario
You are building a reusable payment library. When added to another Spring Boot project:
- Using spring.factories: payment configs auto-load in both old and new projects.
- Using AutoConfiguration.imports: payment configs auto-load in Spring Boot 3.x with cleaner config files.
Comments
Post a Comment