Spring Boot中SpringApplication.run方法传入的*.class参数有何作用?
*.class to SpringApplication.run() in Spring Boot? Great question! Let's break down exactly why that SimpleApplication.class reference is critical when starting your Spring Boot app, and what it does behind the scenes:
1. It's Your App's Primary Configuration Class
The class you pass (like SimpleApplication.class) acts as the starting point for Spring's configuration system. Since your class is annotated with @SpringBootApplication, which combines three key annotations:
@Configuration: Marks the class as a source of bean definitions@ComponentScan: Tells Spring to scan for components (like@Service,@Repository,@Controller) in the class's package and subpackages@EnableAutoConfiguration: Triggers Spring Boot's automatic configuration magic
Spring needs this reference to know where to begin loading all the configuration and bean definitions that make your app work.
2. It Defines the Base Package for Component Scanning
If you don't explicitly specify packages in @ComponentScan, Spring defaults to using the package of the class you pass to SpringApplication.run() as the base scanning package. For your example:
SimpleApplicationlives inorg.java.simpleapp.simpleapp- Spring will automatically scan this package and all its subpackages for any classes with stereotype annotations (
@Component,@Service, etc.)
Without this class reference, Spring wouldn't know which packages to scan, and your custom beans might never be picked up.
3. It Acts as the Anchor for the Application Context
Spring Boot uses this class to initialize the ApplicationContext (the core container that manages all your beans). The context is built using this class as the primary configuration source—any other @Configuration classes you have are either scanned automatically (thanks to step 2) or imported explicitly via annotations on this main class.
4. It Provides Application Metadata
Spring uses this class to gather metadata about your application:
- It can infer the application name (if you don't set it explicitly in
application.properties) from the class name - It looks for custom
ApplicationContextInitializerorApplicationListenerbeans associated with this class, which let you hook into the app's startup lifecycle
Why Is This Necessary?
Put simply: Spring Boot can't guess where your app's configuration starts. Without passing this class reference, the framework has no way to know which packages to scan, which auto-configurations to enable, or where to load bean definitions from. Your app would fail to start because the core Spring container wouldn't be properly initialized.
To test this, try removing the class reference (or passing a random class) — you'll see your app either fails to start, or doesn't pick up your custom components and configurations.
Example code from your question:
package org.java.simpleapp.simpleapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SimpleApplication { public static void main(String[] args) { SpringApplication.run(SimpleApplication.class, args); } }
内容的提问来源于stack exchange,提问作者nnkparikh

